Question

In: Computer Science

Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...

Comparing (Sort Algorithms)

Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs).

The parts of the program should be followed as.....

1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time.

2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time.

3. Print the above two execution time on the screen. Program terminates if x = 80000, otherwise set x = x * 2.

Solutions

Expert Solution

Editable code

Program

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <time.h>

void Bubble_Sort(int* n, int size) {

       int t;

       for (int i = 1; i < size; i++) {

              for (int j = 0; j < size - i; j++) {

                     if (n[j] > n[j + 1]) {

                           t = n[j];

                           n[j] = n[j + 1];

                           n[j + 1] = t;

                     }

              }

       }

}

void Swap(int *X, int *Y)

{

       int Temp = *X;

       *X = *Y;

       *Y = Temp;

}

void Randomize_Array(int* Number, int N)

{

       srand(10);

       for (int i = 0; i < N; ++i)

       {

              int R = rand() % 100;

              Swap(&Number[i], &Number[R]);

       }

}

int cmp(const void *P, const void *Q)

{

       int *P1, *Q1;

       P1 = (int *)P;

       Q1 = (int *)Q;

       return *P1 > *Q1;

}

int main()

{

       int* Array = NULL;

       for (int x = 10000;x <= 80000;x = x * 2)

       {

              Array = new int[x];

              int N = x;

              int Temp = N;

              for (int i = 0; i < N; ++i)

              {

                     Array[i] = Temp--;

              }

              clock_t T1, T2;

              Randomize_Array(Array, N);

              T1 = clock();

              Bubble_Sort(Array, N);

              T2 = clock();

             

              printf("\n\n\n");

              printf("The Bubble Sort execution time for x = %d is %lf ", x, (T2 - T1) / (float)CLOCKS_PER_SEC);

              T1 = clock();

              qsort(Array, N, sizeof(int), cmp);

              T2 = clock();

              printf("\nThe Quick Sort execution time for x = %d is %lf ", x, (T2 - T1) / (float)CLOCKS_PER_SEC);

       }

       printf("\n\n\n");

       system("pause");

       return 0;

}


Related Solutions

Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAM PS: YOU ARE ANSEWRING THE SAME PROGRAMS I WANT DIFFERENT ONE PLEASE , THANK YOU . BECAUSE THE ONE WERE POSTING DOESNT WORKING !!
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection sort and Bubble Sort. The objective of this project is to understand the relation between the theoretical asymptotic complexities (Big-O) computed in class and the actual running times for different kinds of input. The enclosed source file main.cpp contains a program that reads two inputs: a letter specifying which sorting algorithm to use (I for InsertionSort , S for SelectionSort and B for BubbleSort),...
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions. Counter...
IN JAVA. Which sorting does in place sorting? Which sort does the minimum swap to order...
IN JAVA. Which sorting does in place sorting? Which sort does the minimum swap to order ascending /desendinf manner?
Question #1 (Java) - Which sorting does in-place sorting? - Which sort does the minimum swap...
Question #1 (Java) - Which sorting does in-place sorting? - Which sort does the minimum swap to order ascending/descending manner? Note: I just need a short answer to the question no code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT