-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection.java
43 lines (38 loc) · 996 Bytes
/
Selection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Selection
{
public static void sort(Comparable[] a)
{ // Sort a[] into increasing order.
int N = a.length;
for (int i = 0; i < N; i++)
{
int min = i;
for (int j = i + 1; j < N; j++)
{
if (less(a[j], a[min])) min = j;
}
exch(a, i, min);
}
}
private static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }
private static void exch(Comparable[] a, int i, int j)
{ Comparable t = a[i]; a[i] = a[j]; a[j] = t; }
private static boolean isSorted(Comparable[] a)
{
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i - 1])) return false;
return true;
}
private static void show(String[] a)
{
for (int i = 0; i < a.length; i++) StdOut.print(a[i] + " ");
StdOut.println();
}
public static void main(String[] args)
{
String[] a = StdIn.readAllStrings();
sort(a);
assert isSorted(a);
show(a);
}
}