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

Quick

The document describes a quicksort algorithm for sorting an array of integers. It includes functions for swapping elements, partitioning the array around a pivot, and recursively sorting parts of the array. The main function gets input from the user, calls quicksort, and prints the sorted output.

Uploaded by

smit.karelia
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)
8 views2 pages

Quick

The document describes a quicksort algorithm for sorting an array of integers. It includes functions for swapping elements, partitioning the array around a pivot, and recursively sorting parts of the array. The main function gets input from the user, calls quicksort, and prints the sorted output.

Uploaded by

smit.karelia
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

#include <stdio.

h>

void swap(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) //Partitions the array and returns
pivot
{
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); //partitioning index

//sort elements before and after partitioning


quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}

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

quicksort(arr, 0, n - 1);

printf("Quick Sort\n");
printf("Sorted array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

You might also like