ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-213Shell Sort
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-213

    Data Structures & Algorithms

    Progress0 / 37 done
    Topics37
    1Abstract Data Types2Complexity Analysis3Big Oh Notation4Stacks (Linked Lists and Array Implementations)5Recursion and analyzing recursive algorithms6Divide and Conquer Algorithms7Sorting Algorithms8Selection Sort9Insertion Sort10Merge Sort11Quick Sort12Bubble Sort13Heap Sort14Shell Sort15Radix Sort16Bucket Sort17Queue18Dequeuer19Priority Queues (linked and array implementations of queues)20Linked List and Its Various Types21Sorted Linked List22Searching an Unsorted Array23Binary Search for Sorted Arrays24Hashing and Indexing25Open Addressing and Chaining26Trees and Tree Traversals27Binary Search Trees28Heaps29M-Way Trees30Balanced Trees31Graphs32Breadth-First Traversal33Depth-First Traversal34Topological Order35Shortest Path36Adjacency Matrix and Adjacency List Implementations37Memory Management and Garbage Collection
    4 min read
    Topic 14 of 37
    Topic Study Note Illustration
    14

    Shell Sort

    Description : Shell sort is an optimization of insertion sort that allows the exchange of items that are far apart. It uses a gap sequence to determine...

    Shell Sort

    Description: Shell sort is an optimization of insertion sort that allows the exchange of items that are far apart. It uses a gap sequence to determine which elements to compare and swap. The idea is to allow the elements to move more quickly to their correct positions, effectively reducing the overall sorting time.

    How It Works

    1. Choose Gaps: Start with a large gap and reduce it over time. The first gap might be half the size of the array, then a quarter, and so on.
    2. Sort Subarrays: For each gap, perform a gapped insertion sort. This means that you take elements that are a specific distance apart and sort them as if they are in a smaller array.
    3. Repeat: Continue this process until the gap is reduced to 1. A final insertion sort will finish the sorting.

    Time Complexity

    • Worst Case: O(n3/2)O(n^{3/2})O(n3/2) — depends on the gap sequence used.
    • Average Case: O(nlog⁡n)O(n \log n)O(nlogn) — with an optimal gap sequence.
    • Best Case: O(n)O(n)O(n) — occurs when the array is already sorted.

    Space Complexity

    • Space Complexity: O(1)O(1)O(1) — Shell sort is an in-place sorting algorithm, requiring a constant amount of additional space.

    Stability

    • Stability: Shell sort is not a stable sorting algorithm. Equal elements may not maintain their relative order after sorting.

    Example Code

    Here's a C++ implementation of shell sort:

    #include <iostream>
    using namespace std;
    
    void shellSort(int arr[], 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
            for (int i = gap; i < n; i++) {
                int temp = arr[i]; // Element to be positioned
                int j; 
    
                // Shift earlier gap-sorted elements up until the correct location for arr[i] is found
                for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
                    arr[j] = arr[j - gap];
                }
                arr[j] = temp; // Put temp (the original arr[i]) in its correct location
            }
        }
    }
    
    int main() {
        int arr[] = {12, 34, 54, 2, 3};
        int n = sizeof(arr) / sizeof(arr[0]);
        shellSort(arr, n);
    
        cout << "Sorted array: ";
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    Explanation of the Code

    1. Function Definition: The shellSort function takes an array and its size as parameters.
    2. Gap Selection: The outer loop starts with a gap of half the size of the array and continues halving the gap until it becomes zero.
    3. Gapped Insertion Sort:
      • The inner loop performs a gapped insertion sort by comparing elements that are gap distance apart.
      • Elements are shifted up to make space for the current element (temp) to be placed in its correct position.
    4. Output: After sorting, the program prints the sorted array.

    Conclusion

    Shell sort improves upon insertion sort by allowing the exchange of distant elements, thus reducing the number of comparisons and movements needed as the gaps decrease. While not as efficient as more advanced algorithms like quick sort or merge sort for large datasets, it is simple to implement and can be very effective for smaller lists. Its in-place nature and lower overhead make it a useful choice for certain applications where memory usage is a concern.

    ← Previous topic 13
    Heap Sort
    Next topic 15 →
    Radix Sort

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    636words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Data Structures & Algorithms