# Array sort

This is a discussion on Array sort within the C Programming forums, part of the General Programming Boards category; Hi I have this code but i can't write the numbers sorted, what's wrong?? Code: #include <stdio.h> #include <stdlib.h> #include ...

1. ## Array sort

Hi

I have this code but i can't write the numbers sorted, what's wrong??
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TAM 100

void LeVetor(int num[])
{
int i, n, tmp;
srand(time(NULL));
// Inicia o array
for(i = 0;i < TAM;++i)
num[i] = i;
// Embaralha o array
for (i = TAM; i > 1;)
{
n = rand() % i;
--i;
tmp = num[n];
num[n] = num[i];
num[i] = tmp;
}
// Iteração através do array. Os números já estão random
for(i = 0;i < 4;++i)
printf("%d\n", num[i]);
}

void OrdenaVetor(int num[])
{
int aux, i=0, j=0;
for(i=0; i<TAM; i++){
for(j=0; j<TAM; j++)
{
if(num[i]<num[j])
{
aux=num[i];
num[i]=num[j];
num[j]=aux;
}
}
}
}

int main(void)
{
int num[TAM];
int i;
LeVetor(num);
printf("\n \n \n");
OrdenaVetor(num);
for(i=0; i<4; i++)
{
printf("%d", num[i]);
}

}```
I want to have 4 numbers random, this is working, then i want another printf with this 4 numbers sorted.

What's wrong with this, i have
Random number
37
94
38
5

Sorted
0123
it should be
5
37
38
94

any help?

Thnaks

2. Please post the algorithm name; you are trying to use to sort.

Your code matches none of the ones I know.

Link to bubble sort the one normally taught first. Bubble sort - Wikipedia, the free encyclopedia

Tim S.

3. Originally Posted by stahta01
Please post the algorithm name; you are trying to use to sort.
Code:
```void bubbleSort(int v[], int n)
{
int i, j, temp;
for (i = n; i > 0; i--)
for (j = 1; j <= i; j++)
if(v[j-1] > v[j]){
temp = v[j-1];
v[j-1] = v[j];
v[j] = temp;
}
}```

4. Does it now sort correctly since you changed the code?

Tim S.

5. No;

Gives me the same number unsorted....

What's wrong?

Look at the changes and figure out what you did wrong.

NOTE: The code was working right you interpreted [the result] as being wrong.

Code:
```int main(void)
{
int num[TAM];
int i;
LeVetor(num);
printf("\n \n \n");
bubbleSort(num, 4);
for(i=0; i<4; i++)
{
printf("%d\n", num[i]);
}

return 0;
}```
Oops, found bug in bubble sort.
NOTE: Normally experienced programers try to avoid using "<=" when "<" works.
Because most of the times using "<=" or ">=" has resulted in fence post errors based on past experience.
Look-up fence post errors. http://en.wikipedia.org/wiki/Off-by-one_error

Code:
```void bubbleSort(int v[], int n)
{
int i, j, temp;
for (i = n; i > 0; i--)
for (j = 1; j < i; j++)
if(v[j-1] > v[j]){
temp = v[j-1];
v[j-1] = v[j];
v[j] = temp;
}
}```

Code:
`for (j = 1; j <= i; j++)`
And you

Code:
`for (j = 1; j < i; j++)`

Why did you use

bubbleSort(num, 4);

??

Many thanks

8. Originally Posted by Gil Carvalho

Why did you use

bubbleSort(num, 4);
Because you thought 0123 was the wrong result.

Tim S.

9. If you have an 100 element array, is arrayName[100] a valid location to use?

That is why I changed it to < instead of <=.
i is set to 100 the first time; which results in j being 100 once if "<=" is used.

Tim S.

10. Note: When testing sorting routines it is good to test with all the inputs the same value at least once.
If you write, the routine wrong it could result in a endless loop for that input set.

I did not try that with your sorting routine.

Other things good to test are:
Input with reverse sorted data set.
data set with zero, one, two, or three elements in the data set

Tim S.

11. Originally Posted by stahta01
Note: When testing sorting routines it is good to test with all the inputs the same value at least once.
If you write, the routine wrong it could result in a endless loop for that input set.

I did not try that with your sorting routine.

Other things good to test are:
Input with reverse sorted data set.
data set with zero, one, two, or three elements in the data set

Tim S.
Ok Tim

I will try to be more concentrated next time and test with your picks

Thanks

How can i put this code into a c file, the c file has already an int main, i want to call this when the user choose the option to sort the array??

13. Originally Posted by Gil Carvalho

How can i put this code into a c file, the c file has already an int main, i want to call this when the user choose the option to sort the array??
I really do NOT see the problem; this assumes you know how to edit code.

You either cut and paste the code or do a multiple file project.

NOTE: There can only be a single main function in a C project; so, do NOT try it with two or more main functions.

NOTE: If this is a question about how to do a multiple file project; you need to post the IDE name and possibly the Compiler/Linker used.

Tim S.

14. Originally Posted by stahta01
I really do NOT see the problem; this assumes you know how to edit code.

You either cut and paste the code or do a multiple file project.

NOTE: There can only be a single main function in a C project; so, do NOT try it with two or more main functions.

NOTE: If this is a question about how to do a multiple file project; you need to post the IDE name and possibly the Compiler/Linker used.

Tim S.
Thanks Tim