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

LCS2022052 DAA Lab03

The document describes an implementation of the quicksort algorithm in C code. It includes functions for partitioning the array, recursively sorting subarrays, swapping elements, and printing the array. The main function takes user input for the array size and elements, calls quicksort to sort the array, and prints the unsorted and sorted arrays.

Uploaded by

Sonu
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)
21 views2 pages

LCS2022052 DAA Lab03

The document describes an implementation of the quicksort algorithm in C code. It includes functions for partitioning the array, recursively sorting subarrays, swapping elements, and printing the array. The main function takes user input for the array size and elements, calls quicksort to sort the array, and prints the unsorted and sorted arrays.

Uploaded by

Sonu
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/ 2

Umesh Singh Verma (LCS2022052)

Design Analysis of Algorithm – Lab 03 Topic – QUICK SORT ALGORITHM

Code:
#include <stdio.h>

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; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}

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

return (i + 1);
}

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);
}
}

void printArray(int array[], int size) {


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

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

int n = sizeof(data) / sizeof(data[0]);

printf("Unsorted Array\n");
printArray(data, n);

quickSort(data, 0, n - 1);

printf("Sorted array in ascending order: \n");


printArray(data, n);
}

Screen shot of the output:

You might also like