0% found this document useful (0 votes)
3 views

Quick Sort

ADA

Uploaded by

Dushyanth ms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Quick Sort

ADA

Uploaded by

Dushyanth ms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

#include <stdlib.h>

#include <time.h>

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pivot = arr[high];

int i = low - 1;

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

if (arr[j] < pivot) {

i++;

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

quickSort(arr, low, i);

quickSort(arr, i + 2, high);

void main() {

int arr[50], n, i;
clock_t start, end;

double cpu_time_used;

printf("Enter number of elements: ");

scanf("%d", &n);

srand(time(NULL));

for (i = 0; i < n; i++) {

arr[i] = rand() % 100 + 1;

printf("Original array: ");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

start = clock();

quickSort(arr, 0, n - 1);

end = clock();

cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("Sorted array: ");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

printf("Time taken for quick sort: %f seconds\n", cpu_time_used);

You might also like