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

Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest.  The highest average should then be on the last row.. When you sort...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
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
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
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...
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort...
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort algorithm and create a recursive implementation of that algorithm. Your program should: Randomly generate an array of at least 20 values. Display the contents of that (unsorted) array. Use the recursive implementation of either Selection or Insertion Sort to sort the values. Display the contents of the now sorted array, to demonstrate the success of the algorithm.
(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.
This is an exercise in correctly implementing insertion sort and selection sort. This assignment includes a...
This is an exercise in correctly implementing insertion sort and selection sort. This assignment includes a text data file containing information on tutorial websites for a variety of programming languages. The data file is named Tutorials. It contains records of programming tutorial websites. The record structure for this text file is: FIELD 1 = Programming Language FIELD 2 = Name and Description of Website FIELD 3 = URL Web Address of Language Tutorial The structure of the file is that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT