常用排序算法—Selection Sort(选择排序)

The idea of the selection sort is to find the smallest element in the list and exchange it with the element in the first position. Then, find the second smallest element and exchange it with the element in the second position, and so on until the entire array is sorted.selection

 

void SlectionSort(int *array, int number_of_elments)
{
    int i,j,index_of_min;
    int temp;
    for (i=0; i<number_of_elments; i++)
    {
         /*minIndex denotes the index which has the minimum value which for now assumed to be 
         the vlaue at current index and we update it in the for loop given below                 */
        for (j=i+1; j<number_of_elments-1; j++)
        {
            if (array[index_of_min]>array[j])
            {
                 /* If some index has got element smaller than minimum then update                                
                 minindex  to be that index*/
                index_of_min=j;
            }
        }
        temp = array[index_of_min];
        array[index_of_min]=array[i];
        array[i] =temp;
    }
}
Properties:

Best case performance – When the list is already sorted O(n2).

Worst case performance - When the list is sorted in reverse order O(n2).

Average case performance – O(n2).

It does not require any extra space for sorting, hence O(1) extra space.

It is not stable.

O(n2) time complexity makes it difficult for long lists.

posted @ 2013-03-21 21:49  Gigbit  阅读(272)  评论(0编辑  收藏  举报