-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection.java
More file actions
39 lines (39 loc) · 1.12 KB
/
Selection.java
File metadata and controls
39 lines (39 loc) · 1.12 KB
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
import java.util.Random;
public class Selection{
public static void sort(Comparable[] a){
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);
}
}
public static boolean less(Comparable a, Comparable b){
return a.compareTo(b)<0;
}
public static void exch(Comparable[] a, int i,int j){
Comparable t=a[i];a[i]=a[j];a[j]=t;
}
public static void show(Comparable[] a){
for (int i=0;i<a.length;i++){System.out.println(a[i]+"");}
}
public static boolean isSorted(Comparable[] a){
for (int i=1;i<a.length;i++){
if (less(a[i],a[i-1])) {return false;}
}
return true;
}
public static void main(String[] args){
Random r = new Random();
Integer[] a=new Integer [10];
for(int i=0 ; i<10 ; i++){
int ran1 = r.nextInt(100);
a[i]=ran1;
}
sort(a);
assert isSorted(a);
show(a);
}
}