[go: up one dir, main page]

0% found this document useful (0 votes)
7 views1 page

Quick Sort

The document contains a C program that implements the quicksort algorithm to sort an array of integers. It prompts the user to input the number of elements and the elements themselves, then sorts the array using quicksort and prints the sorted elements. The quicksort function recursively partitions the array and sorts the elements in place.

Uploaded by

prakash801997
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)
7 views1 page

Quick Sort

The document contains a C program that implements the quicksort algorithm to sort an array of integers. It prompts the user to input the number of elements and the elements themselves, then sorts the array using quicksort and prints the sorted elements. The quicksort function recursively partitions the array and sorts the elements in place.

Uploaded by

prakash801997
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/ 1

#include <stdio.

h>
void quicksort(int A[], int lower, int upper)
{
int i, j, temp;
if (lower >= upper)
return;
i = lower + 1;
j = upper;
while (i <= j)
{
while (i <= upper && A[i] < A[lower])
i = i + 1;
while (j >= lower && A[j] > A[lower])
j = j - 1;
if (i < j)
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[j];
A[j] = A[lower];
A[lower] = temp;
quicksort(A, lower, j - 1);
quicksort(A, j + 1, upper);
}
int main()
{
int n, k;
printf("Enter number of elements: ");
scanf("%d", &n);
int A[n];
printf("Enter elements:\n");
for (k = 0; k < n; k++)
scanf("%d", &A[k]);
quicksort(A, 0, n - 1);
printf("Sorted elements:\n");
for (k = 0; k < n; k++)
printf("%d ", A[k]);
return 0;
}

You might also like