forked from ava11235/it212
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.java
76 lines (60 loc) · 2.1 KB
/
SelectionSort.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//selection sort
import java.util.*;
public class SelectionSort {
public static void main(String[] args) {
int n = 100000;
int[] numbers = createRandomIntArray (n);
long start = System.currentTimeMillis();
selectionSort(numbers);
double elapsed1 = (System.currentTimeMillis() - start) /
1000.0;
System.out.println("selection sort " + elapsed1
+ " seconds");
System.out.println();
boolean res = isSorted(numbers);
System.out.println("Sorted? " + res);
}
//Create a random array of ints
public static int[] createRandomIntArray(int size) {
int[] numbers = new int[size];
Random rand = new Random();
int min = rand.nextInt(size);
int max = rand.nextInt(size - min) + min;
for (int i = 0; i < size; i++) {
numbers[i] = rand.nextInt(size * 2);
}
return numbers;
}
// Return true if array a's elements are in sorted order.
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i+1]) {
return false;
}
}
return true;
}
// Swaps a[i] with a[j].
public static void swap(int[] a, int i, int j) {
if (i != j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
// Places the elements of the given array into sorted order
// using the selection sort algorithm.
// post: array is in sorted (nondecreasing) order
public static void selectionSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
// find index of smallest element
int smallest = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[smallest]) {
smallest = j;
}
}
swap(a, i, smallest); // swap smallest to front
}
}
}