-
Sorting list
I am currently working on a sorting list and I want to find the min, half and max from the sorted list. I have already sorted the list as follows:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int arraySize = 7;
int a[arraySize] = {7, 77, 2, 4, 1, 9, 3};
int i, hold;
for (i=0; i<arraySize;++i)
for (int pass = 0; pass < arraySize-1; ++pass)
{
for (i=0; i<arraySize-1;++i)
{
if (a[i] > a[i+1])
{
hold = a[i];
a[i] = a[i+1];
a[i+1] = hold;
}
}
}
for (i=0; i<arraySize; ++i)
{
cout <<left
<< setw(2) << a[i];
}
cout << endl;
pause();
return EXIT_SUCCESS;
}
The final output should be: 1 4 77
Can somebody help me to find the min, half and max. Thankyou
-
You are sorting in ascending order.
The minimum is the first element, the maximum is the last element, and the median is the (first+last)/2 element (or thereabouts, since you have to account for array indices starting from 0 and the array possibly having an even number of elements, in which case you may take the mean of the two middle elements).
-
i've done the question, so i'll give u a hint on how i did it (using bubble sort
Code:
while(i != length-1 )
{
if(list[i] > list[i+1])
{
temp = list[i+1];
list[i+1] = list[i];
list[i] = temp;
i=0;
}
else
{
i++;
}
}
min = list[0];
max = list[length - 1];
half = list[length/2];