-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.java
42 lines (37 loc) · 1.23 KB
/
InsertionSort.java
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
import java.util.Scanner;
public class InsertionSort {
public static void main(String[] args) {
int aux, iterator, arraySize;
int[] numbers;
Scanner input = new Scanner(System.in);
System.out.println("Insert the Array size:");
arraySize = Integer.parseInt(
input.nextLine()
);
numbers = new int[arraySize];
System.out.println("Insert the disordered values:");
for(int i = 0; i < numbers.length; i++) {
numbers[i] = Integer.parseInt(
input.nextLine()
);
}
for(int i = 1; i < numbers.length; i++) {
iterator = i;
for (int j = iterator - 1; j >= 0; j--) {
if(numbers[iterator] < numbers[j]) {
aux = numbers[iterator];
numbers[iterator] = numbers[j];
numbers[j] = aux;
iterator--;
} else {
continue;
}
}
}
System.out.println("Ok, so the ordered (by Insertion Sort) values are:");
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
input.close();
}
}