arrange an array of integers in ascending order, computer science homework help

User Generated

fuzyna

Computer Science

Description

/ This program uses a selection sort to arrange an array of integers in

// ascending order

//PLACE YOUR NAME HERE
#include <iostream>
using namespace std;
// function prototypes
void selectionSortArray(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 selection sort is performed are:" << endl;
displayArray(values,SIZE);
selectionSortArray(values,SIZE);
cout << "The values after the selection sort is performed are:" << endl;
displayArray(values,SIZE);
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 array for (int count = 0; count < elems; count++)
cout << array[count] << "";
cout << endl;
}

//******************************************************************
//selectionSortArray
//
// task:to sort values of an array in ascending order
// data in:the array, the array size
// data out:the sorted array
//
//******************************************************************
void selectionSortArray(int array[], int elems)
{
int seek;// array position currently being put in order int minCount;// location of smallest value found
int minValue;// holds the smallest value found
for (seek = 0; seek < (elems-1);seek++)// outer loop performs the swap
// and then increments seek
{
minCount = seek;
minValue = array[seek];
for(int index = seek + 1; index < elems; index++)
{
// inner loop searches through array
// starting at array[seek] searching
// for the smallest value. When the
// value is found, the subscript is
// stored in minCount. The value is
// stored in minValue.
if(array[index] < minValue)
{
minValue = array[index];
minCount = index;
}
}

// the following two statements exchange the value of the
// element currently needing the smallest value found in the
// pass(indicated by seek) with the smallest value found
// (located in minValue)
array[minCount] = array[seek];
array[seek] = minValue;

}
}

Exercise 1: Re-write  the sort program  you  chose  so that it orders  integers from largest  to smallest rather  than smallest to largest.
Exercise 2: Modify your  program  from Exercise  1 so that it prints  the array  at each  step of the algorithm. 

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

I have finished the assignment and attached it as a .txt file. You will need to copy it to a .cpp file to compile/run it. I added com...

Similar Content

Related Tags