- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathcomb_sort.java
More file actions
Latest commit
45 lines (43 loc) · 1.34 KB
/
Copy pathcomb_sort.java
File metadata and controls
45 lines (43 loc) · 1.34 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
importjava.util.Scanner;
publicclasscomb_sort {
publicstaticintgetNextGap(intgap) {
gap = (gap * 10) / 13;
if (gap < 1)
return1;
returngap;
}
publicstaticvoidcombSort(int[] a) {
intn = a.length;
intgap = n;
booleanswapped = true;
while (gap != 1 || swapped) {
gap = getNextGap(gap);
swapped = false;
for (inti = 0; i < n - gap; i++) {
if (a[i] > a[i + gap]) {
inttemp = a[i];
a[i] = a[i + gap];
a[i + gap] = temp;
swapped = true;
}
}
}
}
publicstaticvoidmain(String[] args) {
Scannerscanner = newScanner(System.in);
System.out.print("Enter the length of the array: ");
intn = scanner.nextInt();
int[] a = newint[n];
System.out.println("Enter the elements of the array:");
for (inti = 0; i < n; i++)
a[i] = scanner.nextInt();
System.out.println("Original array:");
for (inti = 0; i < n; i++)
System.out.print(a[i] + " ");
combSort(a);
System.out.println("\nSorted array:");
for (inti = 0; i < n; i++)
System.out.print(a[i] + " ");
scanner.close();
}
}