-Methods:
1. The Bubble Sort:
Go Through list, compare pair by pair. (sorted from the front)
2. The Selection Sort:
Select min/max to front/end of list at one turn.
3.The Insertion Sort:
Start from beginning, insert next to right position.
4. The Merge Sort:
split the list and recursively invoke a merge
sort on both halves. Once the two halves are sorted, the fundamental
operation, called a merge, is performed. Merging is the process of
taking two smaller sorted lists and combining them together into a
single, sorted, new list.
5. The Quick Sort:
A quick sort first selects a value, which is called the pivot value.The role of the pivot value
is to assist with splitting the list. The actual position where the
pivot value belongs in the final sorted list, commonly called the
split point, will be used to divide the list for subsequent calls to
the quick sort.(as shown in lecture)
-Codes:
# some sorts
import sys
#sys.setrecursionlimit(1000000)
def select(L):
"""Produce copy of L in sorted order
>>> select([3, 1, 2])
[1, 2, 3]
"""
L1 = L[:]
for i in range(len(L1)):
m = min(L1[i:])
j = L1[i:].index(m) + i
L1[i], L1[j] = L1[j], L1[i]
return L1
def quick(L):
if len(L) > 1 :
return (quick([x for x in L[1:] if x < L[0]])
+ [L[0]] + quick([x for x in L[1:] if x >= L[0]]))
else :
return L
def merge(L1, L2):
"""return merge of L1 and L2
>>> merge([1, 3, 5], [2, 4, 6])
[1, 2, 3, 4, 5, 6]
"""
L1.reverse()
L2.reverse()
L = []
while L1 and L2:
if L1[-1] < L2[-1]:
L.append(L1.pop())
else:
L.append(L2.pop())
return L + L1 + L2
def merge_sort(L):
if len(L) < 2 :
return L
else :
return merge(merge_sort(L[:len(L)//2]), merge_sort(L[len(L)//2:]))
def count_sort(L):
count_list = [0, ] * len(L)
for i in L:
count_list[i] += 1
L[:] = [] # blank slate!
for i in range(len(count_list)):
for j in range(count_list[i]):
L.append(i)
import sys
#sys.setrecursionlimit(1000000)
def select(L):
"""Produce copy of L in sorted order
>>> select([3, 1, 2])
[1, 2, 3]
"""
L1 = L[:]
for i in range(len(L1)):
m = min(L1[i:])
j = L1[i:].index(m) + i
L1[i], L1[j] = L1[j], L1[i]
return L1
def quick(L):
if len(L) > 1 :
return (quick([x for x in L[1:] if x < L[0]])
+ [L[0]] + quick([x for x in L[1:] if x >= L[0]]))
else :
return L
def merge(L1, L2):
"""return merge of L1 and L2
>>> merge([1, 3, 5], [2, 4, 6])
[1, 2, 3, 4, 5, 6]
"""
L1.reverse()
L2.reverse()
L = []
while L1 and L2:
if L1[-1] < L2[-1]:
L.append(L1.pop())
else:
L.append(L2.pop())
return L + L1 + L2
def merge_sort(L):
if len(L) < 2 :
return L
else :
return merge(merge_sort(L[:len(L)//2]), merge_sort(L[len(L)//2:]))
def count_sort(L):
count_list = [0, ] * len(L)
for i in L:
count_list[i] += 1
L[:] = [] # blank slate!
for i in range(len(count_list)):
for j in range(count_list[i]):
L.append(i)
-Sorting( from http://bigocheatsheet.com/)
| Algorithm | Data Structure | Time Complexity | Worst Case Auxiliary Space Complexity | ||||
|---|---|---|---|---|---|---|---|
| Best | Average | Worst | Worst | ||||
| Quicksort | Array | O(n log(n)) |
O(n log(n)) |
O(n^2) |
O(n) |
||
| Mergesort | Array | O(n log(n)) |
O(n log(n)) |
O(n log(n)) |
O(n) |
||
| Heapsort | Array | O(n log(n)) |
O(n log(n)) |
O(n log(n)) |
O(1) |
||
| Bubble Sort | Array | O(n) |
O(n^2) |
O(n^2) |
O(1) |
||
| Insertion Sort | Array | O(n) |
O(n^2) |
O(n^2) |
O(1) |
||
| Select Sort | Array | O(n^2) |
O(n^2) |
O(n^2) |
O(1) |
||
| Bucket Sort | Array | O(n+k) |
O(n+k) |
O(n^2) |
O(nk) |
||
| Radix Sort | Array | O(nk) |
O(nk) |
O(nk) |
O(n+k | ||
No comments:
Post a Comment