-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion_sort.java
More file actions
49 lines (47 loc) · 1.28 KB
/
Insertion_sort.java
File metadata and controls
49 lines (47 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
46
47
48
49
import java.util.Arrays;
import java.util.Scanner;
public class Insertion_sort {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter size of array- ");
int n=s.nextInt();
int ar[]=new int[n];
System.out.println("enter array- ");
for (int i = 0; i <n ; i++) {
ar[i]=s.nextInt();
}
insertion(ar);
System.out.println("array after sorting is- "+ Arrays.toString(ar));
}
//using swapping
public static void insertion(int ar[]){
int count=0;
for (int i = 1; i <ar.length ; i++) {
for (int j = i; j >0 ; j--) {
if (ar[j]<ar[j-1])
swap(ar,j-1,j);
else break;
}
}
}
public static void swap(int ar[],int i,int min){
int temp;
temp=ar[min];
ar[min]=ar[i];
ar[i]=temp;
}
//using shifting
public static void sort(int arr[])
{
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;
}
}
}