-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample_6_6.java
More file actions
38 lines (30 loc) · 942 Bytes
/
Example_6_6.java
File metadata and controls
38 lines (30 loc) · 942 Bytes
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
package unit_06.Examples.Example_06;
import java.util.Scanner;
public class Example_6_6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] nums = new int[10];
for(int i = 0; i < nums.length; i++){
System.out.println("Enter number " + (i + 1) + ": ");
nums[i] = scanner.nextInt();
}
insertionSort(nums);
for (int num : nums) {
System.out.print(num + " , ");
}
}
private static void insertionSort(int[] list){
int temp, loc;
for (int i = 1; i < list.length; i++) {
if(list[i] < list[i - 1]){
temp = list[i];
loc = i;
do{
list[loc] = list[loc - 1];
loc--;
}while(loc > 0 && list[loc - 1] > temp);
list[loc] = temp;
}
}
}
}