The document provides implementations of three sorting algorithms: Quick Sort, Merge Sort, and Heap Sort in C. Each algorithm includes functions for sorting an array of integers in ascending order, along with a main function to test the sorting methods by taking user input. The implementations include detailed comments explaining the functionality of each part of the code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
6
The document provides implementations of three sorting algorithms: Quick Sort, Merge Sort, and Heap Sort in C. Each algorithm includes functions for sorting an array of integers in ascending order, along with a main function to test the sorting methods by taking user input. The implementations include detailed comments explaining the functionality of each part of the code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6
Write a program that implements the following sorting methods to sort a given list
of integers in ascending order i) Quick sort
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) { int temp; temp = *a; *a = *b; *b = temp; }
// Function to partition the array and return the pivot index
int partition(int arr[], int low, int high) { int pivot; int i; int j;
pivot = arr[high]; // Choose the last element as the pivot
i = low - 1; // Index of smaller element
for (j = low; j < high; j++) {
// If the current element is smaller than the pivot if (arr[j] < pivot) { i++; // Move the smaller element index swap(&arr[i], &arr[j]); // Swap arr[i] and arr[j] } }
// Swap arr[i+1] with the pivot element
swap(&arr[i + 1], &arr[high]);
return i + 1; // Return the partition index
}
// Function to perform quicksort
void quickSort(int arr[], int low, int high) { int pi; // Declare partition index variable
if (low < high) {
// Get the partition index pi = partition(arr, low, high);
// Recursively sort the elements before and after partition
quickSort(arr, low, pi - 1); // Sort left of pivot quickSort(arr, pi + 1, high); // Sort right of pivot } }
// Main function to test the quicksort algorithm
int main() { int n; // Number of elements in the array int i; // Loop variable for iteration
// Ask user for the number of elements
printf("Enter the number of elements: "); scanf("%d", &n);
int arr[n]; // Array declaration with user-defined size
// Input the array elements from the user
printf("Enter the elements of the array:\n"); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); }
// Print the original array
printf("Original Array: \n"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n");
// Perform quicksort on the entire array
quickSort(arr, 0, n - 1);
// Print the sorted array
printf("Sorted Array: \n"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n");
return 0; }
iii) Merge sort
#include <stdio.h>
// Function to merge two halves of the array
void merge(int arr[], int left, int mid, int right) { int i, j, k; int n1 = mid - left + 1; // Size of the first half int n2 = right - mid; // Size of the second half
// Temporary arrays to hold the two halves
int L[n1], R[n2]; // Copy data to the temporary arrays for (i = 0; i < n1; i++) { L[i] = arr[left + i]; } for (j = 0; j < n2; j++) { R[j] = arr[mid + 1 + j]; }
// Merge the two halves back into arr[]
i = 0; // Initial index of the first half j = 0; // Initial index of the second half k = left; // Initial index of the merged array
printf("Enter the number of elements: "); scanf("%d", &n);
int arr[n]; // Array declaration with user-defined size
// Input the array elements from the user
printf("Enter the elements of the array:\n"); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); }
// Print the original array
printf("Original Array: \n"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n");
// Perform merge sort on the entire array
mergeSort(arr, 0, n - 1);
// Print the sorted array
printf("Sorted Array: \n"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n");
return 0; }
ii) Heap sort
#include <stdio.h>
// Function to heapify a subtree rooted at node i
void heapify(int arr[], int n, int i) { int largest = i; // Initialize largest as root int left = 2 * i + 1; // Left child index (2*i + 1) int right = 2 * i + 2; // Right child index (2*i + 2) int temp;
// If the left child is larger than the root
if (left < n && arr[left] > arr[largest]) { largest = left; }
// If the right child is larger than the current largest
if (right < n && arr[right] > arr[largest]) { largest = right; } // If the largest is not the root if (largest != i) { // Swap the root with the largest temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp;
// Recursively heapify the affected subtree
heapify(arr, n, largest); } }
// Function to perform heap sort
void heapSort(int arr[], int n) { int i, temp;
// Build a max heap (rearrange the array)
for (i = n / 2 - 1; i >= 0; i--) { heapify(arr, n, i); }
// Extract elements one by one from the heap
for (i = n - 1; i >= 0; i--) { // Move the current root (largest) to the end temp = arr[0]; arr[0] = arr[i]; arr[i] = temp;
// Call heapify on the reduced heap
heapify(arr, i, 0); } }
// Main function to test the heap sort algorithm
int main() { int n, i;
// Ask user for the number of elements
printf("Enter the number of elements: "); scanf("%d", &n);
int arr[n]; // Array declaration with user-defined size
// Input the array elements from the user
printf("Enter the elements of the array:\n"); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); }
// Print the original array
printf("Original Array: \n"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n");
// Perform heap sort on the entire array
heapSort(arr, n); // Print the sorted array printf("Sorted Array: \n"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n");