Пример #1
0
int main()
{
    int length;
    int *array;
    cout << "Enter the length of the array" << endl; 
    cin >> length;
    array = new int[length];
    cout << "Enter the values of the array" << endl; 
    for(int i = 0; i< length ; i++)
    {
        cin >> array[i];
    } 
        
    Sorting objSort;
    //Selection Sort    
  //  objSort.selectionSort(array, length);  
  
    //Bubble Sort
  //  objSort.bubbleSort(array, length);
  
    //Insertion Sort
    //objSort.insertionSort(array, length);
    
    //QuickSort
    //objSort.quickSort(array,0,length-1);
  
   //ShellSort
   // objSort.shellSort(array, length);  
    
    //MergeSort
    objSort.mergeSort(array, 0, length-1);  
    delete[] array;
    return 0;
}
Пример #2
0
/*
 * This function prints the Averages from the tests after running through all the tests.
 *
 * int max_size = maximum size that we want to test each sorting algorithm to
 * int min_size = minimum size that we want to test each sorting algorithm to
 * string theSort = string that contains tha name of the sorting algorithm we are testing.
 *
 * Worst-Case Time Complexity: Θ(n^2 + n^2 + n^2 + n^2) = Θ(n^2)
 */
void testing(int min_size, int max_size, string algorithm){
    // making sure I know what the size of each tests I'm doing
    const int& tests = 5;
    const int& test_size = 10;

    // making sure each size of the array is a set value
    const int& array_size = 10;

    // average cpu_time arrays for each data set
    float* increasing_array_averages = new float[array_size];
    float* decreasing_array_averages = new float[array_size];
    float* random_array_averages = new float[array_size];
    float* constant_array_averages = new float[array_size];

    std::cout << "\nIncreasing Array using " << algorithm << std::endl;

    // run 10 size instances (10000, 20000, 30000, etc.)
    for (int i = 0; i < test_size; i++){
        std::cout << max_size << " instances." << std::endl;

        // instantiate average value and sum value to zero on every new test.
        float average = 0.0;
        float sum = 0.0;

        // create new cpu_times array on every new test.
        float* cpu_times = new float[5];

        // run five tests
        for (int j = min_size; j < tests; j++){
            // set time to 0 before running the cpu times
            float time = 0.0;

            // instantiate new array for increasing ordered array.
            int* increasingArray = ag.generateIncreasingArray(max_size);
            if (algorithm == "insertionSort"){
                clock_t c_start = clock();
                s.insertionSort(increasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "bubbleSort"){
                clock_t c_start = clock();
                s.bubbleSort(increasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }  
            else if (algorithm == "quickSort"){
                clock_t c_start = clock();
                s.quickSort(increasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "mergeSort"){
                clock_t c_start = clock();
                s.mergeSort(increasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }    
            else {
                clock_t c_start = clock();
                s.selectionSort(increasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            //std::cout << "CPU Time (IncreasingArray): " << fixed << setprecision(3) << time << " ms." << std::endl;
            std::cout << fixed << setprecision(3) << time << " ms" << std::endl;

            // add cpu time to cpu_times array
            cpu_times[j] = time;

            // delete array at the end of each run to avoid memory leaks and faulty data
            delete[] increasingArray;
        }
        // add up the cpu_times values
        for (int j = 0; j < 5; j++){
            sum += cpu_times[j];
        }
        // find average value
        average = sum / 5.0;
        //std::cout << "Average time (" << max_size << " instances): " << average << "ms.\n" << std::endl;
        std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl;

        // add average value to increasing_array_averages array per number of runs
        increasing_array_averages[i] = average;

        // once we pass our last instance (100000) break.
        if (max_size == 100000){
            break;
        }

        // go to the next instance (10000 to 20000)
        max_size += 10000;

        // delete cpu_times array to avoid memory leaks or faulty data
        delete[] cpu_times;
    }

    // start max_size back at 10000
    max_size = 10000;

    std::cout << "\nDecreasing Array using " << algorithm << std::endl;

    // run 10 size instances (10000, 20000, 30000, etc.)
    for (int i = min_size; i < test_size; i++){
        std::cout << max_size << " instances." << std::endl;
        // instantiate average value and sum value to zero on every new test.
        float average = 0.0;
        float sum = 0.0;

        // create new cpu_times array on every new test.
        float* cpu_times = new float[5];

        // run five tests
        for (int j = min_size; j < tests; j++){
            // set time to 0 before running the cpu times
            float time = 0.0;

            // instantiate new decreasing order array every run.
            int* decreasingArray = ag.generateDecreasingArray(max_size);

            if (algorithm == "insertionSort"){
                clock_t c_start = clock();
                s.insertionSort(decreasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "bubbleSort"){
                clock_t c_start = clock();
                s.bubbleSort(decreasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }  
            else if (algorithm == "quickSort"){
                clock_t c_start = clock();
                s.quickSort(decreasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "mergeSort"){
                clock_t c_start = clock();
                s.mergeSort(decreasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }    
            else {
                clock_t c_start = clock();
                s.selectionSort(decreasingArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }

            //std::cout << "CPU Time (DecreasingArray): " << fixed << setprecision(3) << time << " ms." << std::endl;
            std::cout << fixed << setprecision(3) << time << " ms" << std::endl;

            // add cpu time to cpu_times array
            cpu_times[j] = time;

            // delete array at the end of each run to avoid memory leaks and faulty data
            delete[] decreasingArray;
        }
        // add up the cpu_times values
        for (int j = 0; j < 5; j++){
            sum += cpu_times[j];
        }
        // find average value
        average = sum / 5.0;
        //std::cout << "Average time (" << max_size << " instances): " << fixed << setprecision(3) << average << "ms.\n" << std::endl;
        std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl;
        // add average value to decreasing_array_averages array per number of runs
        decreasing_array_averages[i] = average;

        // once we pass our last instance (100000) break.
        if (max_size == 100000){
            break;
        }

        // go to the next instance (10000 to 20000)
        max_size += 10000;

        // delete cpu_times array to avoid memory leaks or faulty data
        delete[] cpu_times;
    }

    // start max_size back at 10000
    max_size = 10000;

    std::cout << "\nRandom Array using " << algorithm << std::endl;

    // run 10 size instances (10000, 20000, 30000, etc.)
    for (int i = min_size; i < test_size; i++){
        std::cout << max_size << " instances." << std::endl;
        // instantiate average value and sum value to zero on every new test.
        float average = 0.0;
        float sum = 0.0;

        // create new cpu_times array on every new test.
        float* cpu_times = new float[5];

        // run five tests
        for (int j = min_size; j < tests; j++){
            // set time to 0 before running the cpu times
            float time = 0.0;

            // instantiate new random order array every run.
            int* randomArray = ag.generateRandomArray(max_size);

            if (algorithm == "insertionSort"){
                clock_t c_start = clock();
                s.insertionSort(randomArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "bubbleSort"){
                clock_t c_start = clock();
                s.bubbleSort(randomArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }  
            else if (algorithm == "quickSort"){
                clock_t c_start = clock();
                s.quickSort(randomArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "mergeSort"){
                clock_t c_start = clock();
                s.mergeSort(randomArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }    
            else {
                clock_t c_start = clock();
                s.selectionSort(randomArray, max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }

            //std::cout << "CPU Time (RandomArray): " << fixed << setprecision(3) << time << " ms." << std::endl;
            std::cout << fixed << setprecision(3) << time << " ms" << std::endl;

            // add cpu time to cpu_times array
            cpu_times[j] = time;

            // delete array at the end of each run to avoid memory leaks and faulty data
            delete[] randomArray;
        }
        // add up the cpu_times values
        for (int j = 0; j < 5; j++){
            sum += cpu_times[j];
        }
        // find average value
        average = sum / 5.0;
        //std::cout << "Average time (" << max_size << " instances): " << fixed << setprecision(3) << average << "ms.\n" << std::endl;
        std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl;

        // add average value to random_array_averages array per number of runs
        random_array_averages[i] = average;

        // once we pass our last instance (100000) break.
        if (max_size == 100000){
            break;
        }

        // go to the next instance (10000 to 20000)
        max_size += 10000;

        // delete cpu_times array to avoid memory leaks or faulty data
        delete[] cpu_times;
    }

    // start max_size back at 10000
    max_size = 10000;

    std::cout << "\nConstant Array using " << algorithm << std::endl;

    // run 10 size instances (10000, 20000, 30000, etc.)
    for (int i = 0; i < test_size; i++){
        std::cout << max_size << " instances." << std::endl;

        // instantiate average value and sum value to zero on every new test.
        float average = 0.0;
        float sum = 0.0;

        // create new cpu_times array on every new test.
        float* cpu_times = new float[5];

        // run five tests
        for (int j = 0; j < 5; j++){
            // set time to 0 before running the cpu times
            float time = 0.0;

            if (algorithm == "insertionSort"){
                clock_t c_start = clock();
                s.insertionSort(ag.generateConstArray("Awesome", max_size), max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "bubbleSort"){
                clock_t c_start = clock();
                s.bubbleSort(ag.generateConstArray("Awesome", max_size), max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }  
            else if (algorithm == "quickSort"){
                clock_t c_start = clock();
                s.quickSort(ag.generateConstArray("Awesome", max_size), max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }
            else if (algorithm == "mergeSort"){
                clock_t c_start = clock();
                s.mergeSort(ag.generateConstArray("Awesome", max_size), max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }    
            else {
                clock_t c_start = clock();
                s.selectionSort(ag.generateConstArray("Awesome", max_size), max_size);
                clock_t c_end = clock();
                time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC;
            }

            //std::cout << "CPU Time (ConstantArray): " << fixed << setprecision(3) << time << " ms." << std::endl;
            std::cout << fixed << setprecision(3) << time << " ms" << std::endl;

            // add cpu time to cpu_times array
            cpu_times[j] = time;

            // delete array at the end of each run to avoid memory leaks and faulty data
            delete[] ag.generateConstArray("Awesome", max_size);
        }
        // add up the cpu_times values
        for (int j = 0; j < 5; j++){
            sum += cpu_times[j];
        }
        // find average value
        average = sum / 5.0;
        //std::cout << "Average time (" << max_size << " instances): " << fixed << setprecision(3) << average << " ms.\n" << std::endl;
        std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl;
        // add average value to constant_array_averages array per number of runs
        constant_array_averages[i] = average;

        // once we pass our last instance (100000) break.
        if (max_size == 100000){
            break;
        }

        // go to the next instance (10000 to 20000)
        max_size += 10000;

        // delete cpu_times array to avoid memory leaks or faulty data
        delete[] cpu_times;
    }

    // print averages of each array generated.
    printAverages(increasing_array_averages, decreasing_array_averages, random_array_averages, constant_array_averages, array_size, algorithm);

    // delete each array we used to hold specific (generated arrays) of averages from each test.
    delete[] constant_array_averages;
    delete[] random_array_averages;
    delete[] decreasing_array_averages;
    delete[] increasing_array_averages;
}