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
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
Maybe by sorting them?
Jim
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; } }
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"
Understand bubble sort here
The C standard library has a function named qsort() that sorts an array. Use it!
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.
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.
@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.
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.
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