Menu

Data Structures [ Lab Programs ]


C program that implements Shell sorting methods to sort a given list of integers in ascending order

Shell sorting methods
#include <stdio.h>

// Function to perform Shell Sort
void shellSort(int arr[], int n) {
    // Start with a big gap, then reduce the gap
    for (int gap = n / 2; gap > 0; gap /= 2) {
        
        // Perform a gapped insertion sort
        for (int i = gap; i < n; i++) {
            int temp = arr[i];
            int j;

            // Shift earlier gap-sorted elements up
            for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
                arr[j] = arr[j - gap];
            }

            // Put temp in its correct location
            arr[j] = temp;
        }
    }
}

int main() {
    int n,arr[20],i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    shellSort(arr, n);

    printf("Sorted array: ");
    for(i=0;i<n;i++)
    printf(" %d",arr[i]);

    return 0;
}


int main() {
    int n,arr[20],i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    heapsort(arr, n);

    printf("Sorted array: ");
    for(i=0;i<n;i++)
    printf(" %d",arr[i]);

    return 0;
}

OUTPUT
Enter number of elements: 9
Enter 9 integers:
7 4 1 8 5 2 9 6 3
Sorted array: 1 2 3 4 5 6 7 8 9 

Related Content :

1. Write a program that uses functions to perform the following operations on singly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

2. Write a program that uses functions to perform the following operations on doubly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

3. Write a program that uses functions to perform the following operations on circular linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

4. Write a program that implement Stack (its operations) using Array    View Solution

5. Write a program that implement Stack (its operations) using Linked List (Pointer)    View Solution

6. Write a program that implement Queue(its operations) using Array    View Solution

7. Write a program that implement Queue (its operations) using Linked List (Pointer)    View Solution

8. Write a program that implements Radix sorting methods to sort a given list of integers in ascending order    View Solution

9. Write a program that implements Heap sorting methods to sort a given list of integers in ascending order    View Solution

10. Write a program that implements Shell sorting methods to sort a given list of integers in ascending order    View Solution

11. Write a program that implements Tree sorting methods to sort a given list of integers in ascending order    View Solution

12. Write a program to implement the tree traversal methods using Recursive    View Solution

13. Write a program to implement the tree traversal methods using Non Recursive    View Solution

14. Write a program to implement Binary Search Tree (its operations)    View Solution

15. Write a program to implement AVL Tree (its operations)    View Solution

16. Write a program to implement Red - Black Tree (its operations)    View Solution

17. Write a program to implement the graph traversal methods (Breadth First Search)    View Solution

18.Write a program to implement the graph traversal methods (Depth First Search)    View Solution

19. Write a program to implement the following Hash Functions: i) Division Method, ii) Multiplication Method, iii) Mid-square Method, iv) Folding Method    View Solution