Using an array to sort another array.

Write a function to rank (in a separate array) a set of n integer scores from low to high. For instance, array[order[0]] should contain the smallest value.

Im currently stuck at trying to sort this array because it keeps looping the smallest two numbers in the array. Is there a way to tell the program to skip the previous found smallest number or tell it to skip the number if it is smaller than the previous for loop run?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>

void sortSelect(int arr[], int num, int order[]);

int main (void)
{
    int arr[25];
    int order[25];

    for(int i=0;i<25;i++)
    {
        arr[i]=rand();
        printf("\n%d", arr[i]);
    }
    printf("\n");
    sortSelect(arr,25,order);


    return 0;
}


void sortSelect(int arr[], int num, int order[])
{
    int current;
    int walker;
    int smallest;


    for (current = 0; current < num - 1; current++)
    {
        smallest = current;

        for (walker = current;walker < num; walker ++)
          {
                if (arr[walker] < arr[smallest])

                {

                    smallest = walker;
                    if(smallest == order[current-1])
                        smallest = walker - 1;
                }

          }


    order[current]=smallest;

    printf("\n%d", arr[order[current]]);
    }

  return;
}

There are many sorting algorithms out there, but I think making one yourself if much more fun. One question, what is walker? That's not any term I'd use...

Now, when developing an algorithm of any sort, I think it's best to just sit down and think about and write down how it is supposed to work before even touching a computer. Another thing that helps when your program doesn't do what you'd like, is to write down the value of each variable through each loop. I found I find most of my mistakes this way.
Walker is a term used in the book I'm reading for programming, i guess its his interpretation of literately walking down an array while testing it against a point you set as an index. Thanks for the advice, think ill get some pen and paper out. ;p
It may sound silly at first, I know it did for me. But it works. If you have any trouble, feel free to ask or even look up the various sorting algorithms out there for a point in the right direction.
In line 42, is the code trying to reach the array order, which is uninitialized?
solved it by using arr[order[walker]] > arr[order[current]] and swapping those. And yah, I had to initialize the second array.
Topic archived. No new replies allowed.