-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINSERTION_SORT.java
More file actions
31 lines (28 loc) · 846 Bytes
/
INSERTION_SORT.java
File metadata and controls
31 lines (28 loc) · 846 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
import java.util.*;
import java.util.Arrays;
public class INSERTION_SORT {
public static void INSERTIONSORT(int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
int curr = arr[i];
int prev = i - 1;
// finding out correct pos to inmsert
while (prev >= 0 && arr[prev] > curr) {
arr[prev + 1] = arr[prev];
prev--;
}
// INSertion array
arr[prev + 1] = curr;
}
}
public static void Printarray(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String args[]) {
int arr[] = { 5, 4, 1, 3, 2 };
Arr
Printarray(arr);
}
}