Question

In: Computer Science

PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND...

PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES

PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND BUBBLE SORT FUNCTION TO THIS PROGRAM

#include <iostream>
#include<vector>
#include <algorithm >  
#include <chrono>   
#include <ctime>

using namespace std;

void bubblesSort()
{
// Please create Bubble Sort function// Make another for Selection Sort and  Insertion Sort
}

int main()
{
// empty vector
vector<int> data; // data [0], data [1]... data[N-1] <-- end(data)

// set of values to test N
for (auto N : { 20, 40, 50, 100, 120, 130, 140, 150, 200, 300, 5000, 7000 })
{
N = N * 100;
data.clear(); // wipe-out vector
data.resize(N); // resize

// anomyous/lambda function used to Generate PRN
auto random = [&N]() { return rand() % N; };

// Fill up the vector
generate(begin(data), end(data), random);

using namespace std::chrono;

// start time
auto time1 = high_resolution_clock::now();

// Call your function: bubble Sort
//sort(begin(data), end(data));

bubblesSort (data, N);

selectionSort();

insertionSort();

// end time
auto time2 = high_resolution_clock::now();

auto timeMeasure = duration_cast<milliseconds>(time2 - time1);

//
cout << "Time to sort : " << N << " number was: " << timeMeasure.count() << " milli-sec\n";

}

return 0;
}

Solutions

Expert Solution

#include <iostream>
#include<vector>
#include <algorithm>// no space here   
#include <chrono>   
#include <ctime>

using namespace std;

void swap(vector<int> &data, int i, int j)
{
    int tmp= data[i];
    data[i]=data[j];
    data[j]=data[i];
}



void bubblesSort( vector<int>&data)
{

   int n=data.size();  // will give us size of vector, you can also use N originally
        
        for(int i=0;i<n-1;i++){
            for(int j=0;j<n-i-1;j++){
                if(data[j]>data[j+1])
                swap(data,i,i+1);
            }
        }
        
}

void selectionSort(vector<int> &data){
    
    int n=data.size();
    
    
    for (int i = 0; i < n - 1; i++)
        {
                int min = i;
                for (int j = i + 1; j < n; j++)
                {
                        if (data[j] < data[min])
                                min = j;        // update index of min element
                }
                swap(data, min, i);
        }
    
}


void insertionSort(vector<int> &data){
    // Start from second element (element at index 0
        // is already sorted)
        int n=data.size();
        for (int i = 1; i < n; i++)
        {
                int value = data[i];
                int j = i;
                // Find the index j within the sorted subset arr[0..i-1]
                // where element arr[i] belongs
                while (j > 0 && data[j - 1] > value)
                {
                        data[j] = data[j - 1];
                        j--;
                }

                // Note that subarray arr[j..i-1] is shifted to
                // the right by one position i.e. arr[j+1..i]

                data[j] = value;
        }
}

int main()
{
// empty vector
vector<int> data; // data [0], data [1]... data[N-1] <-- end(data)

// set of values to test N
for (auto N : { 20, 40, 50, 100, 120, 130, 140, 150, 200, 300, 5000, 7000 })
{
N = N * 100;
data.clear(); // wipe-out vector
data.resize(N); // resize

// anomyous/lambda function used to Generate PRN
auto random = [&N]() { return rand() % N; };

// Fill up the vector
generate(data.begin(),data.end(), random);

using namespace std::chrono;

// start time
auto time1 = high_resolution_clock::now();

// Call your function: bubble Sort
//sort(begin(data), end(data));

bubblesSort (data); // i did not take N here

selectionSort(data);

insertionSort(data);

// end time
auto time2 = high_resolution_clock::now();

auto timeMeasure = duration_cast<milliseconds>(time2 - time1);

//
cout << "Time to sort : " << N << " number was: " << timeMeasure.count() << " milli-sec\n";

if(N==10000)
break;  // do not forget to break at some point or it will become a countless loop

}

return 0;
}

Related Solutions

Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
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?
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT