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

Experiment 5: Write A Program For Quick Sorting For A List or Array

The document presents a C program for implementing the quicksort algorithm on an array. It includes functions for displaying the array, swapping elements, partitioning the array, and the main function to handle user input and execute the sorting. The program prompts the user to enter the size and values of the array, then displays the array before and after sorting.

Uploaded by

bobbyaust4
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 views3 pages

Experiment 5: Write A Program For Quick Sorting For A List or Array

The document presents a C program for implementing the quicksort algorithm on an array. It includes functions for displaying the array, swapping elements, partitioning the array, and the main function to handle user input and execute the sorting. The program prompts the user to enter the size and values of the array, then displays the array before and after sorting.

Uploaded by

bobbyaust4
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/ 3

Name: Sameer Chauhan

Enrolled no: 23100BTCSAII14319

Experiment 5
AIM: Write a program for quick sorting for a list or array.
Code:
#include <stdio.h>

void show(int n, int array[])


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

void swap(int *a, int *b)


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

int partition(int array[], int low, int high)


{
int pivot = array[high];
int i = low - 1;

for (int j = low; j <= high - 1; j++)


{
(array[j] < pivot) ? swap(&array[++i], &array[j]) : 0;
}

swap(&array[i + 1], &array[high]);


return i + 1;
}

13
void quicksort(int array[], int low, int high)
{
if (low < high) {

int pi = partition(array, low, high);


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

int main()
{

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

int size = sizeof(array) / sizeof(array[0]);

printf("Befor quick sorting:");


show(size, array);
printf("\n");

quicksort(array, 0, size - 1);


printf("After quick sorting:");
show(size, array);
}

14
Name: Yogesh Singh Kulegi
Enrolled no: 23100BTCSAII14339

OUTPUT:

15

You might also like