#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
| void | selectionSort (int a[], int n) |
| |
| void | insertionSort (int a[], int n) |
| |
| int | impPartition (int a[], int size, int left, int right) |
| |
| void | hybridQuicksortHelper (int a[], int size, int left, int right) |
| |
| void | hybridQuicksort (int a[], int n) |
| |
| void | merge (int aInit[], int aRes[], int aInitLength, int start1, int start2, int end2) |
| |
| void | mergeSort (int initArr[], int n) |
| |
| void | percDown (int array[], int hole, int size) |
| |
| void | heapSort (int a[], int n) |
| |
| char * | checkAscValues (int a[], int n) |
| |
| char * | checkAscending (int a[], int n) |
| |
| int | main () |
| |
- Author
- Henry M. Walker *
- Date
- August 15, 2022 *
◆ numAlgs
◆ sorts
structure to identify both the name of a sorting algorithm and * a pointer to the function that performs the sort * the main function utilizes this struct to define an array of * the sorting algorithms to be timed by this program. *
◆ checkAscending()
| char* checkAscending |
( |
int |
a[], |
|
|
int |
n |
|
) |
| |
check all array elements are in non-descending order *
- Parameters
-
| a | the array to be sorted * |
| n | the size of the array * returns "ok" if array elements in non-descending order; "NO" otherwise * |
◆ checkAscValues()
| char* checkAscValues |
( |
int |
a[], |
|
|
int |
n |
|
) |
| |
check all array elements have values 0, 2, 4, . . ., 2(n-1) *
- Parameters
-
| a | the array to be sorted * |
| n | the size of the array * returns "ok" if array contains required elements; "NO" if not * |
◆ heapSort()
| void heapSort |
( |
int |
a[], |
|
|
int |
n |
|
) |
| |
heap sort, main function *
- Parameters
-
| a | the array to be sorted * |
| n | the size of the array * |
- Postcondition
- the first n elements of a are sorted in non-descending order *
◆ hybridQuicksort()
| void hybridQuicksort |
( |
int |
a[], |
|
|
int |
n |
|
) |
| |
hybrid quicksort, main function * algoithmic elements * random pivot used in partition function * insertion used for small array segments *
- Parameters
-
| a | the array to be sorted * |
| n | the size of the array * |
- Postcondition
- the first n elements of a are sorted in non-descending order *
◆ hybridQuicksortHelper()
| void hybridQuicksortHelper |
( |
int |
a[], |
|
|
int |
size, |
|
|
int |
left, |
|
|
int |
right |
|
) |
| |
Quicksort helper function * algoithmic elements * quicksort used when array segments > variable breakQuicksortToInsertion * insertion sort used for small array segments *
- Parameters
-
| a | the array to be processed * |
| size | the size of the array * |
| left | the lower index for items to be processed * |
| right | the upper index for items to be processed * |
- Postcondition
- sorts elements of a between left and right *
◆ impPartition()
| int impPartition |
( |
int |
a[], |
|
|
int |
size, |
|
|
int |
left, |
|
|
int |
right |
|
) |
| |
Improved Partition function * uses a[left] as pivot value in processing * algoithmic elements * random pivot utilized * swaps only when required by finding misplaced large and small elements *
- Parameters
-
| a | the array to be processed * |
| size | the size of the array * |
| left | the lower index for items to be processed * |
| right | the upper index for items to be processed * |
- Postcondition
- elements of a are rearranged, so that * items between left and index mid are <= a[mid] * items between dex mid and right are >= a[mid] *
- Returns
- mid *
◆ insertionSort()
| void insertionSort |
( |
int |
a[], |
|
|
int |
n |
|
) |
| |
insertion sort *
- Parameters
-
| a | the array to be sorted * |
| n | the size of the array * |
- Postcondition
- the first n elements of a are sorted in non-descending order *
◆ main()
driver program for testing and timing sorting algorithms *
◆ merge()
| void merge |
( |
int |
aInit[], |
|
|
int |
aRes[], |
|
|
int |
aInitLength, |
|
|
int |
start1, |
|
|
int |
start2, |
|
|
int |
end2 |
|
) |
| |
merge sort helper function *
- Parameters
-
| aInit | source array for merging * |
| aRes | target array for merging * |
| aInitLength | the size of the array segment to be merged * |
| start1 | the first index of the first array segment to be merged * |
| start2 | the first index of the second array segment to be merged * |
| end2 | the last index of the second array segment to be merged * |
- Postcondition
- elements aInit[start1]..aInit[start1+mergeSize] merged with * aInit[start2]..Init[end2] * with the result placed in aRes * Note: it may be that start2 >= aInit.length, in which case, only the * valid part of aInit[start1] is copied *
◆ mergeSort()
| void mergeSort |
( |
int |
initArr[], |
|
|
int |
n |
|
) |
| |
merge sort helper function *
- Parameters
-
| initArr | the array to be sorted * |
| n | the size of the array * |
- Postcondition
- the first n elements of a are sorted in non-descending order *
◆ percDown()
| void percDown |
( |
int |
array[], |
|
|
int |
hole, |
|
|
int |
size |
|
) |
| |
percDown function *
- Parameters
-
| array | the array to be made into a heap, starting at hold * |
| hole | base of subtree for start of processing * |
| size | the size of the array * |
- Precondition
- all nodes in left and right subtrees of the hole node are heaps *
- Postcondition
- all nodes in the tree from the hole node downward form a hea *
◆ selectionSort()
| void selectionSort |
( |
int |
a[], |
|
|
int |
n |
|
) |
| |
straight selection sort *
- Parameters
-
| a | the array to be sorted * |
| n | the size of the array * |
- Postcondition
- the first n elements of a are sorted in non-descending order *