Question

Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works.

HINT: Replace the bubbleSort function with the selectionSort.

Your test program should perform the following steps:

  1. Create an array of random numbers.
  2. Display the array in its original order.
  3. Pass the array to your sorting function.
  4. Display the array again to show that it has been sorted.

Provide a screen shot showing that the above steps are working.

CODE:

#include <iostream>
#include <ctime>
using namespace std;


void bubblesort(int numbers[], int size)
{
   for (int pass = 0; pass < size; pass++)
   {
       for (int i = 0; i < size - 1; i++)
       {
           if (numbers[i] > numbers[i + 1])
           {
               int temp = numbers[i];
               numbers[i] = numbers[i + 1];
               numbers[i + 1] = temp;
           }
       }
   }
}


void display(int numbers[], int size)
{
   for (int i = 0; i < size; i++)
       cout << numbers[i] << " ";

   cout << endl;
}


int main()
{
   const int SIZE = 1000;
   int numbers[SIZE];

   srand(time(0));
   for (int i = 0; i < SIZE; i++)
       numbers[i] = rand() % (SIZE * 5);

   display(numbers, SIZE);
   bubblesort(numbers, SIZE);
   display(numbers, SIZE);


   // keep this stuff at the end of main
   cout << endl;
   system("pause");
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <ctime>

using namespace std;

void selectionSort(int numbers[], int size) {
    int mindInd;
    int temp;
    for (int i = 0; i < size; ++i) {
        mindInd = i;
        for (int j = i + 1; j < size; ++j) {
            if (numbers[j] < numbers[mindInd]) {
                mindInd = j;
            }
        }
        temp = numbers[mindInd];
        numbers[mindInd] = numbers[i];
        numbers[i] = temp;
    }
}

void display(int numbers[], int size) {
    for (int i = 0; i < size; i++)
        cout << numbers[i] << " ";

    cout << endl;
}

int main() {
    const int SIZE = 1000;
    int numbers[SIZE];

    srand(time(0));
    for (int i = 0; i < SIZE; i++)
        numbers[i] = rand() % (SIZE * 5);

    display(numbers, SIZE);
    selectionSort(numbers, SIZE);
    display(numbers, SIZE);
    
    // keep this stuff at the end of main
    cout << endl;
    system("pause");
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Modify this C++ below to show the selection sorting algorithm method. Then write a test program...
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
  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

    Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { ​int temp = numbers[i];​ /* put numbers[i] somewhere to keep it safe */ ​numbers[i] = numbers[j]; /* put numbers[j] at index i */ ​numbers[j] = temp;​ /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

  • c++ /*This is the starter file for your final proficiency test This program has a function...

    c++ /*This is the starter file for your final proficiency test This program has a function that will generate a list of integers using the rand function. Your job is to fill the insertNum and oddCount functions.*/ #include <iostream> #include <ctime> using namespace std; //constants and function prototypes const int CAP = 100; int buildList(int[], int size); void printList(int[], int size); //your functions to implement /*This function finds even numbers in the list, and inserts a number before the even...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • C++ Project Modify the Date Class: Standards Your program must start with comments giving your name...

    C++ Project Modify the Date Class: Standards Your program must start with comments giving your name and the name of the assignment. Your program must use good variable names. All input must have a good prompt so that the user knows what to enter. All output must clearly describe what is output. Using the date class, make the following modifications: Make the thanksgiving function you wrote for project 1 into a method of the Date class. It receive the current...

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