Skip to content

Commit

Permalink
Create shellsort.java
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilkotlobudde authored Oct 25, 2022
1 parent 9821be7 commit a298f45
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions shellsort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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);
}
}

0 comments on commit a298f45

Please sign in to comment.