Thread: Problem sorting numeric array.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    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
    Last edited by Cero.Uno; 04-22-2008 at 11:27 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    Talking

    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    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. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Cero.Uno View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  4. 3D array problem
    By rainman39393 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2008, 06:09 PM
  5. array problem?
    By ssjnamek in forum C Programming
    Replies: 14
    Last Post: 02-08-2006, 06:03 PM