forked from bishweashwarsukla/hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion Sort.java
44 lines (42 loc) · 1.15 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
import java.util.*;
public class InsertionSort
{
void sort(int arr[])//Sorting the Array
{
int n=arr.length;
for(int i=1;i<n;++i)
{
int key=arr[i];
int j=i-1;
while(j>=0&&arr[j]>key)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}
void printArray(int arr[])//Printing the Array
{
int n=arr.length;
for(int i=0;i<n;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
void main()//Inputting the value and passing it to the coerresponding Function
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[10];
for(int p=0;p<arr.length;p++)
{
System.out.println("Enter the Element to placed at["+p+"]:");
arr[p]=sc.nextInt();
}
System.out.println("Given Array");
printArray(arr);
InsertionSort ob=new InsertionSort();
ob.sort(arr);
System.out.println("Sorted Array");
printArray(arr);
}
}