forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
45 lines (41 loc) · 1.28 KB
/
InsertionSort.java
File metadata and controls
45 lines (41 loc) · 1.28 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
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
public class InsertionSort
{
private static void insertionSort(int array[], int size){
for (int i = 1; i < size; i++){
int key = array[i];
int j = i -1;
while ( j >= 0 && array[j] > key) {
array[j+1] = array[j];
j = j - 1;
}
array[j+1] = key;
}
}
private static void printArray(int array[]) {
for(int i = 0; i < array.length;i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
try {
int i,n;
System.out.println("How many elements do you want in the array?");
n=scan.nextInt();
int a[]= new int[n];
System.out.println("Enter the array elements one by one");
for(i=0;i<n;i++)
{
a[i]=scan.nextInt();
}
System.out.println("Array before sorting is:");
printArray(a);
System.out.println("Array before sorting is:");
insertionSort(a, a.length);
printArray(a);
} catch(Exception e) {e.printStackTrace();}
scan.close();
}
}