forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSort.java
More file actions
46 lines (37 loc) · 1.18 KB
/
SimpleSort.java
File metadata and controls
46 lines (37 loc) · 1.18 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
package Sorts;
import static Sorts.SortUtils.*;
public class SimpleSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
final int LENGTH = array.length;
for (int i = 0; i < LENGTH; i++) {
for (int j = i + 1; j < LENGTH; j++) {
if (less(array[j], array[i])) {
T element = array[j];
array[j] = array[i];
array[i] = element;
}
}
}
return array;
}
public static void main(String[] args) {
// ==== Int =======
Integer[] a = { 3, 7, 45, 1, 33, 5, 2, 9 };
System.out.print("unsorted: ");
print(a);
System.out.println();
new SimpleSort().sort(a);
System.out.print("sorted: ");
print(a);
System.out.println();
// ==== String =======
String[] b = { "banana", "berry", "orange", "grape", "peach", "cherry", "apple", "pineapple" };
System.out.print("unsorted: ");
print(b);
System.out.println();
new SimpleSort().sort(b);
System.out.print("sorted: ");
print(b);
}
}