# Array Sorting problem

This is a discussion on Array Sorting problem within the C++ Programming forums, part of the General Programming Boards category; The program takes the input and finds mean, median, min, and max of the input numbers. I am having problems ...

1. ## [solved] Array Sorting problem

The program takes the input and finds mean, median, min, and max of the input numbers. I am having problems sorting an array. When it comes to sorting, the largest number gets put first, when in fact it should be smallest to largest. After the first number, the rest of the numbers are correctly sorted. Compiled with Microsoft Visual C++ 6.0.

Code:
```//AS-30
#include <iostream>
#include <string>
using namespace std;

void main ()
{
double data [20],sdata[20];
int length,i;
double min,max,mean,median,sum,temp;
bool swapped;
//input data into array
cout <<"Enter data size: "<<endl;
cin >>length;

for (i=0; i<length;i++)
{
cout <<"Enter a data item: "<<endl;
cin >>data[i];
}
//copy the contents of data into sdata

for (i=0; i<length;i++)
{
sdata[i] = data[i];
}
//sort array sdata
swapped=true;
while (swapped)
{
swapped=false;
for (i=0;i<length;i++)
{
if (sdata[i] > sdata[i+1])
{
//swap
temp=sdata[i];
sdata[i] = sdata [i+1];
sdata [i+1] = temp;
swapped=true;
}
}
}
//compute min,max,mean,median
min=sdata[0];
max=sdata[length-1];
sum=0;

for (i=0; i<length;i++)
{
sum=sum+sdata[i];
}
mean=sum/length;

int indexHi,indexLo,index;
if ((length %2) != 0 )
{
index = length / 2;
median = sdata [index];
}
else
{
indexHi = length / 2;
indexLo = indexHi -1;
median = (sdata[indexLo] + sdata[indexHi] ) / 2;
}

//display original data
cout <<"Original data: "<<endl;
for (i=0; i<length;i++)
{
cout <<data[i] << " ";
}
cout <<endl;
//display sorted data
cout <<"Sorted data: "<<endl;
for (i=0; i<length;i++)
{
cout <<sdata[i] << " ";
}
cout <<endl;
cout <<"Min: "<<min<<endl;
cout <<"Max: "<<max<<endl;
cout <<"Mean: "<<mean<<endl;
cout <<"Median: "<<median<<endl;
}```

2. Code:
`for (i=0;i<length;i++)`
You're overrunning your array... only loop through length - 1 (since you have i+1 in the loop).

Also, there are more efficient sort functions out there, and they're given to you. Search the web for how to use std::sort, and (for future reference) std::swap. (std::sort will be better than your sort, std::swap will be equal or better depending on the data type.)

Edit: hmm, std::copy might also have helped you... algorithms. This is what makes C++ so much fun a lot of the time... the basic stuff is done for you.

3. Like cactus_hugger said, some algorithms are already done for you in C++ standards. Your sorting algorithm is/is a form of bubble sorting. Selection sorting is faster, as it only needs to go through the entire array one complete time for every number in the array. Bubble sorting on the other hand, goes through the entire array as many times as it needs in order to make the array sorted. I had to do a comparison of the two for an assignment and it took bubble sorting roughly ten times more "cycles" to go through the same numbers (different numbers will be a different result) that selection sort could do.

4. Maybe the bubble sort is the assignment. Not some Binary Tree Sorting or such. Anyway, what Cactus_Hugger said is correct. You have this line:
Code:
`if (sdata[i] > sdata[i+1])`
So, if it reached this point where i= length - 1, the "sdata[i+1]" will be equal to "sdata[i+1]" and will return a garbage value because it's out of the array bound. At a time like this, the Step Over and Step Into feature of a debugger will come in handy.

5. Originally Posted by scwizzo
Like cactus_hugger said, some algorithms are already done for you in C++ standards. Your sorting algorithm is/is a form of bubble sorting. Selection sorting is faster, as it only needs to go through the entire array one complete time for every number in the array. Bubble sorting on the other hand, goes through the entire array as many times as it needs in order to make the array sorted. I had to do a comparison of the two for an assignment and it took bubble sorting roughly ten times more "cycles" to go through the same numbers (different numbers will be a different result) that selection sort could do.
Selection sort is typically faster, but not for the reason you stated. "as many times as it needs in order to make the array sorted" can easily be less than "one complete time for every number in the array".
In fact, an optimised bubble sort never has to perform more passes than a Selection sort, and can often perform fewer. The sole reason selection sort is usually faster is because it does less copying of the objects it is sorting. However, I would only expect a 10x speed difference for an object type that is quite expensive to copy.

OP: You've made the mistake of writing void main, that is not valid C or C++ code. It MUST be declared as returning an int.
You are also missing data integrity checks. What's stopping someone from saying they want to input 999999 numbers, and crashing the program?