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

DSA Lab 3

The document describes the QuickSort algorithm through a flowchart and C source code. It includes a flowchart showing the steps of QuickSort, source code implementing the QuickSort algorithm on an integer array with functions for partitioning the array and recursively sorting subarrays, and sample output showing an unsorted array being sorted from largest to smallest.

Uploaded by

Asif Uddin
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)
47 views3 pages

DSA Lab 3

The document describes the QuickSort algorithm through a flowchart and C source code. It includes a flowchart showing the steps of QuickSort, source code implementing the QuickSort algorithm on an integer array with functions for partitioning the array and recursively sorting subarrays, and sample output showing an unsorted array being sorted from largest to smallest.

Uploaded by

Asif Uddin
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

Flowchart:

Figure 1: Flowchart of QuickSort Algorithm


Source Code:
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);

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


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quick_sort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);

quick_sort(arr, low, pi - 1);


quick_sort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {8, 3, 1, 6, 5, 2, 7, 4};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


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

quick_sort(arr, 0, n - 1);

printf("\nSorted array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Original array: 8 3 1 6 5 2 7 4
Sorted array: 1 2 3 4 5 6 7 8
Process returned 0 (0x0) execution time : 5.855 s
Press any key to continue.

You might also like