Skip to content

Commit eae5066

Browse files
authored
Create SelectionSortString
1 parent b0f5b63 commit eae5066

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

SelectionSortString

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.lang.*;
2+
public class sortint{
3+
public static void selectionSort(String[] arr)
4+
{
5+
for (int i = 0; i < arr.length - 1; ++i)
6+
{
7+
int minIndex = i;
8+
for (int j = i + 1; j < arr.length; ++j)
9+
{
10+
// "<" changed to use of compareTo()
11+
if (arr[j].compareTo(arr[minIndex]) < 0)
12+
{
13+
minIndex = j;
14+
}
15+
}
16+
// int changed to String
17+
String temp = arr[i];
18+
arr[i] = arr[minIndex];
19+
arr[minIndex] = temp;
20+
}
21+
for (int i = 0; i < arr.length; ++i){
22+
System.out.println(arr[i]+" ");
23+
}
24+
}
25+
26+
// Driver code
27+
public static void main(String args[])
28+
{
29+
String arr[] = {"hello", "first",
30+
"why"};
31+
selectionSort(arr);
32+
33+
34+
}
35+
}

0 commit comments

Comments
 (0)