Sorting (on): How to analyze a "sorting algorithm"?

How to analyze a "sorting algorithm"?

effectiveness

For sorting algorithm efficiency analysis will generally be measured in several ways:

1. best case, worst case, where the average time complexity

For the degree of order to sort the data of different sorting algorithms in performance would be affected, so the need to analyze the time complexity under different circumstances

2. The complexity factor time constant, low-level

Reaction time complexity of the code execution time trends in data with the scale of growth in the actual software development, data sorting scale may be small, when comparing the performance of the same order of time sorting algorithm complexity, you need to factor constant, low-level is also taken into account together.

3. Comparison of the exchange and the number (or mobile) number

Memory consumption

Algorithm memory consumption can be measured by the spatial complexity, the spatial complexity is O (1) sorting algorithm is called " in situ sort ."

stability

Stability of elements equal to the value described in the present sequence to be sorted, after sorting, the order is equal to the original constant between the elements.

 

A bubble sort

Thinking algorithm: each time comparing two adjacent elements, the size relationship is determined based on whether needs interchangeable, each element of the at least one bubble will move to the position that it should be repeated n times until the sorting is completed.

        public  static  void bubbleSort ( int [] iArray) 
        { 
            IF (iarray.Length <= . 1 ) return ; 

            // the last location record exchanged 
            int lastChangeIndex = 0 ;
             // boundary disorder SEQUENCE 
            int sortBorder iarray.Length = - . 1 ; 
int length = iarray.Length -. 1;
for ( int I = 0 ; I <length; I ++ ) { // exit early flag BOOL IsSorted = to true ; for ( int J = 0 ; J <sortBorder; J ++ ) { IF (iArray [J]> iArray [J + . 1 ]) { int tmp = iArray [J]; iArray [J] = iArray [J + . 1 ]; iArray [J + . 1 ] = tmp; IsSorted = to false ; // the border is updated random number sequence position of the last exchange element lastChangeIndex = J; } } ow (isSorted) break ; } }

1. The process involves bubbling compare only adjacent data exchange operation, only a temporary space constant level, so the space complexity is O (1), it is place sorting algorithm .

2. In bubble sort, only the exchange will change around the order of the two elements, when the adjacent two elements are equal, we do not exchange, will not change before and after the order of the elements, it is a stable sorting algorithm .

3. The bubble sort time complexity

If the average time complexity of bubble sort of analysis can be complicated by probabilistic methods, we can use the " degree of order " and " reverse degree to analyze."

The degree of ordering is the number of elements in the array has an ordered pair relationship

In reverse order of the degree of order on the contrary, the number of elements of the reverse relationship

Full degree of ordering is the number of order of the array is completely ordered, n * (n-1)

= Full degree reverse order degree - the degree of order

Bubble sort comprising two compare and swap operation, switching every time, plus a degree of order, regardless of the improved algorithm, the number of exchanges is always determined, namely the degree of reverse, i.e. n * (n-1) / 2- the initial degree of order, the above example is 15-3 = 12 to 12 times the switching operation.

The number of exchanges taking the average case intermediate values, i.e. n (n-1) / 4 , and during the sorting process, the comparison operation is certainly more than the exchange operation, and the complexity of the upper limit of O (n- 2 ), so that the average case time complexity is O (n- 2 ).

 

Second, insertion sort

算法思路:将数组中的数据分为两个区间,已排序区间和未排序区间,初始已排序区间只有一个元素,就是数组的第一个元素,接着取未排序区间中的元素在已排序区间中找到合适的位置将其插入,并保证已排序区间数据一直有序,重复这个过程直到未排序区间中元素为空,算法结束。

 

public static void insertionSort(int[] iarray)
{
    if(iarray.Length<=1)
        return;
    int length = iarray.Length;
    for(int i=1;i<length;i++)
    {
        int value = iarray[i];
        int j=i-1;
        for(;j>=0;j--)
        {
            if(iarray[j]>value)
                iarray[j+1] = iarray[j]; //数据移动
            else
                break;
        }
        //插入数据
        iarray[j+1] = value;
    }
}

1.插入排序算法运行并不需要额外的存储空间,所以空间复杂度是O(1),是原地排序算法

2.插入排序中,对于值相同的元素,我们可以将后面出现的元素插入到前面出现元素的后面,这样就能保证原有的前后顺序不变,所以是稳定排序算法

3.如果待排序的数组是有序的,我们并不需要移动任何数据,从尾到头在有序数据里面查找插入位置,每次只需要比较一个数据就能确定数据的插入位置,所以最好情况时间复杂度为O(n);(由于每次都是将数据插入到有序区间,所以从尾部开始比较数据就可确定数据是否需要交换);如果待排序的数组是无序的,那么每次插入数据都相当于在数组的第一个位置插入新的数据,需要移动大量的数据,所以最坏情况时间复杂度为O(n2)。

在数组中插入一个数据的平均时间复杂度是O(n),对于插入排序来说,每次插入操作都相当于在数组中插入一个数据,循环执行n次插入操作,所以平均时间复杂度为O(n2)。

 

三、选择排序

思路:将待排序数组分为已排序区间和未排序区间,每次从未排序区间中找到最小的元素,将其放到已排序区间的末尾。

public static void insertionSort(int[] iarray)
{
    if(iarray.Length<=1)
        return;
    int length = iarray.Length;
    for(int i=0;i<length-1;i++)
    {
        // 查找最小值
        int minIndex = i;
        for(int j=i+1;j<length;j++)
        {
            if(iarray[j]<iarray[minIndex])
                minIndex = j;
        }
        int tmp = iarray[i];
        iarray[i] = iarray[minIndex];
        iarray[minIndex] = tmp;
    }
}

1.选择排序算法空间复杂度是O(1),是原地排序算法

2.选择排序最好情况、最坏情况、平均情况时间复杂度都是O(n2)。

3.选择排序是不稳定排序算法

 

针对冒泡排序与插入排序的对比,冒泡排序不管如何优化,元素交换的次数是一个固定值,也就是原始数据的逆序度。插入排序也是同样的,不管如何优化,元素移动的次数也等于原始数据的逆序度。

但是从代码实现上来看,冒泡排序的数据交换比插入排序的数据移动要复杂,需要3个赋值操作,而插入排序只需要1个排序操作,假设执行一个赋值语句的时间粗略的计为单位时间,用冒泡排序和插入排序对同一个逆序度为K的数组进行排序。用冒泡排序需要K次交换操作,总计3*K单位时间,而插入排序只需要K个单位时间。

所以在实际软件开发中,会优先选择插入排序。

 

 

Guess you like

Origin www.cnblogs.com/zhengzc/p/11622400.html