Skip to content

Commit ddcfd3d

Browse files
Merge pull request wangzheng0822#77 from jerryderry/master
merge_sort and quick_sort in python
2 parents 729f0de + b0b538b commit ddcfd3d

File tree

7 files changed

+238
-1
lines changed

7 files changed

+238
-1
lines changed

python/11_sorts/sorts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
def bubble_sort(a: List[int]):
1212
if len(a) <= 1: return
1313

14-
made_swap = False
1514
for i in range(len(a)):
15+
made_swap = False
1616
for j in range(len(a) - i - 1):
1717
if a[j] > a[j+1]:
1818
a[j], a[j+1] = a[j+1], a[j]

python/12_sorts/merge_sort.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Author: Wenru
3+
"""
4+
5+
from typing import List
6+
7+
def merge_sort(a: List[int]):
8+
_merge_sort_between(a, 0, len(a)-1)
9+
10+
def _merge_sort_between(a: List[int], low: int, high: int):
11+
# The indices are inclusive for both low and high.
12+
if low >= high: return
13+
mid = low + (high - low)//2
14+
_merge_sort_between(a, low, mid)
15+
_merge_sort_between(a, mid+1, high)
16+
17+
_merge(a, low, mid, high)
18+
19+
def _merge(a: List[int], low: int, mid: int, high: int):
20+
# a[low:mid], a[mid+1, high] are sorted.
21+
i, j = low, mid+1
22+
tmp = []
23+
while i <= mid and j <= high:
24+
if a[i] <= a[j]:
25+
tmp.append(a[i])
26+
i += 1
27+
else:
28+
tmp.append(a[j])
29+
j += 1
30+
start = i if i <= mid else j
31+
end = mid if i <= mid else high
32+
tmp.extend(a[start:end+1])
33+
a[low:high+1] = tmp
34+
35+
36+
if __name__ == "__main__":
37+
a1 = [3, 5, 6, 7, 8]
38+
a2 = [2, 2, 2, 2]
39+
a3 = [4, 3, 2, 1]
40+
a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
41+
merge_sort(a1)
42+
print(a1)
43+
merge_sort(a2)
44+
print(a2)
45+
merge_sort(a3)
46+
print(a3)
47+
merge_sort(a4)
48+
print(a4)

python/12_sorts/quick_sort.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Author: Wenru
3+
"""
4+
5+
from typing import List
6+
import random
7+
8+
def quick_sort(a: List[int]):
9+
_quick_sort_between(a, 0, len(a)-1)
10+
11+
def _quick_sort_between(a: List[int], low: int, high: int):
12+
if low >= high: return
13+
# get a random position as the pivot
14+
k = random.randint(low, high)
15+
a[low], a[k] = a[k], a[low]
16+
17+
m = _partition(a, low, high) # a[m] is in final position
18+
_quick_sort_between(a, low, m-1)
19+
_quick_sort_between(a, m+1, high)
20+
21+
def _partition(a: List[int], low: int, high: int):
22+
pivot, j = a[low], low
23+
for i in range(low+1, high+1):
24+
if a[i] <= pivot:
25+
j += 1
26+
a[j], a[i] = a[i], a[j] # swap
27+
a[low], a[j] = a[j], a[low]
28+
return j
29+
30+
31+
if __name__ == "__main__":
32+
a1 = [3, 5, 6, 7, 8]
33+
a2 = [2, 2, 2, 2]
34+
a3 = [4, 3, 2, 1]
35+
a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
36+
quick_sort(a1)
37+
print(a1)
38+
quick_sort(a2)
39+
print(a2)
40+
quick_sort(a3)
41+
print(a3)
42+
quick_sort(a4)
43+
print(a4)

swift/12_sorts/.DS_Store

6 KB
Binary file not shown.

swift/12_sorts/QuickSort.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// QuickSort.swift
3+
// algo
4+
//
5+
// Created by Wenru Dong on 2018/10/17.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public func quickSort<T: RandomAccessCollection & MutableCollection>(_ a: inout T) where T.Element: Comparable {
12+
quickSort(&a, from: a.startIndex, to: a.index(before: a.endIndex))
13+
}
14+
15+
fileprivate func quickSort<T: RandomAccessCollection & MutableCollection>(_ a: inout T, from low: T.Index, to high: T.Index) where T.Element: Comparable {
16+
if low >= high { return }
17+
18+
let m = partition(&a, from: low, to: high)
19+
quickSort(&a, from: low, to: a.index(before: m))
20+
quickSort(&a, from: a.index(after: m), to: high)
21+
}
22+
23+
fileprivate func partition<T: RandomAccessCollection & MutableCollection>(_ a: inout T, from low: T.Index, to high: T.Index) -> T.Index where T.Element: Comparable {
24+
let pivot = a[low]
25+
var j = low
26+
var i = a.index(after: low)
27+
while i <= high {
28+
if a[i] < pivot {
29+
a.formIndex(after: &j)
30+
a.swapAt(i, j)
31+
}
32+
a.formIndex(after: &i)
33+
}
34+
a.swapAt(low, j)
35+
return j
36+
}

