Let's take a look at the bubble sort of an array-based extension in Swift, choose sort and quick sort.
1. Bubble sort
Bubble sort again basic but, here is no longer talk about its principle, really can not see Baidu encyclopedia bubble sort
Since bubble sorting avoids the two data exchanges in the array, write a swap function first
Exchanging data for the I and J two locations in an array
Extension Array {
Fileprivate mutating Funcswap (i:int,j:int) {
Let temp = self[i]
Self[i] = Self[j]
SELF[J] = Temp
}
}
Here's the sort, and it's easy to explain.
MARK:-Bubble sort
/**
* By comparing and exchanging with neighboring elements, the small number is exchanged to the front.
*/
Extension Array whereelement:comparable {
mutating func Bubblesort () {
For I in 0..<self.count-1 {
For j in (I+1...self.count-1). Reversed () {
The latter is smaller than the former, the exchange position, that is, the small upward sinking of the large
If SELF[J] < Self[j-1] {
Swap (i:j, j:j-1)
}
}
}
}
}
Because it is a sort, the element must satisfy the protocol to be comparable.
The same is true for the quick sorting and sorting options below.
2. Quick Sort
The algorithm idea of/** fast sorting
* A quick-sort algorithm is:
* 1, set two variables I, J, the beginning of the sorting: i=0,j=n-1;
* 2, with the first array element as the datum data, assigns the value to the key, namely key=a[0];
* 3, starting from J forward search, that is, starting from the forward search (J minus 1), find the first value less than key A[j], will a[j] and A[i] interchange;
* 4, from I start backward search, that is, start backward search (i++), find the first greater than key A[i], will a[i] and A[j] interchange;
* 5, repeat the 3rd, 4, until i=j, (3,4 step, did not find the matching criteria, that is, 3 a[j] is not less than the key,4 a[i] when the value of J, I is not greater than key,
* Make j=j-1,i=i+1 until you find it. Locate the value that matches the condition, and the J pointer position does not change when I exchange it.
* In addition, I==J this process must be exactly when the i+ or J is complete, at which time the loop ends).
*/
Extension Array where element:comparable {
Private Mutatingfunc quickpartition (Left:int, right:int)->int {
var right = right
var left = Left
Record which is the base number
Let base = Self[left]
Record base number position
Let BaseIndex = Left
First scan from the right to the left, find the first number smaller than base, but not to meet
While left < right&& Self[right] >= Base {
right = Right-1
}
Then scan from the left to the right to find the first number larger than base, but not to meet
While left < right&& Self[left] <= Base {
left = left+1
}
Swap the number on the left that is larger than base and the lower right than base
Swap (I:left,j:right)
Swap the number and base number on the left that are larger than base
Swap (I:baseindex,j:left)
Returns the new base number
return left
}
Quick Sort
-Parameters:
-A: Array a
-Left: Index
-Right: Index
privatemutating func quickSort (Left:int, Right:int) {
Sort finished, exit recursion
If left >= right {
Return
}
Each trip is divided so that the left side is smaller than the datum, the right one is larger than the base, and the position of the new datum is returned.
Letbaseindex = Quickpartition (Left:left, Right:right)
Determine if the left side of the line is complete, not the left part of the recursive row
If BaseIndex-1 > left {
QuickSort (Left:left, right:baseindex-1)
}
Determine if the right side of the line is complete, not the right part of the recursive row
If BaseIndex + 1 < Right {
QuickSort (Left:baseindex + 1, right:right)
}
}
mutating func QuickSort () {
QuickSort (left:0, right:self.count-1)
}
}
3. Select sort
MARK:-Select sort
/** Selection Sorting algorithm idea
* Each trip will look for the lowest value index (subscript) from the go, and finally by comparing whether the exchange is required. Each trip will have the smallest element exchanged to the front.
* Approximate process
* 6, 4, 9, 7, 5 (START)
* 4, 6, 9, 7, 5 (First trip: Swap the minimum 4 with 6 to make this trip minimum 4 to the front)
* 4, 5, 9, 7, 6 (Second trip: Swap the minimum 5 with 6 to make this trip minimum 5 to the front)
* 4, 5, 6, 7, 9 (third trip: Swap the minimum 6 with 9 to make this trip minimum 6 to the front)
* 4, 5, 6, 7, 9 (Fourth trip: No swap required, sort done)
*/
Extension Array where element:comparable {
Mutating Funcselectorsort () {
var min =0
Only need to n-1 a trip, to the last trip only one element, must be the smallest
For I in 0..<self.count-1 {
At the beginning of each trip, suppose that the first element of the trip is the smallest
Min = i
Look for the trip there is no smaller, if find smaller, then update the minimum value subscript
For J in I+1..<self.count-1 {
If SELF[J] < Self[min] {
Min = J
}
}
If the first element of the trip is not the smallest, it is necessary to exchange
If min! = i {
Swap (i:min, j:i)
}
}
}
}
Swift's sorting algorithm is summarized here.
If you think this post is well-organized, just give it a little praise. If in the place where there is negligence, but also hope that the Bo friends of the guidance.
Thank you Bo friends for the support of this article Oh!!! Thank you.
Summary of Swift's sorting algorithms