-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
95 lines (85 loc) · 2.69 KB
/
SelectionSort.java
File metadata and controls
95 lines (85 loc) · 2.69 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.Comparator;
/**
* Best - O(n^2)
* Avg - O(n^2)
* Worst - O(n^2)
*
* Applications:
* - Cyber Security when password sorting and matching should
* always take a certain amount of time so as not to give
* away information during execution
*/
public class SelectionSort <E> {
public static void selectionSort(int[] data){
for(int k = 0; k < data.length -1; k++){
int i = k; // index
for(int j = k + 1; j < data.length; j++){
if (data[j] < data[i]) {
i = j; // Search for Lowest index
}
}
// Swap
int smaller = data[i];
data[i] = data[k];
data[k] = smaller;
}
}
public static void selectionSort(char[] data){
for(int k = 0; k < data.length -1; k++){
int i = k; // index
for(int j = k + 1; j < data.length; j++){
if (data[j] < data[i]) {
i = j; // Search for Lowest index
}
}
// Swap
char smaller = data[i];
data[i] = data[k];
data[k] = smaller;
}
}
Comparator<E> comp = (E x, E y) -> compare(x,y);
/**
* Compares Two Keys by natural ordering
* @param x First key to compare
* @param y Second key to compare
* @return -1 if x < y,
* 0 if x==y,
* 1 if x > y
*/
protected int compare(E x, E y){
return comp.compare(x,y);
}
public void selectionSort(E[] data){
for(int k = 0; k < data.length -1; k++){
int i = k; // index
for(int j = k + 1; j < data.length; j++){
if (comp.compare(data[j],data[i]) < 0) {
i = j; // Search for Lowest index
}
}
// Swap
E smaller = data[i];
data[i] = data[k];
data[k] = smaller;
}
}
public static void main(String args[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr1);//sorting array using selection sort
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
char[] a = {'C', 'E', 'B', 'D', 'A', 'I', 'J', 'L', 'K', 'H', 'G', 'F'};
System.out.println(java.util.Arrays.toString(a));
selectionSort(a);
System.out.println(java.util.Arrays.toString(a));
}
}