diff --git a/C++/Sorting/shell_sort.cpp b/C++/Sorting/shell_sort.cpp new file mode 100644 index 000000000..42a3878c1 --- /dev/null +++ b/C++/Sorting/shell_sort.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int shellSort(int array[], int n) +{ + // Start with a big gap, then reduce the gap + for (int gap = n/2; gap > 0; gap /= 2) + { + // Perform an insertion sort within the current gap size. + // The first "gap" elements, a[0..gap-1], are already sorted with a gap between them. + // Extend the sorted sequence by adding one more element at a time until the entire array is sorted + //with the current gap. + for (int i = gap; i < n; i += 1) + { + // add a[i] to the elements that have been gap sorted + // save a[i] in temp and make a hole at position i + int temp = array[i]; + + // shift earlier gap-sorted elements up until the correct + // location for a[i] is found + int j; + for (j = i; j >= gap && array[j - gap] > temp; j -= gap) + array[j] = array[j - gap]; + + // put temp (the original a[i]) in its correct location + array[j] = temp; + } + } + return 0; +} + +void displayArray(int array[], int n) +{ + for (int i=0; i + +void shellSort(int array[], int n) { + // Start with a big gap, then reduce the gap + for (int gap = n/2; gap > 0; gap /= 2) { + // Do a gapped insertion sort for this gap size. + // The first gap elements a[0..gap-1] are already in gapped order. + // Keep adding one more element until the entire array is gap sorted. + for (int i = gap; i < n; i += 1) { + // Add array[i] to the elements that have been gap sorted. + // Save array[i] in temp and make a hole at position i. + int temp = array[i]; + int j; + + // Shift earlier gap-sorted elements up until the correct location for array[i] is found. + for (j = i; j >= gap && array[j - gap] > temp; j -= gap) + array[j] = array[j - gap]; + + // Put temp (the original array[i]) in its correct location. + array[j] = temp; + } + } +} + +void displayArray(int array[], int n) { + for (int i = 0; i < n; i++) + printf("%d ", array[i]); +} + +int main() { + int array[] = {9, 8, 1, 7, 4, 3}; + int n = sizeof(array) / sizeof(array[0]); + + printf("Array before sorting:\n"); + displayArray(array, n); + + shellSort(array, n); + + printf("\nArray after sorting:\n"); + displayArray(array, n); + + return 0; +} \ No newline at end of file diff --git a/Java/Sorting/shell_sort.java b/Java/Sorting/shell_sort.java new file mode 100644 index 000000000..7c0b94f27 --- /dev/null +++ b/Java/Sorting/shell_sort.java @@ -0,0 +1,34 @@ +import java.util.Arrays; + +class shell_sort { + public static void shellSort(int[] arr) { + int n = arr.length; + + // Start with a big gap, then reduce the gap + for (int gap = n / 2; gap > 0; gap /= 2) { + // Do a gapped insertion sort for this gap size. + // The first gap elements a[0..gap-1] are already in gapped order. + // Keep adding one more element until the entire array is gap sorted. + for (int i = gap; i < n; i++) { + int temp = arr[i]; + int j = i; + while (j >= gap && arr[j - gap] > temp) { + // Shift earlier gap-sorted elements up until the correct location for a[i] is found. + arr[j] = arr[j - gap]; + j -= gap; + } + // Put temp (the original a[i]) in its correct location. + arr[j] = temp; + } + } + } + + public static void main(String[] args) { + int[] arr = {9, 8, 1, 7, 4, 3}; + System.out.println("Array before sorting:"); + System.out.println(Arrays.toString(arr)); + shellSort(arr); + System.out.println("Array after sorting:"); + System.out.println(Arrays.toString(arr)); + } +} \ No newline at end of file diff --git a/Python/Sorting/shell_sort.py b/Python/Sorting/shell_sort.py new file mode 100644 index 000000000..fe364d1bc --- /dev/null +++ b/Python/Sorting/shell_sort.py @@ -0,0 +1,29 @@ +def shell_sort(arr): + n = len(arr) + + # Start with a big gap, then reduce the gap + gap = n // 2 + while gap > 0: + # Do a gapped insertion sort for this gap size. + # The first gap elements a[0..gap-1] are already in gapped order. + # Keep adding one more element until the entire array is gap sorted. + for i in range(gap, n): + temp = arr[i] + j = i + while j >= gap and arr[j - gap] > temp: + # Shift earlier gap-sorted elements up until the correct location for a[i] is found. + arr[j] = arr[j - gap] + j -= gap + # Put temp (the original a[i]) in its correct location. + arr[j] = temp + gap //= 2 + + return arr + +# Test the algorithm +arr = [9, 8, 1, 7, 4, 3] +print("Array before sorting:") +print(arr) +sorted_arr = shell_sort(arr) +print("Array after sorting:") +print(sorted_arr) \ No newline at end of file