Skip to content

Commit e080b26

Browse files
author
PARISOT paul andrea
committed
adding of the combSort.java
modification of CombSort
1 parent 5d164ea commit e080b26

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

Sorts/CombSort.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
*
3+
* @author Paul Parisot (https://github.com/tosirap)
4+
*
5+
*/
6+
7+
public class CombSort{
8+
/**
9+
* This method implements the Generic comb Sort
10+
* @param arr The array to be sorted
11+
* Sorts the array in increasing order
12+
**/
13+
14+
public static <T extends Comparable<T>> void cS(T[] arr) {
15+
int intervalle = arr.length; //initialisation of intervalle
16+
boolean echange = true; //initialisation at true;
17+
18+
while(intervalle>1 || echange){
19+
intervalle /= 1.3;
20+
if(intervalle<1){
21+
intervalle=1;
22+
}
23+
24+
int i=0;
25+
echange= false;
26+
27+
while(i<(arr.length-intervalle)){
28+
if(arr[i].compareTo(arr[i+intervalle])>0){
29+
T temp = arr[i];
30+
arr[i]=arr[i+intervalle];
31+
arr[i+intervalle]=temp;
32+
echange=true;
33+
}
34+
i++;
35+
}
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
41+
// Integer Input
42+
int[] arr1 = {4,23,6,78,1,54,231,9,12};
43+
44+
Integer[] array = new Integer[arr1.length];
45+
for (int i=0;i<arr1.length;i++){
46+
array[i] = arr1[i];
47+
}
48+
49+
cS(array);
50+
51+
// Output => 1 4 6 9 12 23 54 78 231
52+
for(int i=0; i<array.length; i++){
53+
System.out.print(array[i]+"\t");
54+
}
55+
56+
System.out.println();
57+
58+
// String Input
59+
String[] array1 = {"c", "a", "e", "b","d"};
60+
61+
cS(array1);
62+
63+
//Output => a b c d e
64+
for(int i=0; i<array1.length; i++){
65+
System.out.print(array1[i]+"\t");
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)