-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion_sort.java
More file actions
48 lines (39 loc) · 924 Bytes
/
Insertion_sort.java
File metadata and controls
48 lines (39 loc) · 924 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
39
40
41
42
43
44
45
46
47
48
/**
* @author SAGAR
*
*/
import java.util.Scanner;
public class Insertion_sort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner x = new Scanner(System.in);
System.out.println("Enter the Size of Array");
int size = x.nextInt();
System.out.println("Enter the Array");
int array[] = new int[size];
for (int i = 0; i < size; i++) {
array[i] = x.nextInt();
}
insertionSort(array, size);
x.close();
}
public static void insertionSort(int Array[], int n) {
int key = 0, j = 0;
for (int i = 1; i < n; i++) {
key = Array[i];
j = i - 1;
while (j >= 0 && Array[j] > key) {
Array[j + 1] = Array[j];
j = j - 1;
}
Array[j + 1] = key;
}
printArray(Array, n);
}
public static void printArray(int a[],int n)
{
for (int i = 0; i < n; i++) {
System.out.print(a[i]+" ");
}
}
}