logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jul21/sorts.h

/*

An include file for the sorting functions discussed in class.

This file has the following sorts:

-Bubble Sort
-Insertion Sort
-Selection Sort
-Merge Sort
-Quicksort

*/


void swap(int *x, int *y){

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

}

void bubbleSort(int arr[], int size){

	if(!arr)
		return;

	for(int i = 0; i < size-1; i++){
		//last elements are already in order
		for(int j = 0; j < size-i-1; j++){
			if(arr[j] > arr[j+1])//if the elements adjacent to each other aren't in ascending order
				swap(&arr[j], &arr[j+1]);				
		}
	}
}

void insertionSort(int arr[], int size){

		if(!arr)
			return;

  	int key, j;
    for(int i = 1; i < size; i++) {
        key = arr[i];
        j = i - 1;
 
        /* Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }

}

void selectionSort(int arr[], int size){

		if(!arr)
			return;

    int i, j, min_idx;
 
    // One by one move boundary of unsorted subarray
    for (i = 0; i < size-1; i++) {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < size; j++)
          if (arr[j] < arr[min_idx])
            min_idx = j;
 
        // Swap the found minimum element with the first element
        swap(&arr[min_idx], &arr[i]);
    }
}

void merge(int arr[], int start, int mid, int end) {
	// create a temp array
	int temp[end - start + 1];

	// crawlers for both intervals and for temp
	int i = start, j = mid+1, k = 0;

	// traverse both arrays and in each iteration add smaller of both elements in temp 
	while(i <= mid && j <= end) {
		if(arr[i] <= arr[j]) {
			temp[k] = arr[i];
			k += 1; i += 1;
		}
		else {
			temp[k] = arr[j];
			k += 1; j += 1;
		}
	}

	// add elements left in the first interval 
	while(i <= mid) {
		temp[k] = arr[i];
		k += 1; i += 1;
	}

	// add elements left in the second interval 
	while(j <= end) {
		temp[k] = arr[j];
		k += 1; j += 1;
	}

	// copy temp to original interval
	for(i = start; i <= end; i += 1) {
		arr[i] = temp[i - start];
	}
}

// arr is an array of integer type
// start and end are the starting and ending index of current interval of arr

void mergeSort(int arr[], int start, int end) {

	if(!arr)
		return;

	if(start < end) {
		int mid = (start + end) / 2;
		mergeSort(arr, start, mid);
		mergeSort(arr, mid+1, end);
		merge(arr, start, mid, end);
	}
}

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(!arr)
		return;

  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);
  }
}