//DISPLAY 7.12 Sorting an Array //Tests the procedure sort. #include   void fillArray(int a[], int size, int& numberUsed); //Precondition: size is the declared size of the array a. //Postcondition: numberUsed is the number of values stored in a. //a[0] through a[numberUsed - 1] have been filled with //nonnegative integers read from the keyboard.   void sort(int a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1]. void sortDescend(int a[], int numberUsed);       void swapValues(int& v1, int& v2); //Interchanges the values of v1 and v2.   int indexOfLargest(const int a[], int startIndex, int numberUsed);       int indexOfSmallest(const int a[], int startIndex, int numberUsed); //Precondition: 0 <= startIndex < numberUsed. Referenced array elements have //values. //Returns the index i such that a[i] is the smallest of the values //a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1].   int main( ) {     using namespace std;     cout << "This program sorts numbers from lowest to highest.\n";      int sampleArray[10], numberUsed;     fillArray(sampleArray, 10, numberUsed);     //sort(sampleArray, numberUsed);                 sortDescend(sampleArray, numberUsed);     cout << "In sorted order the numbers are:\n";     for (int index = 0; index < numberUsed; index++)         cout << sampleArray[index] << " ";     cout << endl;    return 0; } //Uses iostream: void fillArray(int a[], int size, int& numberUsed) {                 using namespace std;                 cout << "Enter up to " << size << " nonnegative whole numbers.\n"                                 << "Mark the end of the list with a negative number.\n";                 int next, index = 0;                 cin >> next;                 while ((next >= 0) && (index < size))                 {                                 a[index] = next;                                 index++;                                 cin >> next;                 }numberUsed = index; }void sort(int a[], int numberUsed) { int indexOfNextSmallest; for (int index = 0; index < numberUsed - 1; index++)     {//Place the correct value in a[index]:         indexOfNextSmallest =                      indexOfSmallest(a, index, numberUsed);         swapValues(a[index], a[indexOfNextSmallest]);         //a[0] <= a[1] <=...<= a[index] are the smallest of the original array         //elements. The rest of the elements are in the remaining positions.     } }   void sortDescend(int a[], int numberUsed) { int indexOfNextLargest; for (int index = 0; index < numberUsed - 1; index++)                 {//Place the correct value in a[index]:                                 indexOfNextLargest =                                                 indexOfLargest(a, index, numberUsed);                                 swapValues(a[index], a[indexOfNextLargest]);                                 //a[0] <= a[1] <=...<= a[index] are the smallest of the original array                                 //elements. The rest of the elements are in the remaining positions.                 } }void swapValues(int& v1, int& v2) {     int temp;     temp = v1;     v1 = v2;     v2 = temp; }   int indexOfLargest(const int a[], int startIndex, int numberUsed) {                 int max = a[startIndex],                                 indexOfMax = startIndex;                 for (int index = startIndex + 1; index < numberUsed; index++)                                 if (a[index] > max)                                 {                                                 max = a[index];                                                 indexOfMax = index;                                                 //min is the smallest of a[startIndex] through a[index]                                 }                   return indexOfMax; }int indexOfSmallest(const int a[], int startIndex, int numberUsed) {  int min = a[startIndex],         indexOfMin = startIndex;     for (int index = startIndex + 1; index < numberUsed; index++)         if (a[index] < min)         {             min = a[index];             indexOfMin = index;             //min is the smallest of a[startIndex] through a[index]         }  return indexOfMin; } Modify example 7.12 so that instead of sorting in ascending order, sort the same list of numbers in descending order. Do not modify any of the existing sorting functions.  Instead, add additional functions as necessary.  For example create a function called “sortDescend” We see the following dialog when run:This program sorts numbers from highest to lowest.Enter up to 10 nonnegative whole numbers.Mark the end of the list with a negative number.80 30 50 70 60 90 20 30 40 -1In sorted order the numbers are:90 80 70 60 50 40 30 30

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

//DISPLAY 7.12 Sorting an Array

//Tests the procedure sort.

#include <iostream>

 

void fillArray(int a[], int size, int& numberUsed);

//Precondition: size is the declared size of the array a.

