/* ****************************************************************************** * file name : main.cpp * author : Hung Q. Ngo * description: test run all of the following sorting algorithms * - insertion sort * - selection sort * - merge sort * - quick sort * - randomized quick sort * - heap sort * on random, sorted, and inversedly sorted inputs of varying sizes * print the run times out in a csv file to be opened with Excel * I did the graphing from Excel ****************************************************************************** */ #include #include #include // for rand() and srand() #include // for time() #include "search_sort.h" using namespace std; void print_vec(vector& vec); /** * ----------------------------------------------------------------------------- * print a vector of integers, finish with a new line * ----------------------------------------------------------------------------- */ void print_vec(vector& vec) { for (int i=0; i random_vector(size_t n) { srand(static_cast(time(0))); vector ret(n); for (int i=0; i descending_vector(size_t n) { vector ret(n); for (int i=0; i ascending_vector(size_t n) { vector ret(n); for (int i=0; i almost_ascending_vector(size_t n) { vector ret(n); int i, j,k; srand(static_cast(time(0))); for (i=0; i input_sizes, void (*sort_algo)(vector&), vector (*get_data)(size_t)) { time_t start,end; double dif; vector data; for(int i=0; i input_sizes, vector (*fp)(size_t)) { cout << "Insertion,"; test_run(input_sizes, insertion_sort, fp); cout << "Selection,"; test_run(input_sizes, selection_sort, fp); cout << "Merge,"; test_run(input_sizes, merge_sort, fp); cout << "Heap,"; test_run(input_sizes, heap_sort, fp); cout << "Quicksort,"; test_run(input_sizes, quick_sort, fp); cout << "Randomized Quicksort,"; test_run(input_sizes, randomized_quick_sort, fp); } /** * ----------------------------------------------------------------------------- * Test those guys and gather some data * We have 4 sorting algorithms: Insertion, Selection, Merge, Quick, and RQuick * We'll test them with different sizes and different types of input: * n = 1000, 2000, 10000, 20000, 50000, 100000 * input already sorted * input inversedly sorted * input randomized * input almost sorted * n=200000 gives me segmentation fault * ----------------------------------------------------------------------------- */ int main() { // to test a specific sorting algorithm you can do /* vector vec = random_vector(30); cout << "Before: "; print_vec(vec); randomized_quick_sort(vec); cout << "After: "; print_vec(vec); */ static int arr[] = {1000,2000,10000,20000,50000,100000}; vector input_sizes(arr, arr + sizeof(arr)/sizeof(int)); cout << "Randomized input: " << endl; for (int i=0; i