forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion_Sort.java
56 lines (47 loc) · 1.09 KB
/
Insertion_Sort.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
class Insertion_Sort
{
// function for insertion sort
public static void InsertionSort(int[] array, int size)
{
int temp, j;
for(int i = 1; i < size; i++)
{
temp = array[i];
j = i - 1;
// Do swapping
while(j >= 0 && array[j] > temp)
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
// function ro print array
public static void Print_Array(int[] array, int size)
{
for(int i = 0; i < size; i++)
System.out.print(array[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
int num;
Scanner s = new Scanner(System.in);
num = s.nextInt();
int array[] = new int[num];
for (int i = 0; i < num; i++) {
array[i] = s.nextInt();
}
InsertionSort(array, num);
Print_Array(array, num);
}
}
/*
Input :
num = 6
array = [1, 6, 3, 3, 5, 2]
Output :
[1, 2, 3, 3, 5, 6]
*/