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

Arun Exp-5

experiment 8 of DAA 5th sem in Chandigarh University

Uploaded by

kumararunlamba89
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)
10 views3 pages

Arun Exp-5

experiment 8 of DAA 5th sem in Chandigarh University

Uploaded by

kumararunlamba89
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment - 5
Student Name: Arun UID: 22BET10320
Branch: BE-IT Section/Group: 22BET_IOT_703/B
Semester: 5th Date of Performance: 13/08/2024
Subject Name: DAA Subject Code: 22ITH-311

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. The elements can be read from a file or can
be generated using the random number generator.

2. Objective: Implement Quick sort algorithm to sort elements, measure time complexity,
and analyze performance with varying input sizes (n) using file input or random
number generation. Repeat experiment to understand algorithm's efficiency, scalability,
and identify potential areas for optimization, providing insights into its practical
applications and limitations.

3. Implementation/Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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


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

int partition(int arr[], int low, int high) {


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]);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));


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

clock_t start_time = clock();


quickSort(arr, 0, n - 1);
clock_t end_time = clock();

printf("Sorted array:\n");
printArray(arr, n);
printf("Time taken to sort: %f seconds\n", (double)(end_time - start_time) /
CLOCKS_PER_SEC);

free(arr);
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Output:

5. Time Complexity:
quickSort: O(n log n), partition: O(n), swap: O(1), printArray: O(n), main: O(n)
Overall time complexity: O(n log n)

6. Learning Outcomes:
• Understanding of Quicksort Algorithm: The code demonstrates a clear understanding
of the quicksort algorithm, including the partition scheme, recursive function calls,
and the swapping of elements to sort the array.
• Implementation of Dynamic Memory Allocation: The code showcases the use of
dynamic memory allocation using malloc to allocate memory for the array, and free to
deallocate the memory when it's no longer needed.
• User Input and Output Handling: The code demonstrates how to handle user input and
output, including reading input from the user, storing it in an array, and printing the
sorted array to the console.

You might also like