-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombSort.java
More file actions
48 lines (37 loc) · 796 Bytes
/
CombSort.java
File metadata and controls
48 lines (37 loc) · 796 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
47
48
package array.sorting;
public class CombSort{
int nextGap(int gap){
gap = (gap * 10) / 13;
if (gap < 1)
return 1;
return gap;
}
void Csort(int arr[]) {
int n = arr.length;
int gap = n;
boolean swapped = true;
while (gap != 1 || swapped == true) {
gap = nextGap(gap);
swapped = false;
for (int i = 0; i < n - gap; i++) {
if (arr[i] > arr[i + gap]) {
int temp = arr[i];
arr[i] = arr[i + gap];
arr[i + gap] = temp;
swapped = true;
}
}
}
}
public static void main(String args[])
{
CombSort a = new CombSort();
int arr[] = {8,4,6,3,2,5,7,8,54,3};
a.Csort(arr);
System.out.println("sorted array");
for (int i = 0; i < arr.length; ++i){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}