Re-write the sort program you chose so that it orders integers from largest to s
ID: 3917355 • Letter: R
Question
Re-write the sort program you chose so that it orders integers from largest to smallest rather than smallest to largest.
***********************************************************************************************
// This program uses a bubble sort to arrange an array of integers in
// ascending order
#include <iostream>
using namespace std;
// function prototypes
void bubbleSortArray(int[], int);
void displayArray(int[], int);
const int SIZE = 5;
int main()
{
int values[SIZE] = { 9, 2, 0, 11, 5 };
cout << "The values before the bubble sort is performed are:" << endl;
displayArray(values, SIZE);
bubbleSortArray(values, SIZE);
cout << "The values after the bubble sort is performed are:" << endl;
displayArray(values, SIZE);
system("pause");
return 0;
}
//******************************************************************
// displayArray
//
// task: to print the array
// data in: the array to be printed, the array size
// data out: none
//
//******************************************************************
void displayArray(int array[], int elems) // function heading
{
// displays the array
for (int count = 0; count < elems; count++)
cout << array[count] << " " << endl;
}
//******************************************************************
// bubbleSortArray
//
// task: to sort values of an array in ascending order
// data in: the array, the array size
// data out: the sorted array
//
//******************************************************************
void bubbleSortArray(int array[], int elems)
{
bool swap;
int temp;
int bottom = elems - 1; // bottom indicates the end part of the
// array where the largest values have
// settled in order
do
{
swap = false;
for (int count = 0; count < bottom; count++)
{
if (array[count] > array[count + 1])
{
// the next three lines do a swap
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true; // indicates that a swap occurred
}
}
bottom--; // bottom is decremented by 1 since each pass through
// the array adds one more value that is set in order
} while (swap != false);
// loop repeats until a pass through the array with
// no swaps occurs
}
Explanation / Answer
Your program works fine with the given static input.
Please find the given program with slight modifications which give you the same output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, Arrayss[50], j, tmp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{
cin>>Arrayss[i];
}
cout<<"Sorting Arrayssay using bubble sort technique... ";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(Arrayss[j]>Arrayss[j+1])
{
tmp=Arrayss[j];
Arrayss[j]=Arrayss[j+1];
Arrayss[j+1]=tmp;
}
}
}
cout<<"Elements sorted successfully..!! ";
cout<<"Sorted list in ascending order : ";
for(i=0; i<n; i++)
{
cout<<Arrayss[i]<<" ";
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.