//Postcondition: numberUsed is the number of values stored in a.

//a[0] through a[numberUsed - 1] have been filled with

//nonnegative integers read from the keyboard.

 

void sort(int a[], int numberUsed);

//Precondition: numberUsed <= declared size of the array a.

//The array elements a[0] through a[numberUsed - 1] have values.

//Postcondition: The values of a[0] through a[numberUsed - 1] have

//been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1].

void sortDescend(int a[], int numberUsed);

 

 

 

void swapValues(int& v1, int& v2);

//Interchanges the values of v1 and v2.

 

int indexOfLargest(const int a[], int startIndex, int numberUsed);

 

 

 

int indexOfSmallest(const int a[], int startIndex, int numberUsed);

//Precondition: 0 <= startIndex < numberUsed. Referenced array elements have

//values.

//Returns the index i such that a[i] is the smallest of the values

//a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1].

 

int main( )

{

    using namespace std;

    cout << "This program sorts numbers from lowest to highest.\n";

 

   int sampleArray[10], numberUsed;

    fillArray(sampleArray, 10, numberUsed);

    //sort(sampleArray, numberUsed);

                sortDescend(sampleArray, numberUsed);

    cout << "In sorted order the numbers are:\n";

    for (int index = 0; index < numberUsed; index++)

        cout << sampleArray[index] << " ";

    cout << endl;

   return 0;

}

//Uses iostream:

void fillArray(int a[], int size, int& numberUsed)

{

                using namespace std;

                cout << "Enter up to " << size << " nonnegative whole numbers.\n"

                                << "Mark the end of the list with a negative number.\n";

                int next, index = 0;

                cin >> next;

                while ((next >= 0) && (index < size))

                {

                                a[index] = next;

                                index++;

                                cin >> next;

                }numberUsed = index;

}void sort(int a[], int numberUsed)

{ int indexOfNextSmallest;

for (int index = 0; index < numberUsed - 1; index++)

    {//Place the correct value in a[index]:

        indexOfNextSmallest =

                     indexOfSmallest(a, index, numberUsed);

        swapValues(a[index], a[indexOfNextSmallest]);

        //a[0] <= a[1] <=...<= a[index] are the smallest of the original array

        //elements. The rest of the elements are in the remaining positions.

    }

}

 

void sortDescend(int a[], int numberUsed)

{

int indexOfNextLargest;

for (int index = 0; index < numberUsed - 1; index++)

                {//Place the correct value in a[index]:

                                indexOfNextLargest =

                                                indexOfLargest(a, index, numberUsed);

                                swapValues(a[index], a[indexOfNextLargest]);

                                //a[0] <= a[1] <=...<= a[index] are the smallest of the original array

                                //elements. The rest of the elements are in the remaining positions.

                }

}void swapValues(int& v1, int& v2)

{

    int temp;

    temp = v1;

    v1 = v2;

    v2 = temp;

}

 

int indexOfLargest(const int a[], int startIndex, int numberUsed)

{

                int max = a[startIndex],

                                indexOfMax = startIndex;

                for (int index = startIndex + 1; index < numberUsed; index++)

                                if (a[index] > max)

                                {

                                                max = a[index];

                                                indexOfMax = index;

                                                //min is the smallest of a[startIndex] through a[index]

                                }

 

                return indexOfMax;

}int indexOfSmallest(const int a[], int startIndex, int numberUsed)

{  int min = a[startIndex],

        indexOfMin = startIndex;

    for (int index = startIndex + 1; index < numberUsed; index++)

        if (a[index] < min)

        {

            min = a[index];

            indexOfMin = index;

            //min is the smallest of a[startIndex] through a[index]

        }  return indexOfMin;

}

Modify example 7.12 so that instead of sorting in ascending order, sort the same list of numbers in descending order. Do not modify any of the existing sorting functions.  Instead, add additional functions as necessary.  For example create a function called “sortDescend” We see the following dialog when run:This program sorts numbers from highest to lowest.Enter up to 10 nonnegative whole numbers.Mark the end of the list with a negative number.80 30 50 70 60 90 20 30 40 -1In sorted order the numbers are:90 80 70 60 50 40 30 30 20

