0% found this document useful (0 votes)
5 views

Data Structure Programs using C Language (Unit-3)

The document provides implementations of various sorting algorithms in C, including Quick Sort, Insertion Sort, Selection Sort, Merge Sort, Bubble Sort, Heap Sort, and Radix Sort. Each algorithm is accompanied by a function to print the sorted array and a main function demonstrating its usage. The code snippets illustrate the logic and steps involved in each sorting technique.

Uploaded by

raw01sachin2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structure Programs using C Language (Unit-3)

The document provides implementations of various sorting algorithms in C, including Quick Sort, Insertion Sort, Selection Sort, Merge Sort, Bubble Sort, Heap Sort, and Radix Sort. Each algorithm is accompanied by a function to print the sorted array and a main function demonstrating its usage. The code snippets illustrate the logic and steps involved in each sorting technique.

Uploaded by

raw01sachin2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Structure Programs using C Language (Unit-3)

1. Quick Sort
#include <stdio.h>
// Function to swap two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Partition function to place pivot element at the correct position
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choose the last element as pivot
int i = low - 1; // Index of smaller element

for (int j = low; j < high; j++) {


// If current element is smaller than pivot
if (arr[j] < pivot) {
i++; // Increment index of smaller element
swap(&arr[i], &arr[j]); // Swap elements
}
}
swap(&arr[i + 1], &arr[high]); // Swap pivot to correct position
return i + 1; // Return the partition index
}
// Recursive Quick Sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high); // Get partition index
quickSort(arr, low, pi - 1); // Sort elements before partition
quickSort(arr, pi + 1, high); // Sort elements after partition
}
}
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1); // Call Quick Sort
printf("Sorted array: ");
printArray(arr, n); // Print sorted array
return 0;
}
OUTPUT:-

2. Insertion Sort

#include <stdio.h>

// Insertion Sort function


void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i]; // Store the current element
int j = i - 1;

// Shift elements that are greater than key to the right


while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key; // Place key in its correct position
}
}

// Function to print the array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n); // Call Insertion Sort
printf("Sorted array: ");
printArray(arr, n); // Print sorted array
return 0;
}

OUTPUT:-

3. Selection Sort

#include <stdio.h>

// Selection Sort function


void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i; // Assume the first element is the minimum

// Find the minimum element in the unsorted part


for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}

// Swap the found minimum element with the first element


int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

// Function to print the array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n); // Call Selection Sort
printf("Sorted array: ");
printArray(arr, n); // Print sorted array
return 0;
}

OUTPUT:-

4. Merge Sort

#include <stdio.h>

// Function to merge two halves


void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1; // Size of left half
int n2 = r - m; // Size of right half
int L[n1], R[n2];

// Copy data to temporary arrays


for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

int i = 0, j = 0, k = l;

// Merge the arrays back into arr[]


while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}

// Copy remaining elements of L[], if any


while (i < n1)
arr[k++] = L[i++];

// Copy remaining elements of R[], if any


while (j < n2)
arr[k++] = R[j++];
}

// Recursive Merge Sort function


void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2; // Find the middle point
mergeSort(arr, l, m); // Sort first half
mergeSort(arr, m + 1, r); // Sort second half
merge(arr, l, m, r); // Merge the sorted halves
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1); // Call Merge Sort
printf("Sorted array: ");
printArray(arr, n); // Print sorted array
return 0;
}
OUTPUT:-

5. Bubble Sort

#include <stdio.h>

// Bubble Sort function


void bubbleSort(int arr[], int n) {
// Loop through the entire array
for (int i = 0; i < n - 1; i++) {
// Last i elements are already sorted, so ignore them
for (int j = 0; j < n - i - 1; j++) {
// Swap if the element is greater than the next element
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

// Function to print the array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n); // Call Bubble Sort
printf("Sorted array: ");
printArray(arr, n); // Print sorted array
return 0;
}

OUTPUT:-

6. 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
int right = 2 * i + 2; // Right child index

// Check if left child is larger than root


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

// Check if right child is larger than the largest so far


if (right < n && arr[right] > arr[largest])
largest = right;
// If largest is not root, swap and continue heapifying
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}

// Heap Sort function


void heapSort(int arr[], int n) {
// Build a max 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--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

// Call heapify on the reduced heap


heapify(arr, i, 0);
}
}

// Function to print the array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n); // Call Heap Sort
printf("Sorted array: ");
printArray(arr, n); // Print sorted array
return 0;
}

OUTPUT:-
7. Radix Sort

#include <stdio.h>

// Function to get the maximum value in the array


int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}

// Counting sort function used by Radix Sort


void countingSort(int arr[], int n, int exp) {
int output[n]; // Output array
int count[10] = {0}; // Count array to store digit counts

// Store count of occurrences in count[]


for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;

// Change count[i] so it contains the actual position of this digit in output[]


for (int i = 1; i < 10; i++)
count[i] += count[i - 1];

// Build the output array


for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy the output array to arr[], so that arr[] contains sorted numbers
for (int i = 0; i < n; i++)
arr[i] = output[i];
}

// Radix Sort function


void radixSort(int arr[], int n) {
int max = getMax(arr, n); // Find the maximum number to know the number of digits

// Do counting sort for every digit. The exp is 10^i where i is the current digit number
for (int exp = 1; max / exp > 0; exp *= 10)
countingSort(arr, n, exp);
}

// Function to print the array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
radixSort(arr, n); // Call Radix Sort
printf("Sorted array: ");
printArray(arr, n); // Print sorted array
return 0;
}
OUTPUT:-

You might also like