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

  1. #1
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26

    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++) {
            printf("\nPlease insert numbers: ");
            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. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Suggestion, write a function to swap the two locations.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Elaborate sir!

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

  4. #4
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Is it a syntax error? The sort that will compile but mess up the whole thing runtime?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    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...
    Last edited by Sokion; 06-22-2012 at 07:15 AM.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Aww thanks!

  9. #9
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by Sokion View Post
    The code compiles very well without a single warning.
    I suggest you check your compiler, then, or your copy-pasting, or possibly your eyes.

    Where do you return from main, for example?

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

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  10. #10
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Quote Originally Posted by ledow View Post
    I suggest you check your compiler, then, or your copy-pasting, or possibly your eyes.

    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. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    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

  12. #12
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Quote Originally Posted by laserlight View Post
    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. #13
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    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++) {
            printf("\nPlease insert numbers: ");
            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. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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++) {
            printf("\nPlease insert numbers: ");
            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;
    }
    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

  15. #15
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Thanks, will do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-31-2012, 01:11 AM
  2. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  3. Insertion sort and Quicksort
    By Kae in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2006, 12:34 AM
  4. Replies: 2
    Last Post: 05-06-2003, 08:34 AM