Thread: Help rearranging array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    43

    Help rearranging array

    Hello!

    Imagine i have an array with the following values: 2 4 8 3 1 20.

    I need to be capable of rearranging the values of the array so I can print them in ascending order. How would I do this?

    Regards

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Maybe by sorting them?

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Try a bubble sort:

    Code:
    #include <stdio.h>
    
    #define SIZE 6
    
    void sort(int *array, int size);
    
    void main()
    {
         int num_to_sort[]= {2, 4, 8, 3, 1, 20};
         int i;
         
         printf("Here is the array unsorted: "); 
         
         for(i = 0; i < SIZE; i++)
            printf("%d\t", num_to_sort[i]); 
            
        printf("\nAnd here is the sorted array:\n"); 
        
        sort(num_to_sort, SIZE); 
        
        for( i =0; i < SIZE; i++)
            printf("%d\t", num_to_sort[i]); 
        
    } 
        
        void sort(int *array, int size)
        {
            int a;
            int b; 
            int temp; 
            
            for(a = 0; a < SIZE - 1; a++)
                for (b = a+1; b < SIZE; b++)
                    if(array[a] > array[b]) 
                    {
                        temp = array[b]; 
                        array [b] = array[a]; 
                        array[a] = temp; 
                        
                    } 
        }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rpmischris View Post
    Try a bubble sort:

    * complete solution snipped *
    Yes by all means, the original poster should try a bubble sort. However, it just so happens that what you posted is not a bubble sort!
    The items swapped in bubble sort are always adjacent. What you posted is also sometimes used and does work, but it is not bubble sort. Some call it "substitution sort".

    Also, you are not supposed to just hand out free solutions, even though in this instance code for such an algorithm is easy to find. I very much suspect that this is homework.
    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"

  5. #5

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The C standard library has a function named qsort() that sorts an array. Use it!

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    43
    Thank you everyone for your answers! Jimblumberg, i read the rules before posting a question, and i think it is not against the rules to post question about homework.

    Anyway, this isn't actually any homerwork assignment, although it will be useful for college in the future. I tried to make the question as simple as possible. The array I want to sort is not that one, it is a very big array generated by my already big code that I need to organize so I can analyse it later. I was having serious difficulties sorting it.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Median View Post
    i think it is not against the rules to post question about homework.
    That depends on interpretation, and how you ask, but there is a good chance that questions about homework will run afoul of the site policy on homework, here.

    Whatever your intent, questions about printing things in ascending order are pretty common homework questions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @rpmischris:
    That is my favorite little Substitution sort! I memorized it very early on, but over the years, I forgot the name of it, and started calling it Bubble sort, as well.

    It's not Bubble sort, but it has very similar look and run-time.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Quote Originally Posted by Median View Post
    Jimblumberg, i read the rules before posting a question, and i think it is not against the rules to post question about homework.
    Did I say anything about you the rules? You asked simply: "I need to be capable of rearranging the values of the array so I can print them in ascending order. How would I do this?" And I suggested that you look at sorts. With the amount of information you provided I thought that the answer matched the question.

    Quote Originally Posted by Median View Post
    Anyway, this isn't actually any homerwork assignment, although it will be useful for college in the future. I tried to make the question as simple as possible. The array I want to sort is not that one, it is a very big array generated by my already big code that I need to organize so I can analyse it later. I was having serious difficulties sorting it.
    If you would have stated this in your first post you may have gotten a different answer. But when you don't provide any details answers will probably match the detail level of your question. Now if your array is really large and there is no requirement as to the type of sort or that you implement your own sort method, I recommend the previously mentioned qsort() method as the first choice.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Randomly rearranging the elements in a vector
    By Signifier in forum C++ Programming
    Replies: 11
    Last Post: 08-01-2007, 12:21 PM
  3. Rearranging Names
    By BJtoVisualcC++ in forum C++ Programming
    Replies: 4
    Last Post: 06-23-2007, 11:34 AM
  4. Rearranging contents of an array
    By jlf029 in forum C++ Programming
    Replies: 9
    Last Post: 11-08-2005, 12:34 AM
  5. Rearranging a string... and a glitch
    By Trauts in forum C++ Programming
    Replies: 17
    Last Post: 10-29-2002, 05:11 PM

Tags for this Thread