From cdd30c0fdc5cbbdaae7bb8a4001f5aeac5e649b0 Mon Sep 17 00:00:00 2001 From: techgirl0110 <116191142+techgirl0110@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:58:46 +0530 Subject: [PATCH] Added a file of shell sort Kindly review and the pr --- Sorting/shell_sort.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Sorting/shell_sort.java diff --git a/Sorting/shell_sort.java b/Sorting/shell_sort.java new file mode 100644 index 0000000..d8bbc71 --- /dev/null +++ b/Sorting/shell_sort.java @@ -0,0 +1,41 @@ +class ShellSort { +/* function to implement shellSort */ +static void shell(int a[], int n) +{ + /* Rearrange the array elements at n/2, n/4, ..., 1 intervals */ + for (int interval = n/2; interval > 0; interval /= 2) + { + for (int i = interval; i < n; i += 1) + { + /* store a[i] to the variable temp and make + +the ith position empty */ + int temp = a[i]; + int j; + for (j = i; j >= interval && a[j - interval] > +temp; j -= interval) + a[j] = a[j - interval]; + + /* put temp (the original a[i]) in its correct +position */ + a[j] = temp; + } + } +} +static void printArr(int a[], int n) /* function to print the array elements */ +{ + int i; + for (i = 0; i < n; i++) + System.out.print(a[i] + " "); +} +public static void main(String args[]) +{ + int a[] = { 30, 28, 37, 5, 9, 14, 22, 39 }; + int n = a.length; + System.out.print("Before sorting array elements are - \n"); + printArr(a, n); + shell(a, n); + System.out.print("\nAfter applying shell sort, the array elements are - \n"); + printArr(a, n); +} +}