swift/12_sorts/SortsTests.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// SortsTests.swift
3+
// SortsTests
4+
//
5+
// Created by Wenru Dong on 2018/10/14.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
class SortsTests: XCTestCase {
12+
13+
override func setUp() {
14+
super.setUp()
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDown() {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
super.tearDown()
21+
}
22+
23+
func testMergeSort() {
24+
var a1 = [1, 1, 1, 1]
25+
mergeSort(&a1)
26+
XCTAssertEqual(a1, [1, 1, 1, 1])
27+
28+
var a2 = [4, 3, 2, 1]
29+
mergeSort(&a2)
30+
XCTAssertEqual(a2, [1, 2, 3, 4])
31+
32+
var a3 = [3, 6, 9, 7, 8, -1, 9, 3, -2, 0]
33+
mergeSort(&a3)
34+
XCTAssertEqual(a3, [-2, -1, 0, 3, 3, 6, 7, 8, 9, 9])
35+
}
36+
37+
func testQuickSort() {
38+
var a1 = [1, 1, 1, 1]
39+
quickSort(&a1)
40+
XCTAssertEqual(a1, [1, 1, 1, 1])
41+
42+
var a2 = [4, 3, 2, 1]
43+
quickSort(&a2)
44+
XCTAssertEqual(a2, [1, 2, 3, 4])
45+
46+
var a3 = [3, 6, 9, 7, 8, -1, 9, 3, -2, 0]
47+
quickSort(&a3)
48+
XCTAssertEqual(a3, [-2, -1, 0, 3, 3, 6, 7, 8, 9, 9])
49+
}
50+
51+
func testPerformanceExample() {
52+
// This is an example of a performance test case.
53+
self.measure {
54+
// Put the code you want to measure the time of here.
55+
}
56+
}
57+
58+
}

swift/12_sorts/mergeSort.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// sorts.swift
3+
// algo
4+
//
5+
// Created by Wenru Dong on 2018/10/14.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public func mergeSort<T>(_ a: inout T) where T: RandomAccessCollection, T: MutableCollection, T.Element: Comparable {
12+
mergeSort(&a, from: a.startIndex, to: a.index(before: a.endIndex))
13+
}
14+
15+
fileprivate func mergeSort<T>(_ a: inout T, from low: T.Index, to high: T.Index) where T: RandomAccessCollection, T: MutableCollection, T.Element: Comparable {
16+
if low >= high { return }
17+
let dist = a.distance(from: low, to: high)
18+
let mid = a.index(low, offsetBy: dist/2)
19+
mergeSort(&a, from: low, to: mid)
20+
mergeSort(&a, from: a.index(mid, offsetBy: 1), to: high)
21+
merge(&a, from: low, through: mid, to: high)
22+
}
23+
24+
fileprivate func merge<T>(_ a: inout T, from low: T.Index, through mid: T.Index, to high: T.Index) where T: RandomAccessCollection, T: MutableCollection, T.Element: Comparable {
25+
var i = low
26+
var j = a.index(mid, offsetBy: 1)
27+
var tmp = Array<T.Element>()
28+
tmp.reserveCapacity(a.distance(from: low, to: high) + 1)
29+
while i <= mid && j <= high {
30+
if a[i] <= a[j] {
31+
tmp.append(a[i])
32+
a.formIndex(after: &i)
33+
} else {
34+
tmp.append(a[j])
35+
a.formIndex(after: &j)
36+
}
37+
}
38+
39+
var start = i
40+
var end = mid
41+
if j <= high {
42+
start = j
43+
end = high
44+
}
45+
tmp.append(contentsOf: a[start...end])
46+
47+
var current = low
48+
for element in tmp {
49+
a[current] = element
50+
a.formIndex(after: &current)
51+
}
52+
}

0 commit comments

Comments
 (0)