views:

132

answers:

2
int aux;

        for(int i=0;i<array.Count()-1;i++)
        {
            for(int j=i+1;j<array.Count();j++)
            {
                if(array[i] > array[j])
                {
                    aux = array[j];
                    array[j] = array[i];
                    array[i] = aux;
                }
            }
        }
+8  A: 

This is a dumbed down selection sort. Instead of swapping array[i] with the minimum element after it, you just swap it with each smaller element. Eventually the correct element will obviously end up in the right position, and you do write less code.

This is a lot less efficient because more swaps are performed, but it's basically selection sort.

IVlad
Why the downvote?
IVlad
I didn't downvote. Thanks for the answer. This is a piece of code I wrote in Java back in high school and I came up with it when first having to sort something. I didn't know sorting algorithms existed back then or at least the taught never occurred to me. I just found it somewhere and converted it to C#. It does work still.
Para
+1  A: 

This is almost selection sort, except that you don't swap the minimum remaining element with the current element, but you swap every remaining element that's smaller than the current with the current element until the current element is the minimum.

sepp2k