# Thread: Need help with algorithm to sort single array (without using quicksort)

1. ## Need help with algorithm to sort single array (without using quicksort)

Well, I am trying to sort an array of ten numbers in ascending and descending orders.

The code compiles very well without a single warning. Yet the results are horrendous and horrifying, when it comes to sorting the "printf" function keeps returning a pleasant assortment of "1111111" or "2222222" Namely the last or first value in the array instead of the previously inserted numbers.

I have no idea what is going on and I need your magnanimous help against the beast...

Aaaand here is the code!

Code:
```#include <stdio.h>
#include <stdlib.h>

void sortA(int [], int);
void sortD(int [], int);

int main(){

int anStore[10] = {0};
int nChoice;

int x;

for(x=0; x<10; x++) {
scanf("%d", &anStore[x]);}

printf("Please choose sorting options: \n1.\tAscending\n2.\tDescending\n\nChoice: ");
scanf("%d", &nChoice);

switch(nChoice){
case 1:{ sortA(anStore, 10); } break;
case 2:{ sortD(anStore, 10); } break; }

printf("Numbers after sorting are...\n");

int y;

for(y=0; y<10; y++){
printf("\n%d", anStore[y]); }}

void sortA(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] < anSrt[y])
anSrt[y] = anSrt[x]; }} }

void sortD(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] > anSrt[y])
anSrt[y] = anSrt[x]; }} }```

2. Suggestion, write a function to swap the two locations.

Tim S.

3. Elaborate sir!

Well, that's what my two sorting functions basically do? but obviously it is not working...

4. Is it a syntax error? The sort that will compile but mess up the whole thing runtime?

5. A standard swap function works.
Using an "=" sign in place of a swap function does not work.

Common code in side of a swap function
Code:
```temp = *a;
*a = *b;
*b = temp;```
Tim S.

6. WOW that works! THANK YOU!!

Here is how i did my swap function:

Code:
```void swap(int *, int *);

void swap(int *a, int *b){

int store;

store = *a;
*a = *b;
*b = store;```
And these are the modified sorting functions!
Code:
```void sortA(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] > anSrt[y])
swap(&anSrt[y], &anSrt[x]);}} }

void sortD(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] < anSrt[y])
swap(&anSrt[y], &anSrt[x]); }} }```
But tell me something, this swap function, it swaps the addresses not the values, yes?

Can you explain the logic of the swap function to me, or point me somewhere where it is explained slowly?

I mean I understand it, but not why it is written like that...

7. But tell me something, this swap function, it swaps the addresses not the values, yes?
I would say false to that statement; but, it might be correct from a English Language viewpoint.

Put the value of location a into temp location
Put the value of location b into location a
Put the value of temp location into location b

Tim S.

8. Aww thanks!

9. Originally Posted by Sokion
The code compiles very well without a single warning.

Where do you return from main, for example?

-Wall is your friend if you're using gcc.

10. Originally Posted by ledow

Where do you return from main, for example?

-Wall is your friend if you're using gcc.
Umm okay, thanks...

Er, why should I return from main()? You mean like make main() return something?

main() is going to be the only function I have in the primary source code file, these other functions will have a separate source code and an include file on their own. This is just a draft...

11. Originally Posted by Sokion
why should I return from main()? You mean like make main() return something?
You declared that the main function returns an int, so you should return an int from it: return 0; if main returns normally. That said, if you are compiling with respect to the 1999 edition of the C standard or later, this returning of 0 in main is optional as a special case, so it is something of a bad example on ledow's part.

12. Originally Posted by laserlight
You declared that the main function returns an int, so you should return an int from it: return 0; if main returns normally. That said, if you are compiling with respect to the 1999 edition of the C standard or later, this returning of 0 in main is optional as a special case, so it is something of a bad example on ledow's part.
Oh okay, thanks!

13. Ooookay, well problem solved!

Here is the source code after corrections! Thanks everyone! Especially stahta01!

Code:
```#include <stdio.h>
#include <stdlib.h>

void sortA(int [], int);
void sortD(int [], int);
void swap(int *, int *);

int main(){

int anStore[10] = {0};
int nChoice;

int x;

for(x=0; x<10; x++) {
scanf("%d", &anStore[x]);}

printf("Please choose sorting options: \n1.\tAscending\n2.\tDescending\n\nChoice: ");
scanf("%d", &nChoice);

switch(nChoice){
case 1:{ sortA(anStore, 10); } break;
case 2:{ sortD(anStore, 10); } break; }

printf("Numbers after sorting are...\n\n");

int y;

for(y=0; y<10; y++){
printf("%d\n", anStore[y]); }}

void sortA(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] > anSrt[y])
swap(&anSrt[y], &anSrt[x]);}} }

void sortD(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] < anSrt[y])
swap(&anSrt[y], &anSrt[x]); }} }

void swap(int *a, int *b){

int store;

store = *a;
*a = *b;
*b = store;}```

14. I have two stylistic suggestions:
• Place your closing braces on their own lines. This is more conventional.
• Have your function prototypes match the function definitions, including the parameter names. This makes them more readable.

For example:
Code:
```#include <stdio.h>
#include <stdlib.h>

void sortA(int anSrt[], int nSize);
void sortD(int anSrt[], int nSize);
void swap(int *a, int *b);

int main(){

int anStore[10] = {0};
int nChoice;

int x;

for(x=0; x<10; x++) {
scanf("%d", &anStore[x]);
}

printf("Please choose sorting options: \n1.\tAscending\n2.\tDescending\n\nChoice: ");
scanf("%d", &nChoice);

switch(nChoice){
case 1:{ sortA(anStore, 10); } break;
case 2:{ sortD(anStore, 10); } break;
}

printf("Numbers after sorting are...\n\n");

int y;

for(y=0; y<10; y++){
printf("%d\n", anStore[y]);
}
}

void sortA(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] > anSrt[y])
swap(&anSrt[y], &anSrt[x]);
}
}
}

void sortD(int anSrt[], int nSize){

int x, y;

for(y=0; y<nSize; y++){
for(x=0; x<nSize; x++){

if(anSrt[x] < anSrt[y])
swap(&anSrt[y], &anSrt[x]);
}
}
}

void swap(int *a, int *b){

int store;

store = *a;
*a = *b;
*b = store;
}```

15. Thanks, will do.