forked from ashishjohn1908/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSortAlg.java
More file actions
46 lines (34 loc) · 979 Bytes
/
Copy pathInsertSortAlg.java
File metadata and controls
46 lines (34 loc) · 979 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
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Arrays;
/**
* Created by IntelliJ IDEA.
* User: plamen
* Date: 29-Aug-2009
* Time: 13:21:35
* To change this template use File | Settings | File Templates.
*/
public class InsertSortAlg {
static int[] A = {77, 99, 44, 55, 22, 88, 11, 0, 66, 33};
public static void main(String[] args) {
System.out.println("A before the sort: " + Arrays.toString(A));
sortArray(A);
System.out.println("A after the sort: " + Arrays.toString(A));
}
static boolean greaterThan(int a, int b) {
return (a > b);
}
static void swap(int A[], int a, int b) {
int temp = A[a];
A[a] = A[b];
A[b] = temp;
}
static void sortArray(int[] A) {
int n = A.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (greaterThan(A[i], A[j])) {
swap(A, i, j);
}
}
}
}
}