mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-14 09:22:32 +08:00
Add Simple Sort (#2545)
This commit is contained in:
46
Sorts/SimpleSort.java
Normal file
46
Sorts/SimpleSort.java
Normal file
@ -0,0 +1,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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user