Daa Practical File Prabhjot
Daa Practical File Prabhjot
OF
DESIGN ANALYSIS AND ALGORITHM
(Session : 2021-2025)
SUBMITTED TO SUBMITTED BY
Mrs. Sonia Saini Prabhjot Singh
CSE dept. 252102107
UIET,KUK CSE-B
4th Sem.
PROGRAM 1
AIM: Sort a given set of elements using the Quick sort method and determine
the time required to sort the elements. Repeat the experiment for different
values of n, the number of elements in the list to be sorted and plot a graph of
the time taken versus n. The elements can be read from a file or can be
generated using the random number generator.
SOURCE CODE:
#include <iostream>
using namespace std;
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void printArray(int array[], int size) {
int i;
for (i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
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);
}
}
int main() {
int data[] = {8, 7, 6, 1, 0, 9, 2};
int n = sizeof(data) / sizeof(data[0]);