2. This exercise will give you the opportunity to become familiar with another sorting algorithm
called a bubble sort. Using the original version of example 7.12, write a program that uses two
identical arrays of at least 18 integers.
a. It should call a function that uses the selection sort algorithm to sort one of the arrays in
ascending order. The function should keep count of the number of exchanges (swaps) it
makes.
The program should then call a function that uses the bubble sort algorithm (you may copy the
function from example 7.13) to sort the other array. It should also keep count of the number of
exchanges it makes
3.This exercise will give you another opportunity to become familiar with a sorting algorithm called a
bubble sort. Take some time to read the text and review the code to see how bubble sort works.
Using the original version of example 7.12, write a program that uses two identical arrays of at 8
integers.
It should call a function that uses the selection sort algorithm to sort one of the arrays in
ascending order. The function should print out the array contents after each pass of the
sort (Recall what a pass is from the first video in Module 13). The function should keep
count of the number of exchanges it makes.
The program should then call a function that uses the bubble sort algorithm (you may copy the
function from example 7.13) to sort the other array. It should also keep count of the number of
exchanges it makes and print out the array contents after each pass of the sort.
Transcribed Image Text:2. This exercise will give you the opportunity to become familiar with another sorting algorithm called a bubble sort. Using the original version of example 7.12, write a program that uses two identical arrays of at least 18 integers. a. It should call a function that uses the selection sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges (swaps) it makes. The program should then call a function that uses the bubble sort algorithm (you may copy the function from example 7.13) to sort the other array. It should also keep count of the number of exchanges it makes 3.This exercise will give you another opportunity to become familiar with a sorting algorithm called a bubble sort. Take some time to read the text and review the code to see how bubble sort works. Using the original version of example 7.12, write a program that uses two identical arrays of at 8 integers. It should call a function that uses the selection sort algorithm to sort one of the arrays in ascending order. The function should print out the array contents after each pass of the sort (Recall what a pass is from the first video in Module 13). The function should keep count of the number of exchanges it makes. The program should then call a function that uses the bubble sort algorithm (you may copy the function from example 7.13) to sort the other array. It should also keep count of the number of exchanges it makes and print out the array contents after each pass of the sort.
J/DISPLAY Z.13 Bubble Sort Program
//Sorts an array of integers using Bubble Sort.
#include "stdafx.h"
www
#include <iostream>
void bubblesortlint arrl), int length);
//Precondition: length <= declared size of the array arr.
//The array elements arr(0] through a[length - 1] have values.
//Postcondition: The values of arr0] through arlength - 1] have
//been rearranged so that arrlo] <= a[1] <= . <= ardlength - 1].
int mainl)
{
using namespace std;
int al] = {3, 10, 9, 2, 5, 1);
bubblesortla, 6);
for (int i=0; i<6; it+)
{
www
cout <<
www.
a <<"".
}
cout << endl;
com
return 0;
}
void bubblesartlint al), )
{
// Bubble largest number toward the right
for (int i = length-1; i> 0; -)
for (int j = 0; j< i; itl
if (ardli) > ardi+1])
{
// Swap the numbers
int temp = arrli+1];
adli+1] = acli);
arrlil = temp;
wordr
Transcribed Image Text:J/DISPLAY Z.13 Bubble Sort Program //Sorts an array of integers using Bubble Sort. #include "stdafx.h" www #include <iostream> void bubblesortlint arrl), int length); //Precondition: length <= declared size of the array arr. //The array elements arr(0] through a[length - 1] have values. //Postcondition: The values of arr0] through arlength - 1] have //been rearranged so that arrlo] <= a[1] <= . <= ardlength - 1]. int mainl) { using namespace std; int al] = {3, 10, 9, 2, 5, 1); bubblesortla, 6); for (int i=0; i<6; it+) { www cout << www. a <<"". } cout << endl; com return 0; } void bubblesartlint al), ) { // Bubble largest number toward the right for (int i = length-1; i> 0; -) for (int j = 0; j< i; itl if (ardli) > ardi+1]) { // Swap the numbers int temp = arrli+1]; adli+1] = acli); arrlil = temp; wordr
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY