forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.java
More file actions
28 lines (21 loc) · 498 Bytes
/
insertionsort.java
File metadata and controls
28 lines (21 loc) · 498 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
public class insertionsort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 10, 99,-1, 100, 50, 29 };
insrtionsort(arr);
}
public static void insrtionsort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
for (int k = 0; k <= 4; k++) {
System.out.print(arr[k] + " ");
}
}
}