How can I arrange numbers in ascending order using if and swap statements? Could someon开发者_如何学编程e give me some direction?
The easiest site to learn sorting algorithms:
http://www.sorting-algorithms.com
For specifically Bubble Sort:
http://www.sorting-algorithms.com/bubble-sort
Have you tried searching for sorting algorithms? For example, wikipedia's article has a table of over a dozen. I'm sure you could find one to your liking.
P.S. The articles for the more common algorithms have beautiful illustrations or animations. Those help me a great deal, besides just being fun to look at.
The classic answer to how to efficiently sort 1 million 32-bit integers is: bubble sort is the wrong way to go. ;)
Just kidding. The classic answer to how to sort things is this video: "Sorting Out Sorting" Given the animations and explanations in that video, writing the code is left as an exercise for the reader.
you can use a bubble sort http://en.wikipedia.org/wiki/Bubble_sort
is this a homework problem?
Sound a lot like homework. Avoid bubble sort though -- insertion sort and selection sort are faster and (at least to me) easier to understand.
Edit: I don't think there's much excuse for dealing with bubble sort, as a learning experience, or much of anything else. AFAICT, the only reason bubble sort ever came to be known at all in the first place is purely a historical accident: back in the '50s, there was a paper that showed that a bubble sort was optimal for a drum computer with two heads, so it had access to two data items at a time, with those two data items working through an array in ascending order (only).
Even that probably wouldn't have been enough for people to remember it, but as it happens, that paper seems to have been pretty much the first one to ever deal with computational complexity as a coherent science. For quite a while after that, bubble sort was the first algorithm studied by anybody interested in what we'd now call computer science, algorithms, computational complexity, or anything very similar.
It's (long past) time for this accident of history to rest in peace.
Try insertion sort; it's fairly simple to understand and implement, seems to fit your requirements and will give you a good understanding of sorting on the way. You should check other sorting algorithms as well, at least if you're not taught a few more (quicksort, mergesort, etc.) in class.
edit: Technically, bubble sort is closer to your requirements (just if and swaps) than insertion sort, so it may be what your instructor had in mind..
Do You mean something like this?
void sort3( int t[3] ){
int m[3];
if( t[0] > t[1] ){
if( t[2] > t[0] ){
m[0] = t[1]; m[1] = t[0]; m[2] = t[2];.
} else {
if( t[2] > t[1] ){
m[0] = t[1]; m[1] = t[2]; m[2] = t[0];.
} else {
m[0] = t[2]; m[1] = t[1]; m[2] = t[0];.
}
}
} else {
if( t[2] > t[1] ){
m[0] = t[0]; m[1] = t[1]; m[2] = t[2];.
} else {
if( t[2] > t[0] ){
m[0] = t[0]; m[1] = t[2]; m[2] = t[1];.
} else {
m[0] = t[2]; m[1] = t[0]; m[2] = t[1];.
}
}
}
memcpy( t, m, sizeof m );
}
Sorting this way 4 elements needs 97 lines, 5 elements needs 513 lines. (I use a perl script to produce the codes.)
精彩评论