0% found this document useful (0 votes)
2 views3 pages

Quick Sort

The document outlines the Quick Sort algorithm, detailing the steps to read an array of elements, sort them using the quickSort function, and display the sorted array. It includes the implementation of the partition function to determine the pivot and recursively sort the subarrays. The provided C program demonstrates the complete process of sorting an array using Quick Sort.

Uploaded by

issackjoby8
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)
2 views3 pages

Quick Sort

The document outlines the Quick Sort algorithm, detailing the steps to read an array of elements, sort them using the quickSort function, and display the sorted array. It includes the implementation of the partition function to determine the pivot and recursively sort the subarrays. The provided C program demonstrates the complete process of sorting an array using Quick Sort.

Uploaded by

issackjoby8
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

Quick Sort

ALGORITHM: Quick Sort

Step 1: Start

Step 2:Read the number of elements n.

Step 3:Read n elements into the array arr[].

Step 4:Call the function quickSort(arr, 0, n - 1) to sort the array.

Step 5:Function: quickSort(arr, low, high)

1.​ If low < high, then​


a) Call the function partition(arr, low, high) to find the pivot position pi.​
b) Recursively call quickSort(arr, low, pi - 1) to sort the left subarray.​
c) Recursively call quickSort(arr, pi + 1, high) to sort the right subarray.​

Step 6:Function: partition(arr, low, high)

1.​ Select the last element of the array as the pivot.​

2.​ Initialize i = low - 1.​

3.​ For j = low to high - 1 do​

If arr[j] < pivot,​

-Increment i.​
-Swap arr[i] and arr[j].​
4.​ After the loop, swap arr[i + 1] and arr[high] (place pivot in correct position).​

5.​ Return (i + 1) as the partition index.​

Step 7:After all recursive calls are completed, the array will be sorted in ascending order.

Step 8:Display the sorted array.

Step 9:Stop.

PROGRAM

#include <stdio.h>

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


int temp = *a;
*a = *b;
*b = 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], &arr[j]);
}
}
swap(&arr[i + 1], &arr[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 display(int arr[], int n) {


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

int main() {
int n, arr[100];

printf("Enter number of elements: ");


scanf("%d", &n);

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


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

printf("\nBefore Sorting: ");


display(arr, n);

quickSort(arr, 0, n - 1);

printf("After Quick Sort: ");


display(arr, n);

return 0;
}

You might also like