Question

In: Computer Science

(Using C++) Sample array: 100, 45, 33, 55, 356, 11, 1000, 999, 987 You need to...

(Using C++)

Sample array: 100, 45, 33, 55, 356, 11, 1000, 999, 987

You need to perform the following steps to evaluate the algorithm’s time complexity:

  1. Pick any two sorting algorithms (e.g., merge sort, bubble sort).
  2. Find the algorithm implementation.
  3. Create the array, and store all of the elements described above.
  4. Pass the array into the first sorting algorithm.
  5. Pass the array into the second sorting algorithm.
  6. Calculate the execution time for the first sorting algorithm’s execution.
  7. Calculate the execution time for the second sorting algorithm’s execution.

You need to recommend the sorting algorithm that should be used for sorting based on its execution time.

Note that you can use any programming language to implement the steps above.

Your program should contain the following:

  • The implementation of the first sorting algorithm
  • The implementation of the second sorting algorithm
  • An array that can store all of the elements from the sample array

Your program should also do the following:

  • Store the start transaction before executing the sorting algorithm, and insert the end transaction afterward
  • Calculate the difference between end_transaction and start_transaction
  • Pick the sorting algorithm that has the lower execution time

Solutions

Expert Solution

Algorithms :

1. Heap Sort

2. Quick Sort

import timeit

#Heapsort implementation
def heapify(arr, n, i): 
    largest = i 
    l = 2 * i + 1      
    r = 2 * i + 2      
  
    if l < n and arr[i] < arr[l]: 
        largest = l 
  
    if r < n and arr[largest] < arr[r]: 
        largest = r 
  
    if largest != i: 
        arr[i],arr[largest] = arr[largest],arr[i]
  
        heapify(arr, n, largest) 
  
def heapSort(arr): 
    n = len(arr) 
  
    for i in range(n//2 - 1, -1, -1): 
        heapify(arr, n, i) 
  
    for i in range(n-1, 0, -1): 
        arr[i], arr[0] = arr[0], arr[i] 
        heapify(arr, i, 0)
    return arr

#quicksort Implementatio
def partition(arr,low,high): 
    i = ( low-1 )         
    pivot = arr[high] 
  
    for j in range(low , high): 
  
        if   arr[j] < pivot: 
          
            i = i+1 
            arr[i],arr[j] = arr[j],arr[i] 
  
    arr[i+1],arr[high] = arr[high],arr[i+1] 
    return ( i+1 ) 
    
def quickSort(arr,low,high): 
    if low < high: 
  
        pi = partition(arr,low,high)
  
        quickSort(arr, low, pi-1) 
        quickSort(arr, pi+1, high) 
    return arr

data = [100, 45, 33, 55, 356, 11, 1000, 999, 987]

startHeapSort = timeit.timeit()
sortHeap = heapSort(data.copy())
endHeapSort = timeit.timeit()

startQuickSort = timeit.timeit()
sortQuick = quickSort(data.copy(), 0, len(data.copy())-1)
endQuickSort = timeit.timeit()

tHeapSort = endHeapSort - startHeapSort
tQuickSort = endQuickSort - startQuickSort

if tHeapSort < tQuickSort:
    print("Heap Sort execution time is better.")
else:
    print("Quick Sort execution time is better.")

Related Solutions

c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Part 1 A, You are testing H0: μ=100   vs   H1: μ>100 , using a sample of...
Part 1 A, You are testing H0: μ=100   vs   H1: μ>100 , using a sample of n=20 . The test statistic is ttest=2.15 . The P value should be: 0.02232 0.97768 0.02198 B. You are testing H0: μ=15   vs   H1: μ≠15 , using a sample of n=8 . The 95% t Confidence Interval for μ is 17, 23 . The P value of the test could be: 0.9750 0.0500 0.0002 C. You are testing H0: μ=50   vs   H1: μ<50 ,...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
C++ Simple Programming Assignment you need to build off of the code below: #include using namespace...
C++ Simple Programming Assignment you need to build off of the code below: #include using namespace std; // class definition class Fraction {         // two data members         // one representing numerator         int numerator;         // other, representing denominator         int denominator;         public:                 void set (int numerator, int denominator);                 Fraction addedTo (Fraction f);                 Fraction subtract (Fraction f);                 Fraction multipliedBy (Fraction f);                 Fraction dividedBy (Fraction f);                 bool isEqualTo (Fraction f);                 void print (); }; void Fraction :: set (int numerator, int denominator) {         this...
You have a 100-mg sample of I-131. Using Table 7.4 in your text, predict the amount...
You have a 100-mg sample of I-131. Using Table 7.4 in your text, predict the amount of I-131 that would remain after 32 days. Show your work.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT