# Problem sorting numeric array.

This is a discussion on Problem sorting numeric array. within the C Programming forums, part of the General Programming Boards category; Hi, I'm having trouble sorting the numbers in ascending order from an array. As always I'd appreicate some feedback, my ...

1. ## Problem sorting numeric array.

Hi,

I'm having trouble sorting the numbers in ascending order from an array. As always I'd appreicate some feedback, my logic is failing me somewhere.

Code:
```int test[10] = { 130, 140, 70, 30, 223, 43, 2, 87, 55, 56 };
sort_numeric(test);
return 0;```
my function:

Code:
```void sort_numeric(int scores[10])
{

int buffer[10] = { 0,0,0,0,0,0,0,0,0,0 };
int marker[10] = { 0,0,0,0,0,0,0,0,0,0 };
int i = 0, j = 0, temp = 0;

for ( i = 0; i < 10; i++ )
{
temp = 0;
for ( j = 0; j < 10; j++ )
{
if ( ( scores[j] > temp ) && ( marker[j] == 0 ) )
{
temp = scores[j];
marker[j] = 1;
}
}
buffer[i] = temp;
}

for ( i = 0; i < 10; i++ )
{
printf("&#37;i\n", buffer[i]);
}

}```
Output

223
87
56
2
0
0
0
0
0
0

2. What sorting algorithm did you have in mind? I do not recall any of the common ones needing two extra arrays.

Anyway, it would be simpler to just use qsort(), e.g.,
Code:
```#include <stdio.h>
#include <stdlib.h>

int cmp_int(const void *x, const void *y)
{
return *((int*)x) - *((int*)y);
}

int main(void)
{
int i;
int test[10] = {130, 140, 70, 30, 223, 43, 2, 87, 55, 56};
qsort(test, 10, sizeof(int), cmp_int);
for (i = 0; i < 10; ++i)
{
printf("&#37;d\n", test[i]);
}
return 0;
}```

3. thanks laserlight

It seems my approach was wrong, I managed to do it through, my first algorithm woot

Code:
```void sort_numeric(int scores[10])
{

int buffer[10] = { 0,0,0,0,0,0,0,0,0,0 };
int i = 0, j = 0, L = 0, marker = 0;

for ( i = 0; i < 10; i++ )
{
marker = 0;
L = 0;
for ( j = 0; j < 10; j++ )
{
if ( j == 0 )
L = scores[j];

if ( scores[j] > L )
{
L = scores[j];
marker = j;
}
}
buffer[i] = L;
scores[marker] = 0;
}

for ( i = 0; i < 10; i++ )
{
printf("&#37;i\n", buffer[i]);
}

}```
223
140
130
87
70
56
55
43
30
2

4. Note that you wanted your array to be sorted in ascending order, but your result shows that the array was sorted in descending order.

Incidentally, that looks like a variant of selection sort with an extra array, but one that nonetheless alters the original array. It also will not work with negative numbers.

5. forgive me, I meant DESC order.

I'm using an extra array to return the values back to the original after sorting, perhaps theres a more efficient way like excluding the second array which I've yet to overcome.

Code:
```void sort_numeric(int data[10])
{

// L = largest, M = Marker
int buffer[10] = { 0,0,0,0,0,0,0,0,0,0 };
int i = 0, j = 0, L = 0, M = 0;

for ( i = 0; i < 10; i++ )
{
M = 0, L = 0;
for ( j = 0; j < 10; j++ )
{
if ( data[j] > L )
L = data[j], M = j;
}
data[M] = buffer[i], buffer[i] = L;
}

for ( i = 0; i < 10; i++ )
{
data[i] = buffer[i];
}

}```

6. I'm using an extra array to return the values back to the original after sorting, perhaps theres a more efficient way like excluding the second array which I've yet to overcome.
Okay, I think you tried hard enough. Here's an example implementation of an in-place selection sort that sorts ints in descending order:
Code:
```#include <stdio.h>
#include <stdlib.h>

/* Selection sort in descending order. */
void sort_numeric(int scores[], size_t size)
{
size_t i;

if (size == 0) return; /* Do nothing for a zero-sized array. */

for (i = 0; i < size - 1; ++i)
{
int temp;

/* Search for largest number in the remaining part of the array. */
size_t index_max = i;
size_t j;
for (j = i + 1; j < size; ++j)
{
if (scores[j] > scores[index_max])
{
index_max = j;
}
}

/* Swap current element with largest remaining. */
temp = scores[i];
scores[i] = scores[index_max];
scores[index_max] = temp;
}
}

int main(void)
{
int i;
int test[10] = {-130, 140, 70, 30, 223, 43, 2, 87, 55, 56};

sort_numeric(test, 10);

for (i = 0; i < 10; ++i)
{
printf("&#37;d\n", test[i]);
}

return 0;
}```
You probably could find other (better?) examples on the Web if you searched hard enough.

7. Originally Posted by Cero.Uno
I'm using an extra array to return the values back to the original after sorting, perhaps theres a more efficient way like excluding the second array which I've yet to overcome.
In that case, just don't sort in-place. Make a copy of the items into a second array first which you can happily throw away afterwards, or use an algorithm that doesn't operate in-place such. You could still use a form of selection sort where you would find the next smallest item each time and copy it into the spot in the temporary sorted array instead of doing any swapping.