1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include "Driver.h"
#include <stdio.h>
#include <ctime>
#include <iostream>
int main()
{
// Declare variables
int array[] = { 42, 4, 5, 600, 1200, 75, 21, 18, 1, 99}; // Array that holds the numbers
int array_size = sizeof(array) / sizeof(array[0]); // The size of the array
double doneTime;
// ---------------------------------------------------------------------------------------------------------------------
// Selection Sort
// See how long it takes for the array to be sorted
clock_t start = clock(); // Start the clock
selectionSort(array, array_size);
clock_t finish = clock(); // Stop the clock after sort is finished
doneTime = static_cast<double>(finish - start) / CLOCKS_PER_SEC; // Calculate how long it took to sort the array
std::cout << "The time it took to store the array for selection sort: "; // Message on how long it took
std:: cout << doneTime << " seconds\n"; // Print out how long it took
// Show the array after it has been sorted
std::cout << "Array Sorted (selection sort):\n";
printArray(array, array_size); // Call on the printArray function
// ----------------------------------------------------------------------------------------------------------------------
// Bubble Sort
// See how long it takes for the array to be sorted
clock_t start_bubble = clock(); // Start the clock
bubbleSort(array, array_size);
clock_t finish_bubble = clock(); // Stop the clock after sort is finished
doneTime = static_cast<double>(finish_bubble - start_bubble) / CLOCKS_PER_SEC; // Calculate how long it took to sort the array
std::cout << "\n\nThe time it took to store the array for bubble sort: "; // Message on how long it took
std::cout << doneTime << " seconds\n"; // Print out how long it took
// Show the array after it has been sorted
std::cout << "Array Sorted (bubble sort):\n";
printArray(array, array_size); // Call on the printArray function
// -----------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------
// Insertion Sort
// See how long it takes for the array to be sorted
clock_t start_insertion = clock(); // Start the clock
insertionSort(array, array_size);
clock_t finish_insertion = clock(); // Stop the clock after sort is finished
doneTime = static_cast<double>(finish_insertion - start_insertion) / CLOCKS_PER_SEC; // Calculate how long it took to sort the array
std::cout << "\n\nThe time it took to store the array for insertion sort: "; // Message on how long it took
std::cout << doneTime << " seconds\n"; // Print out how long it took
// Show the array after it has been sorted
std::cout << "Array Sorted (insertion sort):\n";
printArray(array, array_size); // Call on the printArray function
// -----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------
// Merge Sort
// See how long it takes for the array to be sorted
clock_t start_merge = clock();
mergeSort(array, 0, array_size - 1);
clock_t finish_merge = clock();
doneTime = static_cast<double>(finish_merge - start_merge) / CLOCKS_PER_SEC;
std::cout << "\n\nThe time it took to store the array for merge sort: ";
std::cout << doneTime << " seconds\n";
// Show the array after it has been sorted
std::cout << "Array Sorted (merge sort):\n";
printArray(array, array_size);
// --------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------
// Quick Sort
// See how long it takes for the array to be sorted
clock_t start_quick = clock(); // Start the clock
quickSort(array, 0, array_size - 1);
clock_t finish_quick = clock(); // Stop the clock after sort is finished
doneTime = static_cast<double>(finish_quick - start_quick) / CLOCKS_PER_SEC; // Calculate how long it took to sort the array
std::cout << "\n\nThe time it took to store the array for quick sort: "; // Message on how long it took
std::cout << doneTime << " seconds\n"; // Print out how long it took
// Show the array after it has been sorted
std::cout << "Array Sorted (quick sort):\n";
printArray(array, array_size); // Call on the printArray function
// -----------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------
// Radix Sort
// See how long it takes for the array to be sorted
clock_t start_radix = clock(); // Start the clock
radixSort(array, array_size);
clock_t finish_radix = clock(); // Stop the clock after sort is finished
doneTime = static_cast<double>(finish_radix - start_radix) / CLOCKS_PER_SEC; // Calculate how long it took to sort the array
std::cout << "\n\nThe time it took to store the array for radix sort: "; // Message on how long it took
std::cout << doneTime << " seconds\n"; // Print out how long it took
// Show the array after it has been sorted
std::cout << "Array Sorted (radix sort):\n";
printArray(array, array_size); // Call on the printArray function
// -----------------------------------------------------------------------------------------------------------------------
|