-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
34 lines (30 loc) · 1.21 KB
/
InsertionSort.java
File metadata and controls
34 lines (30 loc) · 1.21 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
class InsertionSort {
public static void main(String[] array) {
sort(array);
printArray(array);
}
public static void sort(String[] array){
//Loops through every indeks from 1 to the end:
for (int i = 1; i < array.length; i++){
int j = i;
//If the previous element is bigger than the current element.
while ((j > 0) && (Integer.parseInt(array[j-1]) > Integer.parseInt(array[j]))){
//Switches place between the previous element and the current.
String previous = array[j-1];
String thisElement = array[j];
array[j-1] = thisElement;
array[j] = previous;
j = j-1;
//Checks the other lower indexes if the element is bigger.
} //Stops when every index is sortet from the currents index and bakwards to the start.
}//Stops when the whole array is sorted.
}
public static void printArray(String[] array){
//Prints the sorted array:
System.out.print("[");
for (String e: array){
System.out.print(e + ", ");
}
System.out.print("]");
}
}