-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion Sort
More file actions
33 lines (33 loc) · 902 Bytes
/
Insertion Sort
File metadata and controls
33 lines (33 loc) · 902 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
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) throws java.lang.Exception {
//your code here
//taking Inputs
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
//function called
InsertionSort(arr);
}
//Insertion sort
static void InsertionSort(int[] arr) {
for (int i = 0; i < arr.length ; i++) {
int current=arr[i];
int j=i-1;
while(j>=0 && current<arr[j])
{
arr[j+1]=arr[j];
j--;
}
//placing element
arr[j+1]=current;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}