Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning.
- Time : O(n^2), as there are two nested loops
- Space : O(1) (In-place)
1 | def selectionSort(nums): |
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
- Time : O(n^2), Best Case : O(n)
- Space : O(1)
1
2
3
4
5
6def bubbleSort(nums):
for i in xrange(len(nums)):
for j in xrange(len(nums)-i-1):
if nums[j] > nums[j+1]:
nums[j+1], nums[j] = nums[j], nums[j+1]
return nums
Insertion Sort
- Time : O(n^2), Best Case : O(n) when the elements are already sorted!
- Space : O(1)
1 | def insertionSort(nums): |
Merge sort
It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.
- Time : O(nlogn)
- Space : O(n)
88. Merge Sorted Array
Merge Two Sorted Array in Merge Sort Algorithms
- Three Pointers: nums1, nums2 and results
1 | class Solution(object): |
Merge k sorted array
- Time : O(nlogk)
- sol1: Iterative way (Brute Force)
- sol2: Binary Reduction (Divide and Conquer)
- if very large,so data in disk : read and write logk times
- sol3: k pointers smallest element(Heap)
- if very large,so data in disk : read and write only 1 time
Counting Inversions
- based on merge sort, count all the inversion paris!
Quick Sort
It picks an element as pivot and partitions the given array around the picked pivot.
- The key process in quickSort is partition().
Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.
1 | def partition(nums, l, r): |
215. Kth Largest Element in an Array
1 | class Solution(object): |
LintCode 80. Median
1 | class Solution: |
283. Move Zeroes
Maintaining the relative order of the non-zero elements.
1 | class Solution(object): |
1 | class Solution(object): |
Follow up : order can be changed
The order of all other elments can be changed.
- left and right pointer swap each number, it could change the order, but it reduces the operation number!
- this method actually is similar to the partition function in Quick Sort!!!
1 | class Solution(object): |
Rainbow Sort
75. Sort Colors
Sol1. Counting Sort
two-pass algorithm1
2
3
4
5
6
7
8
9
10
11
12
13# Time : O(n)
# Space : O(n)
class Solution(object):
def sortColors(self, nums):
cnt = collections.defaultdict(int)
for num in nums:
cnt[num] += 1
p = 0
for i in xrange(3):
for _ in xrange(cnt[i]):
nums[p] = i
p += 1
Sol2. Dutch Partitioning Problem
one-pass algorithm1
2
3
4
5
6
7
8
9
10
11
12
13
14class Solution(object):
def sortColors(self, nums):
l, r = 0, len(nums)-1
i = 0
while i <= r:
if nums[i] == 0:
nums[l], nums[i] = nums[i], nums[l]
l += 1
i += 1 # missing part
elif nums[i] == 1:
i += 1
else:
nums[i], nums[r] = nums[r], nums[i]
r -= 1
Wiggle Sort
280. Wiggle Sort
Solution1. Sort
- Time : O(nlogn)
1
2
3
4
5class Solution(object):
def wiggleSort(self, nums):
nums.sort()
for i in range(2, len(nums), 2):
nums[i], nums[i-1] = nums[i-1], nums[i]
Solution2. One Pass
- Time : O(n)
1
2
3
4
5class Solution(object):
def wiggleSort(self, nums):
for i in range(1, len(nums)):
if (i % 2 == 0 and nums[i] > nums[i-1]) or (i % 2 == 1 and nums[i] < nums[i-1]):
nums[i], nums[i-1] = nums[i-1], nums[i]
324. Wiggle Sort II
1 | class Solution(object): |
Big data
Sort with small memory
Given a single computer with a single CPU and a single core, which has 2GB of memory and 1GB available for use, it also has two 100GB hard drives. How to sort 80GB integers of 64bits.?
- External Sort
- 400 chunks. Use 1GB memory to sort each 0.2GB. => write to disk
- 400-way merge
chunk1 xxxxxx xxxxxxxxxxxxxxx
chunk2 yyyyyy yyyyyyyyyyyyyyy
…
chunk400 zzzzzz zzzzzzzzzzzzzzz
read from each chunk, block by block, insert into a min Heap. - result block
- Why read one block by block instead of one by one? File IO!!!