Skip to content

Commit ee3f820

Browse files
authored
Add Simple Sort (TheAlgorithms#2545)
1 parent 71c91a1 commit ee3f820

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Sorts/SimpleSort.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package Sorts;
2+
3+
import static Sorts.SortUtils.*;
4+
5+
public class SimpleSort implements SortAlgorithm {
6+
7+
@Override
8+
public <T extends Comparable<T>> T[] sort(T[] array) {
9+
final int LENGTH = array.length;
10+
11+
for (int i = 0; i < LENGTH; i++) {
12+
for (int j = i + 1; j < LENGTH; j++) {
13+
if (less(array[j], array[i])) {
14+
T element = array[j];
15+
array[j] = array[i];
16+
array[i] = element;
17+
}
18+
}
19+
}
20+
21+
return array;
22+
}
23+
24+
public static void main(String[] args) {
25+
// ==== Int =======
26+
Integer[] a = { 3, 7, 45, 1, 33, 5, 2, 9 };
27+
System.out.print("unsorted: ");
28+
print(a);
29+
System.out.println();
30+
31+
new SimpleSort().sort(a);
32+
System.out.print("sorted: ");
33+
print(a);
34+
System.out.println();
35+
36+
// ==== String =======
37+
String[] b = { "banana", "berry", "orange", "grape", "peach", "cherry", "apple", "pineapple" };
38+
System.out.print("unsorted: ");
39+
print(b);
40+
System.out.println();
41+
42+
new SimpleSort().sort(b);
43+
System.out.print("sorted: ");
44+
print(b);
45+
}
46+
}

0 commit comments

Comments
 (0)