0% found this document useful (0 votes)
6 views2 pages

Quick Sort

The document provides a C program that implements the Quick Sort algorithm to sort an array of integers. It includes functions for sorting and partitioning the array, as well as user input for the number of elements and the elements themselves. The output displays the sorted array after processing the input.

Uploaded by

deadshotlev
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
6 views2 pages

Quick Sort

The document provides a C program that implements the Quick Sort algorithm to sort an array of integers. It includes functions for sorting and partitioning the array, as well as user input for the number of elements and the elements themselves. The output displays the sorted array after processing the input.

Uploaded by

deadshotlev
Copyright
© © All Rights Reserved
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/ 2

QUICK SORT:-

#include<stdio.h>
void quick_sort(int a[], int low, int high) {
if (low < high) {
int pivot;
pivot = partition(a, low, high);
quick_sort(a, low, pivot - 1);
quick_sort(a, pivot + 1, high);
}
}

int partition(int a[], int low, int high) {


int pivot = a[low];
int i = low;
int j = high;
int temp;

while (i < j) {
while (i <= high && a[i] <= pivot) {
i++;
}
while (a[j] > pivot) {
j--;
}
if (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

temp = a[low];
a[low] = a[j];
a[j] = temp;

return j;
}

int main()
{
int a[10], n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

quick_sort(a, 0, n - 1);

printf("Sorted array: ");


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

return 0;
}
OUTPUT:

Enter the number of elements: 5


Enter the elements: 1
3
4
5
2
Sorted array: 1 2 3 4 5

You might also like