Like Tree7Likes
  • 1 Post By stahta01
  • 1 Post By stahta01
  • 1 Post By stahta01
  • 1 Post By ledow
  • 1 Post By laserlight
  • 2 Post By laserlight

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

This is a discussion on Need help with algorithm to sort single array (without using quicksort) within the C Programming forums, part of the General Programming Boards category; Well, I am trying to sort an array of ten numbers in ascending and descending orders. The code compiles very ...

  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
    2,521
    Suggestion, write a function to swap the two locations.

    Tim S.
    Sokion likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,521
    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.
    Sokion likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,521
    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.
    Sokion likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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.
    Sokion likes this.

    - 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
    21,461
    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.
    Sokion likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,461
    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;
    }
    Sokion and iMalc like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:02 AM
  3. Insertion sort and Quicksort
    By Kae in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 11:34 PM
  4. Replies: 2
    Last Post: 05-06-2003, 08:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21