Quick Sort C Program- Sorting Algorithms

quicksort cprogram

Quick sort is also a divide and conquer algorithm like merge sort.

In this algorithm we choose a pivot element and divide the array based on the selected pivot element.

Quick sort have a Best case Time complexity Ω(n log(n)) and worst case time complexity O(n^2)

#include <stdio.h>

enum { SIZE = 17};

void quicksort(int *target, int left, int right) {
  if(left >= right) return;
  int i = left, j = right;
  int tmp, pivot = target[i];
  for(;;) {
    while(target[i] < pivot) i++;
    while(pivot < target[j]) j--;
    if(i >= j) break;
    tmp = target[i]; target[i] = target[j]; target[j] = tmp;
    i++; j--;
  quicksort(target, left, i-1);
  quicksort(target, j+1, right);

int main() {
  int i, array[SIZE] = {3,1,65,393,2,5,8,22,9,6,2353,52,75,212,4,34,1342};

  quicksort(array, 0, SIZE-1);

  for(i=0; i<SIZE; i++)
    printf("%d ", array[i]);

credits: Github

Similar Posts:


You May Also Like

About the Author: shakirck

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.