Anda di halaman 1dari 3

"""

Sorting algorithmns
===============================================
Outlines common sorting techniques and analysis
Author: Kyo Jin Kwon
Date Last Updated: April 10, 2016
"""
# Bubble Sort
def bubble_sort(L):
"""
Returns sorted <L>.
Iterates through <L> n-1 times.
On each iteration, it swaps the largest
unswapped item at its proper place relative to
the end of the <L>.
I.e. biggest value of each iteration "bubbles" up
to its rightful place.
@param list L: list input for sorting
@rtype: list
>>> lst = [0, 9, 1, 1, -100, 199, 76, -32, 0]
>>> bubble_sort(lst)
[-100, -32, 0, 0, 1, 1, 9, 76, 199]
"""
for i in range(1, len(L)): # n-1 iterations
# Iterate through all items up to len(L) - i
for j in range(0, len(L) - i):
# Make necessary swaps
if L[j] > L[j+1]:
L[j], L[j+1] = L[j+1], L[j]
return L
# Selection Sort
def selection_sort(L):
"""
Returns sorted <L>.
Iterates through <L> n-1 times.
Similar to bubble sort, except only one
swap is made in each iteration.
The maximum value of each iteration is
"selected" and swapped to its appropriate
place.
@param list L: list input for sorting
@rtype: list
>>> lst = [0, 9, 1, 1, -100, 199, 76, -32, 0]
>>> selection_sort(lst)
[-100, -32, 0, 0, 1, 1, 9, 76, 199]
"""
#n-1 iterations from L[-1] to L[1]
for i in range(len(L)-1, 0, -1):
#set initial index of max value to 0
max_index = 0

#iterate from beginning of list to L[i]


for j in range(0, i+1):
#if a bigger max is found, make that
#the new max_index
if L[j] > L[max_index]:
max_index = j
#swap L[i] with L[max_index]
#i.e. put L[max_index] near the end of L
L[i], L[max_index] = L[max_index], L[i]
return L
# Insertion Sort
def insertion_sort(L):
"""
Returns sorted <L>.
Iterates through <L> n-1 times.
On each iteration, it makes swaps to grow
a sorted list in the beginning of <L>.
I.e. it "inserts" new items of <L> into
a (initially empty) sorted sublist at the front.
@param list L: list input for sorting
@rtype: list
>>> lst = [0, 9, 1, 1, -100, 199, 76, -32, 0]
>>> insertion_sort(lst)
[-100, -32, 0, 0, 1, 1, 9, 76, 199]
"""
for i in range(1, len(L)): # n-1 iterations
j = i #set j to i
# if jth item is less than preceeding item
while (j > 0) and (L[j] < L[j-1]):
L[j], L[j-1] = L[j-1], L[j] #swap items
# sort remaining elements of sublist at the front
# (i.e. put L[j] in its proper order)
j -= 1
return L
# Quick Sort
def quick_sort(L):
"""
Returns sorted <L>.
@param list L: list input for sorting
@rtype: list
>>> lst = [0, 9, 1, 1, -100, 199, 76, -32, 0]
>>> quick_sort(lst)
[-100, -32, 0, 0, 1, 1, 9, 76, 199]
"""
if len(L) < 2:
return L[:]
else:
# L[0] is the pivot
return (quick_sort([i for i in L if i < L[0]]) +
[L[0]] +
quick_sort([i for i in L[1:] if i >= L[0]]))

# Merge Sort
def merge(L1, L2):
"""
Returns sorted list combining <L1> and <L2>.
>>> merge([1, 3, 5], [2, 4, 6])
[1, 2, 3, 4, 5, 6]
@param list L1: list to combine
@param list L2: list to combine
@rtype list
"""
n_lst = []
i1, i2 = 0, 0
while (i1 < len(L1)) and (i2 < len(L2)):
if L1[i1] < L2[i2]:
n_lst.append(L1[i1])
i1 += 1
else:
n_lst.append(L2[i2])
i2 += 1
return n_lst + L1[i1:] + L2[i2:]
def merge_sort(L):
# Reference:
# https://www.youtube.com/watch?v=y-UWAo48IWg
"""
Returns sorted <L>.
Divide <L> into sublists (recursive depth O(lg n)),
sort the sublists (O(n) at each level) and merge them together - O(nlgn).
>>> lst = [0, 9, 1, 1, -100, 199, 76, -32, 0]
>>> merge_sort(lst)
[-100, -32, 0, 0, 1, 1, 9, 76, 199]
@param list L: list input for sorting
@rtype: list
"""
if len(L) <= 1:
return L[:]
else:
left = merge_sort(L[:len(L)//2])
right = merge_sort(L[len(L)//2:])
return merge(left, right)
if __name__ == "__main__":
import doctest
doctest.testmod()