views:

560

answers:

7

How can I arrange numbers in ascending order using if and swap statements? Could someone give me some direction?

A: 

you can use a bubble sort http://en.wikipedia.org/wiki/Bubble%5Fsort

is this a homework problem?

John Boker
A: 

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.

Jerry Coffin
Judging by the question it's the first time the OP encounters sorting. In that case, it's natural for him not to avoid bubble sort, if only as a learning experience
Nathan Fellman
There are some things you just don't let a beginner do, for his own good. Operate the laser without wearing protective eyeware, and code a bubble sort, are two of them.
Steve Jessop
+2  A: 

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.

Jefromi
+2  A: 

The easiest site to learn sorting algorithms:

http://www.sorting-algorithms.com

For specifically Bubble Sort:

http://www.sorting-algorithms.com/bubble-sort

John Rasch
1. Or no, not the Bubble Sort again.2. Very cool site. +1
Alexander Pogrebnyak
A: 

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.)

EDIT: sorry, i missed the homework tag.

sambowry
I bet bubble sort looks really good now, eh?
Michael Foukarakis
+2  A: 

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.

Posco Grubb
"Sorting Out Sorting" is great video! Thanks! Just curious: how do you find such a good things?..
avp
Way back in high school in the 90's, my AP computer science teacher was geeky enough to show us "Sorting Out Sorting". I remembered it recently; not remembering the title, I googled "old sorting video". Several hyperlinks later led me to Obama's comment.
Posco Grubb
A: 

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..

Michael Foukarakis