Last active
January 31, 2020 21:55
-
-
Save hanglearning/8273fa39ef9ef890b0fd34a524d3a4ee to your computer and use it in GitHub Desktop.
Some sorting algorithm coding practices in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bubble sort | |
def bubble_sort(arr): | |
arr_len = len(arr) | |
for i in range(arr_len - 1, 0, -1): | |
for j in range(i): | |
if arr[j] > arr[j + 1]: | |
arr[j], arr[j + 1] = arr[j + 1], arr[j] | |
a = [2,5,1,8,-2,0,9,2] | |
bubble_sort(a) # inplace sorting | |
# selection sort swap max | |
def select_sort_swap_max(arr): | |
arr_len = len(arr) | |
for i in range(arr_len - 1, 0, -1): | |
max_index = 0 | |
for j in range(i + 1): | |
if arr[j] > arr[max_index]: | |
max_index = j | |
arr[i], arr[max_index]= arr[max_index], arr[i] | |
a = [2,5,1,8,-2,0,9,2] | |
select_sort_swap_max(a) # inplace sorting | |
def select_sort_swap_max_2(array): | |
""" | |
input: int[] array | |
return: int[] | |
""" | |
# write your solution here | |
# swap max | |
# MISTAKE!!! if not array: # CAUTION: edge case!!! | |
if array: | |
for i in range(len(array) - 1, 0, -1): | |
max_index = 0 | |
for j in range(1, i + 1): | |
if array[j] > array[max_index]: | |
max_index = j | |
array[i], array[max_index] = array[max_index], array[i] | |
return array | |
# selection sort swap min - MISTAKE | |
# def select_sort_swap_min(arr): | |
# arr_len = len(arr) | |
# for i in range(arr_len - 1, 0, -1): | |
# min_index = 0 | |
# for j in range(i + 1): | |
# if arr[j] < arr[min_index]: | |
# min_index = j | |
# arr[arr_len - i], arr[min_index]= arr[min_index], arr[arr_len - i] | |
# a = [2,5,1,8,-2,0,9,2] | |
# select_sort_swap_min(a) # inplace sorting | |
# selection sort swap min | |
def select_sort_swap_min(arr): | |
len_arr = len(arr) | |
for i in range(len_arr): | |
# least_index = 0 | |
least_index = i | |
for j in range(i, len_arr): | |
if arr[j] < arr[least_index]: | |
least_index = j | |
arr[i], arr[least_index] = arr[least_index], arr[i] | |
a = [2,5,1,8,-2,0,9,2] | |
select_sort_swap_min(a) # inplace sorting | |
# Insertion Sort | |
# Merge Sort | |
def merge(left, right): | |
merged_list = [] | |
i = 0 | |
j = 0 | |
while i < len(left) and j < len(right): | |
if left[i] < right[j]: | |
merged_list.append(left[i]) | |
i += 1 | |
else: | |
merged_list.append(right[j]) | |
j += 1 | |
if i == len(left): | |
merged_list = merged_list + right[j:] | |
else: | |
merged_list = merged_list + left[i:] | |
return merged_list | |
def mergeSort(array): | |
""" | |
input: int[] array | |
return: int[] | |
""" | |
if not array or len(array) == 1: | |
return array | |
middle = (len(array) + 1) // 2 | |
return merge(mergeSort(array[:middle]), mergeSort(array[middle:])) | |
# return mergeSort(merge(array[:middle], array[middle:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment