image

Heap Sort in C: With Simple Functions, Strings, and Variable Memory

Heap Sort is an efficient sorting algorithm that creates a binary heap data structure from the input array and then repeatedly extracts the maximum or minimum element from the heap. It has a time complexity of O(n log n), making it efficient for sorting large datasets. In this article, we'll explore the implementation of Heap Sort in C, covering simple functions, sorting strings, and handling variable memory allocation.

Understanding Heap Sort

Before diving into the implementation, let's briefly understand the concept of Heap Sort. A binary heap is a tree-based data structure that satisfies the heap property: for every node, the value of that node is greater than or equal to (in the case of a max-heap) or less than or equal to (in the case of a min-heap) the values of its children.

The Heap Sort algorithm consists of two main phases:

  1. Heapify: In this phase, the input array is transformed into a binary heap data structure.
  2. Heap Sort: After the heap is built, the root node (which is the maximum or minimum element) is repeatedly swapped with the last element of the heap, and the heap is reduced by one element. The process continues until the entire array is sorted.

Implementing Heap Sort in C

Here's an implementation of Heap Sort in C with simple functions for sorting integers and strings, as well as handling variable memory allocation:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Function to swap two integers

void swap(int *a, int *b) {

    int temp = *a;

    *a = *b;

    *b = temp;

}

// Function to heapify the given array

void heapify(int arr[], int n, int i) {

    int largest = i;

    int left = 2 * i + 1;

    int right = 2 * i + 2;

    if (left < n && arr[left] > arr[largest])

        largest = left;

    if (right < n && arr[right] > arr[largest])

        largest = right;

    if (largest != i) {

        swap(&arr[i], &arr[largest]);

        heapify(arr, n, largest);

    }

}

// Function to sort an integer array using Heap Sort

void heapSort(int arr[], int n) {

    // Build the heap

    for (int i = n / 2 - 1; i >= 0; i--)

        heapify(arr, n, i);

    // Extract elements from the heap one by one

    for (int i = n - 1; i >= 0; i--) {

        swap(&arr[0], &arr[i]);

        heapify(arr, i, 0);

    }

}

// Function to sort a string array using Heap Sort

void heapSortStrings(char **strings, int n) {

    // Build the heap

    for (int i = n / 2 - 1; i >= 0; i--) {

        int largest = i;

        int left = 2 * i + 1;

        int right = 2 * i + 2;

        if (left < n && strcmp(strings[left], strings[largest]) > 0)

            largest = left;

        if (right < n && strcmp(strings[right], strings[largest]) > 0)

            largest = right;

        if (largest != i) {

            char *temp = strings[i];

            strings[i] = strings[largest];

            strings[largest] = temp;

            heapify(strings, n, largest);

        }

    }

 

    // Extract elements from the heap one by one

    for (int i = n - 1; i >= 0; i--) {

        char *temp = strings[0];

        strings[0] = strings[i];

        strings[i] = temp;

        heapify(strings, i, 0);

    }

}

int main() {

    int n;

    printf("Enter the number of integers: ");

    scanf("%d", &n);

    int *arr = (int *)malloc(n * sizeof(int));

    printf("Enter %d integers:\n", n);

    for (int i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

    heapSort(arr, n);

    printf("Sorted integers: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

    free(arr);

    int m;

    printf("\nEnter the number of strings: ");

    scanf("%d", &m);

    char **strings = (char **)malloc(m * sizeof(char *));

    for (int i = 0; i < m; i++) {

        char buffer[100];

        printf("Enter string %d: ", i + 1);

        scanf("%s", buffer);

        strings[i] = (char *)malloc((strlen(buffer) + 1) * sizeof(char));

        strcpy(strings[i], buffer);

    }

    heapSortStrings(strings, m);

    printf("Sorted strings: ");

    for (int i = 0; i < m; i++) {

        printf("%s ", strings[i]);

        free(strings[i]);

    }

    printf("\n");

    free(strings);

    return 0;

}

Here's how the code works:

  1. The swap function is a helper function that swaps two integer values.
  2. The heapify function is responsible for building and maintaining the max-heap property for the given array.
  3. The heapSort function sorts an integer array using Heap Sort. It first builds the max-heap using heapify and then repeatedly extracts the maximum element from the heap and places it at the end of the sorted subarray.
  4. The heapSortStrings function sorts an array of strings using Heap Sort. It works similarly to heapSort, but it compares strings using the strcmp function.
  5. In the main function, the user is prompted to enter the number of integers and strings, respectively.
  6. The integer array is dynamically allocated using malloc, and the user is prompted to enter the integers.
  7. The heapSort function is called to sort the integer array.
  8. The sorted integers are printed, and the memory allocated for the integer array is freed using free.
  9. The string array is dynamically allocated using malloc, and the user is prompted to enter the strings.
  10. The heapSortStrings function is called to sort the string array.
  11. The sorted strings are printed, and the memory allocated for each string and the string array itself is freed using free.

FAQs

1. What is the time complexity of Heap Sort?

The time complexity of Heap Sort is O(n log n) in the average and worst cases, where n is the number of elements to be sorted. This time complexity makes Heap Sort an efficient algorithm for sorting large datasets.

2. Why is Heap Sort preferred over other sorting algorithms in certain scenarios?

Heap Sort has several advantages over other sorting algorithms:

  • It has a time complexity of O(n log n), which is better than the O(n^2) complexity of algorithms like Bubble Sort and Insertion Sort for large datasets.
  • It is an in-place sorting algorithm, meaning it does not require extra memory proportional to the size of the input array.
  • It is not recursive, which can be advantageous in certain scenarios where function call overhead needs to be minimized.

3. How does Heap Sort handle duplicates?

Heap Sort can handle duplicate values in the input array without any issues. The order of duplicate elements in the sorted output may vary, but the algorithm will correctly sort all elements, including duplicates.

4. Can Heap Sort be used to sort other data types besides integers and strings?

Yes, Heap Sort can sort other data types, such as floating-point numbers, custom data structures, or objects. However, you may need to provide a custom comparison function or modify the implementation to handle the specific data type.

5. What is the advantage of using dynamic memory allocation in the provided implementation?

The provided implementation uses dynamic memory allocation (with malloc and free) to handle variable-length input arrays. This approach allows the program

Share On