zoukankan      html  css  js  c++  java
  • 内部排序之简单选择排序

    1. 算法原理

      简单选择排序(Simple Selection Sort)的实质为,从待排序序列A[i…n]中,选取关键字最小的元素A[x](i<x<=n),放置于已排序序列A[1…i]的末尾位置(A[i])上,即将第i小的元素放置在位置i上。

      

    2. 稳定性

      

      排序完成之后,25和25*两个元素的前后位置发生变化,故简单选择排序是不稳定的。

    3. 时间复杂度

      简单选择排序的比较次数与初始排序方式无关,若待排序序列中所含元素的个数为N,则比较次数为1+2+…+n-1+n=n*(n-1)/2≈n^2/2,时间复杂度为O(n^2)。

    4. 算法程序

    function sortedArray = simpleSelectionSort(array, number, sortKind)
    
    % Simple selection sort.
    %
    % input - array : disordered array.
    %         number : the number of array elements.
    %         sortKind : kind of sort (1 - positive and 0 -negative).
    %
    % output - sortedArray : sorted array.
    
    if (number <= 0)
        error('The disordered array empty.');
    end
    
    if (number > 1)
        % the number of disordered array is more than 1.
        for index = 1 : number - 1
            % index of the smallest element.
            indexSelect = index;
            
            for i = index + 1 : number
                if (sortKind == 1)
                    % positive
                    if (array(indexSelect) > array(i))
                        indexSelect = i;
                    end
                end
                
                if (sortKind == 0)
                    % negative
                    if (array(indexSelect) < array(i))
                        indexSelect = i;
                    end
                end
            end
            
            if (indexSelect ~= index)
                % exchange elements.
                tempElement = array(index);
                array(index) = array(indexSelect);
                array(indexSelect) = tempElement;
            end
        end
    end
    
    sortedArray = array;
    

    5. 视觉直观感受

      

  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/liekkas0626/p/5227962.html
Copyright © 2011-2022 走看看