I am completely lost, I can reaqlly use help thanks you. This is the assignment
ID: 3813272 • Letter: I
Question
I am completely lost, I can reaqlly use help thanks you. This is the assignment in my C++ programming class....
Consider the following sort procedure written in pseudo code:
Exchange Sort of Array X[]
For I = 0 to (number of array elements - 2), increment by 1 For J = (I + 1) to (number of array elements - 1), increment by 1 If X[I] is greater than X[J] then temp = X[I] X[I] = X[J] X[J] = temp Next J Next I
Implement the sort procedure in main() without using pointers. Declare the array as follows using the test data shown: int x[] = { 28, 87, -3, 45, 19 }; Use a for loop to print out the array before sorting and again after sorting. There should be no output within the sort procedure
Explanation / Answer
#include <iostream>
using namespace std;
void ExchangeSort(int array[5])
{
int i, j;
int temp;
for (i=0; i< 4; i++)
{
for(j = (i+1); j < 5; j++)
{
if (array[i] > array[j]) //compare consecutive elements of array
{
temp= array[i]; // swap
array[i] = array[j];
array[j] = temp;
}
}
}
}
int main()
{
int x[] = { 28, 87, -3, 45, 19 };
int i;
cout<<"Array elements before sorting :";
for(i=0;i<5;i++)
{
cout<<x[i]<<" ";
}
ExchangeSort(x);
cout<<" Array elements after sorting :";
for(i=0;i<5;i++)
{
cout<<x[i]<<" ";
}
return 0;
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.