Program
Time Complexity: O(n2)
#include <stdio.h> #include <conio.h> #include <time.h> void Exch(int *p, int *q) { int temp = *p; *p = *q; *q = temp; } void QuickSort(int a[], int low, int high) { int i, j, key, k; if (low >= high) return; key = low; i = low + 1; j = high; while (i <= j) { while (a[i] <= a[key]) i = i + 1; while (a[j] > a[key]) j = j - 1; if (i < j) Exch(&a[i], &a[j]); } Exch(&a[j], &a[key]); QuickSort(a, low, j - 1); QuickSort(a, j + 1, high); } void main() { int n, a[1000], i; clock_t st, et; double ts; printf("*********************************************************"); printf("\n*********************************************************"); printf("\n** WAP for quick sort with time complexity in C **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n*********************************************************"); printf("\n*********************************************************"); printf("\n Enter How many Numbers: "); scanf("%d", &n); printf("\nEnter array elements\n"); for (i = 0; i < n; i++) scanf("%d", &a[i]); st = clock(); QuickSort(a, 0, n - 1); et = clock(); ts = (double)(et - st) / CLOCKS_PER_SEC; printf("\nSorted Numbers are: \n"); for (i = 0; i < n; i++) printf("%d\n", a[i]); printf("\nThe time taken is %e", ts); }
Output 1
********************************************************* ********************************************************* ** WAP for quick sort with time complexity in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** ********************************************************* ********************************************************* Enter How many Numbers: 8 Enter array elements 23 32 21 14 21 23 43 31 Sorted Numbers are: 14 21 21 23 23 31 32 43 The time taken is 0.000000e+000