logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/sorts/quickSort.c

/*

A demonstration of Quicksort

This is one of the fastest Sorting algorithms

The steps for in-place quicksort are as follows:

1. If the range has less than two elements, return immediately 
   as there is nothing to do. Possibly for other very short lengths 
   a special-purpose sorting method is applied and the remainder of 
   these steps skipped.
    
2. Otherwise pick a value, called a pivot, that occurs in the range 
   (the precise manner of choosing depends on the partition routine, 
   and can involve randomness).

3. Partition the range: reorder its elements, while determining a point 
   of division, so that all elements with values less than the pivot come 
   before the division, while all elements with values greater than the pivot 
   come after it; elements that are equal to the pivot can go either way. Since 
   at least one instance of the pivot is present, most partition routines ensure 
   that the value that ends up at the point of division is equal to the pivot, and 
   is now in its final position (but termination of quicksort does not depend on this,
   as long as sub-ranges strictly smaller than the original are produced).
    
4. Recursively apply the quicksort to the sub-range up to the point of division and to 
   the sub-range after it, possibly excluding from both ranges the element equal to the 
   pivot at the point of division. (If the partition produces a possibly larger sub-range 
   near the boundary where all elements are known to be equal to the pivot, these can be 
   excluded as well.)


this is just a demonstration file, where you input a number,
and it'll create an array with that many elements, and also
generate that number of random numbers.



*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 1048576

void swap(int *x, int *y);
void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);  

int main(int argc, char *argv[]){

	if(argc < 2){
		fprintf(stderr, "Usage: %s <number>\n", argv[0]);
		exit(1);
	}

	int sz = atoi(argv[1]);

	if(sz > MAX || sz < 1){
		fprintf(stderr, "Error: This value is not allowed\n");
		exit(1);
	}
  
	int arr[sz];
	time_t t;

   /* Intializes random number generator */
	srand((unsigned) time(&t)); //seeding it to the computers clock	

	for(int i = 0; i < sz; i++){
		arr[i] = rand() % sz; //generating an array of random numbers
	}

	quickSort(arr, 0, sz-1);

	
	return 0;
}

int partition(int arr[], int low, int high) {
  
  // select the rightmost element as pivot
  int pivot = arr[high];
  
  // pointer for greater element
  int i = (low - 1);

  // traverse each element of the array
  // compare them with the pivot
  for (int j = low; j < high; j++) {
    if (arr[j] <= pivot) {
        
      // if element smaller than pivot is found
      // swap it with the greater element pointed by i
      i++;
      
      // swap element at i with element at j
      swap(&arr[i], &arr[j]);
    }
  }

  // swap the pivot element with the greater element at i
  swap(&arr[i + 1], &arr[high]);
  
  // return the partition point
  return (i + 1);
}

void quickSort(int arr[], int low, int high) {
  if (low < high) {
    
    // find the pivot element such that
    // elements smaller than pivot are on left of pivot
    // elements greater than pivot are on right of pivot
    int pi = partition(arr, low, high);
    
    // recursive call on the left of pivot
    quickSort(arr, low, pi - 1);
    
    // recursive call on the right of pivot
    quickSort(arr, pi + 1, high);
  }
}
void swap(int *x, int *y){

	int tmp = *x;
	*x = *y;
	*y = tmp;

}