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...
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input...
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13 count: 5 pid: 13342...
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...
I need an answer in C++, please TITLE PRIME FACTORIZATION USING AN ARRAY-BASED STACK INTRODUCTION This...
I need an answer in C++, please TITLE PRIME FACTORIZATION USING AN ARRAY-BASED STACK INTRODUCTION This project revisits one of the problems in Project 2, and it will re-use a function within the solution developed there. An integer is prime if it is divisible only by itself and 1. Every integer can be written as a product of prime numbers, unique except for their order, called its prime factorization. For example, 1776 = 37 x 3 x 2 x 2...
Create a C++ program that creates instances of a Struct using an Array (you can choose...
Create a C++ program that creates instances of a Struct using an Array (you can choose the number of instances to create). You can also use either user input or an initialization list to initialize 3 peoples names. Make sure that the values stored in each member are printed to the screen.
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:...
Need it in C# using visual studio (also can you show me how to implement the...
Need it in C# using visual studio (also can you show me how to implement the code in vs) Create a New Project named Preferred Customer to be used in a retail store for preferred customers, where customers can earn discount on all purchases depending upon how much money they are going to spend. To begin with designing your Application, you must add a class named Person with properties for holding a Person’s Name, Address and Telephone Number. Next step...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT