Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Sorting/shell sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class ShellSort
{
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
int sort(int arr[])
{
int n = arr.length;


for (int gap = n/2; gap > 0; gap /= 2)
{

for (int i = gap; i < n; i += 1)
{

int temp = arr[i];


int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
public static void main(String args[])
{
int arr[] = {12, 34, 54, 2, 3};
System.out.println("Array before sorting");
printArray(arr);

ShellSort ob = new ShellSort();
ob.sort(arr);

System.out.println("Array after sorting");
printArray(arr);
}
}