Sorting Algorithms swift :( f) quick sort (bubble algorithm improved version)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/lin1109221208/article/details/90694078

1 Overview

Quick Sort is an improvement on the bubble algorithm.

Thought merge fast sequence sorting sort similar, are by way of divide and conquer sort, the basic idea is to trip sort the data to be sorted into two independent parts, wherein all of the data to another portion of the part of the data than are to small, then this method to quickly sort the data two parts, respectively, over the entire car can be sorted recursively until the entire data orderly.

2, algorithm principle

Rationale: take the first random value series, and then put forward by comparing the value smaller than the value of the element, the element value larger than the rear side into the value of the first half and are then and the rear half portion of the above operations.

step:

1) out of a number of elements from the column, as a reference pivot

2) reordering columns, all smaller than the reference baseline on the front, in all the larger than the reference standard (the same number can either side) to exit after the partition, the reference position is in the middle of the series, called partition partition operation

3) of the recursive recursive than the reference value and the number of columns a value larger than the reference number of sub-column sorting

Quick trip to the sort of steps:

  1) Set two variables i, j, at the beginning of the sort: i = 0, j = N-1;

2) to the first array element as the reference data, assigned to the Point, i.e. point = A [0];

3) Search forward starting from j, i.e., the forward search starting from the (j minus 1), find the first point is smaller than the value of A [j], the A [j] and A [i] are interchangeable;

4) start from the back search i, i.e., before the start of the backward search (i ++), find the first A [i] is greater than the point of the A [i] and A [j] interchangeable;

5) Repeat steps 3 and 4 until i = j; (3,4 step, did not find qualified value, i.e. 3 A [j] is not less than the key, in. 4 A [i] is not greater than the time key changing j, the value of i,

     Such that j = j-1, i = i + 1, until you find. Time to find qualified value, exchange of i, j pointer position unchanged.

    Further, i == j when this process must exactly i + j- or completed, so that at this time the end of the cycle).

3, for example

To [5, 7, 1, 8, 4] fast row

1) Select the reference point = 5, less than 5 5 placed before, after greater than 5 on 5, Low responsible for traversing the first part, placed behind the front portion is greater than 5, High responsible traverse the latter half of the the front portion is less than 5 after the discharge, the following is a schematic diagram of a trip fast row

2) the need for separately performing the fast row on both sides of the array point, the entire array ordered so far known

4, algorithm

func quickSort(_ array : inout [Int], _ low : Int, _ high : Int){
        if low >= high {
            return
        }
        var i = low
        var j = high
        
        //基准值
        let point = array[i]
        while i<j {
            //从右边开始找,要是找到右边有比point小的,交换他们的位置
            //下面的循环,当找到array[i]<point时结束
            while i<j && array[j]>=point{
                j -= 1
            }
            //循环结束,也就是找到array[j]<point,交换他们的位置
            array[i] = array[j]
            
            //2 从左开始找,此时是i增加,当找到array[i]>point时交换他们的位置
            while i<j && array[i]<=point{
                i += 1
            }
            array[j] = array[i]
        }
        
        //每一次排序比较完成后基准数的位置,按这个位置切分数组
        array[i] = point
        
        //递归处理基准左右的子数列
        quickSort(&array, low, i-1)
        quickSort(&array, i+1, high)
        
    }



调用:
var array6 = [5, 7, 1, 8, 4]
quickSort(&array6, 0, array6.count-1)
print(array6,"\n")


运行结果:

[1, 4, 5, 7, 8] 

5, the time complexity

Quick drain of the time complexity is the time complexity of O (nlogn)

 

github Code

Note: The sort of specific implementation code in  SortSummary.swift  calling file is in ViewController.swift

Guess you like

Origin blog.csdn.net/lin1109221208/article/details/90694078