From 08970aace68de652509cd9cc66b18046cb762509 Mon Sep 17 00:00:00 2001 From: Heet Patel Date: Sun, 20 Oct 2024 15:05:58 +0530 Subject: [PATCH] Impleented Shell Sort Algorithm in Java --- Sorting/Shell_Sort.java | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 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 00000000..e05982fe --- /dev/null +++ b/Sorting/Shell_Sort.java @@ -0,0 +1,63 @@ +import java.util.Scanner; + +public class ShellSort { + // Function to perform Shell Sort + public static void shellSort(int[] arr) { + int n = arr.length; + + // Start with a large gap, then reduce the gap + for (int gap = n / 2; gap > 0; gap /= 2) { + // Perform gapped insertion sort for this gap size + for (int i = gap; i < n; i++) { + int temp = arr[i]; + int j; + + // Shift elements of the array to their correct position + for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { + arr[j] = arr[j - gap]; + } + + // Place the temp (current element) at its correct position + arr[j] = temp; + } + } + } + + // Helper function to print the array + public static void printArray(int[] arr) { + for (int num : arr) { + System.out.print(num + " "); + } + System.out.println(); + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Getting array size from user + System.out.print("Enter the number of elements: "); + int n = scanner.nextInt(); + + // Creating the array + int[] arr = new int[n]; + + // Getting array elements from user + System.out.println("Enter the elements:"); + for (int i = 0; i < n; i++) { + arr[i] = scanner.nextInt(); + } + + // Display the array before sorting + System.out.println("Array before sorting:"); + printArray(arr); + + // Perform Shell Sort + shellSort(arr); + + // Display the array after sorting + System.out.println("Array after sorting:"); + printArray(arr); + + scanner.close(); + } +}