# Thread: Help! Check my code over.

1. ## Help! Check my code over.

I have written a successful piece of code that takes a random assortment of numbers and puts them in numerical order and then finds the mean and median of the list. My professor wants us to make it as fast as possible and i think i have a few things that could be cleaned up, and i still don't fully understand C so any help would be grateful.

Edit: Basically all i need is some help finding where i can cut down on processes to make the run time faster.

Code:
```#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void GetValues(int x[], int *pN);
void SortValues(int x[], int n);
double GetMean(int x[], int n);
double GetMedian(int x[], int n);

void main()
{
int a[100],b[100],pN,i;
double mean,median;
do
{
scanf("");
GetValues(a,&pN);	 // get values and put them into "a" array
if(pN!=0)
{
for(i=0;i<pN;i++)
{
b[i]=a[i];	  // is there a better way of doing this?? i feel like it adds alot of unneccasry steps
}
SortValues(b,pN);	 //sort values using the "b" array
mean=GetMean(b,pN);	  //get mean of "b" array
median=GetMedian(b,pN);   //get median
printf("\n     Original      Sorted\n");	  //print everything
for(i=0;i<pN;i++)
{
printf("%13d %11d\n",a[i],b[i]);
}
printf("\nMean:%11.2lf\n",mean);
printf("Median:%9.2lf\n\n",median);
}
}
while(pN!=0);
}

//GetValues
void GetValues(int x[], int *pN)
{
int size,i;
printf("Enter number of integer values: ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("Enter Value  %d: ",i+1);
scanf("%d",&x[i]);
}
*pN=size;
return;
}

//SortValue
void SortValues(int x[], int n)

{

int i,j,t;

for (i=0; i<n; i++)

{

for (j=0; j<(n-1); j++)

{

if (x[j]>x[j+1])

{

t=x[j];

x[j]=x[j+1];

x[j+1]=t;
}

}

}

return;
}

//GetMean
double GetMean(int x[], int n)
{
int i;
double sum=0;
for(i=0;i<n;i++)
{
sum=sum+x[i];
}
sum=sum/n;
return sum;
}

//GetMedian
double GetMedian(int x[], int n)
{
int j,k,k1;
double median;
j=n%2;
if(j==0)
{
k=n/2;
k1=k-1;
median=x[k]+x[k1];
median=median/2;
}
else
{
k=n/2;
median=x[k];
}
return median;
}```

2. Originally Posted by omGeeK
Code:
```for(i=0;i<pN;i++)
{
b[i]=a[i];	  // is there a better way of doing this?? i feel like it adds alot of unneccasry steps
}```
You can just do this instead:
Code:
`memcpy(b, a, sizeof(a[0]) * pN);`

3. If you want to improve performance you could use a faster sorting algorithm than the bubble sort. lol. It is well established that that one sucks. The running time of the bubble sort algorithm goes as O(n^2) - Not very efficient.

4. I think Swarvy hit the main point here. Like all things in life, the best approaches take the most amount of work. Bubble sort is simple, intuitive and hence by no means efficient.

5. alright, thanks guys did some house keeping lol

6. As the numbers are being put in, make all your calculations, right there inside that loop. No need for other functions.

As each number is entered, it also will have an index array that will be changed, to reflect it's sorted status. So you can output the array just as if it was sorted - but it's not sorted. Only the index array is adjusted.

Now, without moving anything but one int in the index array, you have both the original order (printed in the normal way), or the sorted order, (printed through the index instead).

When the last number is entered, you'll make one more calculation on your running mean and median values, and on the index array for the sorted output - right inside that one loop.

And you're done.

It's not that you're program will be doing a lot less work - there isn't much work here. It's that you'll have everything but the last digit, already worked up. When it's entered, you're practically done already.

If you'd like to see an index used to "sort", an array, let me know.