# Name of sorting method

• 10-05-2006
noodles
Name of sorting method
What is the name of the following sorting method?

Code:

```for (i=0; i<n-1; i++)     for (j=i+1; j<n; j++)         if (a[i]>a[j])             swap(&a[i], &a[j]);```
It seems to be the opposite of bubble sort, with lighter elements going in their proper positions first. However, this one does not compare a[j] with a[j+1], as the normal bubble sort does.

Looks very simple, and I wonder why I couldn't find it anywhere on the net!
• 10-05-2006
whiteflags
> lighter elements going in their proper positions first
or larger elements being pushed down - this is the definition of a bubble sort because the items percolate to their proper places. That and the code looks like a bubble sort to me.
• 10-06-2006
Salem
• 10-06-2006
risby
Quote:

Originally Posted by noodles
What is the name of the following sorting method?

Code:

```for (i=0; i<n-1; i++)     for (j=i+1; j<n; j++)         if (a[i]>a[j])             swap(&a[i], &a[j]);```
It seems to be the opposite of bubble sort, with lighter elements going in their proper positions first. However, this one does not compare a[j] with a[j+1], as the normal bubble sort does.

Looks very simple, and I wonder why I couldn't find it anywhere on the net!

As you say, bubble sort swaps adjacent elements. It also sets a flag to show that a swap has taken place and repeats until the flag remains unset on a pass through the array.

This is a Shell sort that gets the first element from wherever it is in the rest of the array and then gets the second element, etc. no need for a flag and usually more efficient.
• 10-06-2006
Prelude
>As you say, bubble sort swaps adjacent elements.
Not necessarily.

>It also sets a flag to show that a swap has taken place and repeats until the flag remains unset
Again, not necessarily.

>This is a Shell sort
It's most certainly not a Shell sort. If Shell sort were so trivial, life would be grand indeed.
• 10-07-2006
risby
Quote:

Originally Posted by Prelude
>This is a Shell sort
It's most certainly not a Shell sort.

Oh yeah ... it's that other one init ... I'm always getting those two confused ... um ... ?
• 10-07-2006
whiteflags
Quote:

Originally Posted by risby
Oh yeah ... it's that other one init ... I'm always getting those two confused ... um ... ?

[guesses] It's not an insert sort either. [/guesses]
It's a bubble sort, just like I said.