Array sort

This is a discussion on Array sort within the C Programming forums, part of the General Programming Boards category; Hi I have this code but i can't write the numbers sorted, what's wrong?? Code: #include <stdio.h> #include <stdlib.h> #include ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    154

    Array sort

    Hi

    I have this code but i can't write the numbers sorted, what's wrong??
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define TAM 100
    
    void LeVetor(int num[])
    {
        int i, n, tmp;
        srand(time(NULL));
        // Inicia o array
        for(i = 0;i < TAM;++i)
            num[i] = i;
        // Embaralha o array
        for (i = TAM; i > 1;)
        {
            n = rand() % i;
            --i;
            tmp = num[n];
            num[n] = num[i];
            num[i] = tmp;
        }
        // Iteração através do array. Os números já estão random
        for(i = 0;i < 4;++i)
            printf("%d\n", num[i]);
    }
    
    
    void OrdenaVetor(int num[])
    {
        int aux, i=0, j=0;
        for(i=0; i<TAM; i++){
            for(j=0; j<TAM; j++)
            {
                if(num[i]<num[j])
                {
                    aux=num[i];
                    num[i]=num[j];
                    num[j]=aux;
                }
            }
        }
    }
    
    
    int main(void)
    {
        int num[TAM];
        int i;
        LeVetor(num);
        printf("\n \n \n");
        OrdenaVetor(num);
        for(i=0; i<4; i++)
        {
            printf("%d", num[i]);
        }
    
    
    }
    I want to have 4 numbers random, this is working, then i want another printf with this 4 numbers sorted.

    What's wrong with this, i have
    Random number
    37
    94
    38
    5

    Sorted
    0123
    it should be
    5
    37
    38
    94

    any help?

    Thnaks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Please post the algorithm name; you are trying to use to sort.

    Your code matches none of the ones I know.

    Link to bubble sort the one normally taught first. Bubble sort - Wikipedia, the free encyclopedia

    Tim S.
    "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
    Join Date
    Apr 2012
    Posts
    154
    Quote Originally Posted by stahta01 View Post
    Please post the algorithm name; you are trying to use to sort.
    Code:
    void bubbleSort(int v[], int n)  
    {  
      int i, j, temp; 
      for (i = n; i > 0; i--)  
        for (j = 1; j <= i; j++) 
          if(v[j-1] > v[j]){ 
                    temp = v[j-1]; 
                    v[j-1] = v[j]; 
                    v[j] = temp; 
                    } 
    }
    Last edited by Gil Carvalho; 05-17-2012 at 09:32 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Does it now sort correctly since you changed the code?

    Tim S.
    "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

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    No;

    Gives me the same number unsorted....

    What's wrong?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Re-wrote your main your logic bug was in it.

    Look at the changes and figure out what you did wrong.

    NOTE: The code was working right you interpreted [the result] as being wrong.

    Code:
    int main(void)
    {
        int num[TAM];
        int i;
        LeVetor(num);
        printf("\n \n \n");
        bubbleSort(num, 4);
        for(i=0; i<4; i++)
        {
            printf("%d\n", num[i]);
        }
    
    
        return 0;
    }
    Oops, found bug in bubble sort.
    NOTE: Normally experienced programers try to avoid using "<=" when "<" works.
    Because most of the times using "<=" or ">=" has resulted in fence post errors based on past experience.
    Look-up fence post errors. http://en.wikipedia.org/wiki/Off-by-one_error

    Code:
    void bubbleSort(int v[], int n)
    {
      int i, j, temp;
      for (i = n; i > 0; i--)
        for (j = 1; j < i; j++)
          if(v[j-1] > v[j]){
                    temp = v[j-1];
                    v[j-1] = v[j];
                    v[j] = temp;
                    }
    }
    Last edited by stahta01; 05-17-2012 at 10:10 AM.
    "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

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    I have already see


    Code:
    for (j = 1; j <= i; j++)
    And you

    Code:
    for (j = 1; j < i; j++)

    Why did you use

    bubbleSort(num, 4);

    ??

    Many thanks

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Quote Originally Posted by Gil Carvalho View Post

    Why did you use

    bubbleSort(num, 4);
    Because you thought 0123 was the wrong result.

    Tim S.
    Last edited by stahta01; 05-17-2012 at 10:57 AM.
    "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

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    If you have an 100 element array, is arrayName[100] a valid location to use?

    Answer: no.

    That is why I changed it to < instead of <=.
    i is set to 100 the first time; which results in j being 100 once if "<=" is used.

    Tim S.
    Last edited by stahta01; 05-17-2012 at 11:00 AM.
    "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

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Note: When testing sorting routines it is good to test with all the inputs the same value at least once.
    If you write, the routine wrong it could result in a endless loop for that input set.

    I did not try that with your sorting routine.

    Other things good to test are:
    Already sorted input.
    Input with reverse sorted data set.
    data set with zero, one, two, or three elements in the data set

    Tim S.
    "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

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Quote Originally Posted by stahta01 View Post
    Note: When testing sorting routines it is good to test with all the inputs the same value at least once.
    If you write, the routine wrong it could result in a endless loop for that input set.

    I did not try that with your sorting routine.

    Other things good to test are:
    Already sorted input.
    Input with reverse sorted data set.
    data set with zero, one, two, or three elements in the data set

    Tim S.
    Ok Tim

    I will try to be more concentrated next time and test with your picks

    Thanks

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Another question please;

    How can i put this code into a c file, the c file has already an int main, i want to call this when the user choose the option to sort the array??

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Quote Originally Posted by Gil Carvalho View Post
    Another question please;

    How can i put this code into a c file, the c file has already an int main, i want to call this when the user choose the option to sort the array??
    I really do NOT see the problem; this assumes you know how to edit code.

    You either cut and paste the code or do a multiple file project.

    NOTE: There can only be a single main function in a C project; so, do NOT try it with two or more main functions.

    NOTE: If this is a question about how to do a multiple file project; you need to post the IDE name and possibly the Compiler/Linker used.

    Tim S.
    Last edited by stahta01; 05-17-2012 at 03:51 PM.
    "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

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Quote Originally Posted by stahta01 View Post
    I really do NOT see the problem; this assumes you know how to edit code.

    You either cut and paste the code or do a multiple file project.

    NOTE: There can only be a single main function in a C project; so, do NOT try it with two or more main functions.

    NOTE: If this is a question about how to do a multiple file project; you need to post the IDE name and possibly the Compiler/Linker used.

    Tim S.
    Thanks Tim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to sort array??plz help
    By hassan_shohag in forum C Programming
    Replies: 2
    Last Post: 07-13-2010, 12:16 PM
  2. Replies: 1
    Last Post: 01-26-2010, 08:02 AM
  3. Replies: 4
    Last Post: 12-06-2009, 11:27 AM
  4. Array sort
    By freddyvorhees in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 01:18 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM

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