Question

You are working as the software developer and need to identify the best sorting algorithm. Please...

You are working as the software developer and need to identify the best sorting algorithm. Please refer to the following URL:

https://www.geeksforgeeks.org/sorting-algorithms/

You can pick any two-sorting algorithm. You need to determine which sorting algorithm is the best to sort the array of 10k elements.

Use the following steps to help with your solution:

  1. Create C++ functions for any two-sorting algorithm.
  2. Write down the random array generation function to generate at least 10k elements.
  3. Pass the same array into both the sorting algorithm.
  4. Calculate the end transactiontime-start transactiontime for each sorting algorithm.
  5. Determine the best algorithm for sorting 10k elements based on the transaction time.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<stdlib.h>

#include<time.h>

#include <chrono>

#include <unistd.h>

using namespace std;

//method to sort an array of size n using selection sort algorithm

void selectionSort(int *data,int n) {

                                for (int i = 0; i < n - 1; i++) {

                                               // finding the index of smallest element in the unsorted array

                                               int index_min = i;

                                               for (int j = i + 1; j < n; j++)

                                                               if (data[j] < data[index_min])

                                                                               index_min = j;

                                               // swapping element at i with element at index_min

                                               int temp = data[index_min];

                                               data[index_min] = data[i];

                                               data[i] = temp;

                                }

                               

                               

}

//method using bubble sort algorithm to sort numbers in an array

void bubbleSort(int *data, int n){

               

                for(int i=0;i<n;i++){

                                for(int j=0;j<n-1;j++){

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

                                                               // swapping element at j with element at j+1

                                                               int temp = data[j];

                                                               data[j] = data[j+1];

                                                               data[j+1] = temp;

                                               }

                                }

                }

}

//method to fill random elements in an array.

//the range of values in array is between 0 and n

void generateArray(int data[], int n){

                for(int i=0;i<n;i++){

                                data[i]=rand()%n;

                }

}

//method to copy one array to another

void copyArray(int source[], int dest[], int n){

                for(int i=0;i<n;i++){

                                dest[i]=source[i];

                }

}

int main(){

                //seeding random number generator

                srand(time(NULL));

                //number of elements

                const int n=10000;

                //declaring two arrays

                int data1[n];

                int data2[n];

                //filling random values in data1

                generateArray(data1,n);

                //copying data1 to data2, so that both arrays will be the same

                copyArray(data1,data2,n);

                //testing bubble sort

                cout<<"Testing bubble sort technique"<<endl;

                auto start = chrono::steady_clock::now(); //recording start time

                bubbleSort(data1,n); //bubble sorting data1

                auto end = chrono::steady_clock::now(); //recording end time

                //finding time taken in milli seconds and displaying it

                auto time1=chrono::duration_cast<chrono::milliseconds>(end - start).count();

                cout<<time1<<" milliseconds to sort an array with "<<n<<" elements"<<endl;

                //doing the same for selection sort

                cout<<"Testing selection sort technique"<<endl;

                start = chrono::steady_clock::now();

                selectionSort(data2,n);

                end = chrono::steady_clock::now();

                auto time2=chrono::duration_cast<chrono::milliseconds>(end - start).count();

                cout<<time2<<" milliseconds to sort an array with "<<n<<" elements"<<endl;

               

                //comparing two times and displaying which algorithm is more efficient

                if(time1<time2){

                                cout<<"Bubble sort is more efficient"<<endl;

                }else{

                                cout<<"Selection sort is more efficient"<<endl;

                }

               

                return 0;

}

/*OUTPUT*/

Testing bubble sort technique

1067 milliseconds to sort an array with 10000 elements

Testing selection sort technique

270 milliseconds to sort an array with 10000 elements

Selection sort is more efficient

Add a comment
Know the answer?
Add Answer to:
You are working as the software developer and need to identify the best sorting algorithm. Please...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sorting: (40 points) a. We studied several sorting algorithms. Every sorting algorithm has their own special...

    Sorting: (40 points) a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it can only use. Can you explain carefully in which situation the following algorithms would be best sorting algorithm to use in an application. (10 points) i. Insertion sort ii. Selection sort iii. Merge sort iv. Quick sort b. You are given a string of elements as below, Canopus, Mimosa, Betelgeuse, Deneb, Stars, Pollux, Antares, Sirius, Hader i. Your task is to...

  • a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it...

    a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it can only use. Can you explain carefully in which situation the following algorithms would be best sorting algorithm to use in an application. (10 points) i. Insertion sort ii. Selection sort iii. Merge sort iv. Quick sort b. You are given a string of elements as below, Canopus, Mimosa, Betelgeuse, Deneb, Stars, Pollux, Antares, Sirius, Hader i. Your task is to insert the above...

  • c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and...

    c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and implement it as a function. Using the system clock as a timer, determine when the efficiency of the algorithm breaks down. For example, does it become slow after 1000 elements, 10000 elements, 100000 elements? Write some code to figure this out. Then use the sort() algorithm that is part of the STL <algorithm> library. How does this function compare to the function you implemented?...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Data Structures: For each of the following situations, name the best sorting algorithm we studied. (For...

    Data Structures: For each of the following situations, name the best sorting algorithm we studied. (For one or two questions, there may be more than one answer deserving full credit, but you only need to give one answer for each.) (a) The array is mostly sorted already (a few elements are in the wrong place). (b) You need an O(n log n) sort even in the worst case and you cannot use any extra space except for a few local...

  • QUESTION 3 Suppose that you have been running an unknown sorting algorithm. Out of curiosity, you...

    QUESTION 3 Suppose that you have been running an unknown sorting algorithm. Out of curiosity, you once stopped the algorithm when it was part-way done and examined the partially sorted array. You discovered that the last K elements of the array were sorted into ascending order, but the remainder of the array was not ordered in any obvious manner. Based on this, you guess that the sorting algorithm was (select all that apply): heapsort insertion sort mergesort quicksort Shell's sort...

  • ?PLEASE READ CAREFULLY QUESTION #1 I’m doing a project which requires you to implement 4 sorting...

    ?PLEASE READ CAREFULLY QUESTION #1 I’m doing a project which requires you to implement 4 sorting algorithms. Bubble sort pair-wise, Bubble sort list-wise a.k.a selection sort, merge sort, and quick sort. These 4 sorting methods takes in an array of strings and sorts them alphabetically from a-z. I have all 4 sorting algorithms working fine, but I still need to fill out the table. There’s only one section I need help filling out. I basically need help filling out the...

  • . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply...

    . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....

  • In this lab we are going to complete a profile of two sorting algorithms by running...

    In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data. 1. First we need to be able to generate some random integers. You can do this by including the following library : #include Now first run the following to generate a seed : srand (time(NULL)) You can then generate a random number using the function rand() 2. We will use two sort algorithms - Selection Sort and Bubble...

  • Exercise 1: Sorting void mysort(vector & v); Write a function called mysort that rearranges elements of...

    Exercise 1: Sorting void mysort(vector & v); Write a function called mysort that rearranges elements of a vector v so that they form an increasing sequence of values. Allow for different vector positions to contain the same value. Develop your own sorting algorithm; do not use a predefined sort routine from the standard library. Implement a simple sorting algorithm rather than an efficient algorithm. Write code that thoroughly tests your function. Express your tests using assertions. Exercise 2: Comparison For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT