Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit adb4e39

Browse files
committed
Added generic shell sort implementation in Sorts folder
1 parent 230a005 commit adb4e39

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Sorts/ShellSort.java

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

0 commit comments

Comments
 (0)