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++
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write a Pseudo code first for each of these sort methods.   • After writing the pseudo code write python code from the pseudo code. • Upload the pseudo code and Python code for each of the three algorithm mentioned.
Which sorting algorithms is most efficient to sort string consisting of ASCII characters? Heap sort, Merge...
Which sorting algorithms is most efficient to sort string consisting of ASCII characters? Heap sort, Merge sort, insertion sort, bubble sort, selection sort or quick sort?
Sorting algorithm for arrays: understand how to perform the following algorithms. (a). Simple sorting Bubblesort:T(n)ÎO(n2) Selection...
Sorting algorithm for arrays: understand how to perform the following algorithms. (a). Simple sorting Bubblesort:T(n)ÎO(n2) Selection sort : T(n) Î O(n2) Insertion sort: T(n) Î O(n2) (b).Advanced sorting i. Shell sort: O(n2) < O(n1.25) < O(nlogn), O(n1.25) is empirical result for shell sort. Merge sort: T(n) Î O(nlogn), need one temporary array with the same length as the array needed to be sorted. Quick sort: -average case: T(n) Î O(nlogn), -worst case(rare occurrence): T(n) Î O(n2) 5. Searching algorithm for...
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),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT