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

Quick Sort

The document contains a C program that implements the QuickSort algorithm to sort an array of integers. It includes functions for swapping elements, partitioning the array, performing the QuickSort, and printing the array. The main function handles user input for the number of elements and the elements themselves, then displays the original and sorted arrays.

Uploaded by

ektapagare6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Quick Sort

The document contains a C program that implements the QuickSort algorithm to sort an array of integers. It includes functions for swapping elements, partitioning the array, performing the QuickSort, and printing the array. The main function handles user input for the number of elements and the elements themselves, then displays the original and sorted arrays.

Uploaded by

ektapagare6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Source Code:

#include <stdio.h>
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original array: ");


printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}

Output:

You might also like