def merge_sort(array): print("[log] run merge sort") method = "merge sort" count = 0 sys.setrecursionlimit(10000) array, count = merge(array, 0, len(array) - 1, count, method) gi.make_endimage(array, count, method)
def stooge_sort(array): print("[log] run stooge sort") method = "stooge sort" count = 0 sys.setrecursionlimit(10000) array, count = stooge(array, count, method, 0, len(array) - 1) gi.make_endimage(array, count, method)
def shaker_sort(array): print("[log] run shaker_sort") method = "shaker sort" count = 0 l = 0 r = len(array) - 1 while l < r: for i in range(l, r): if array[i] > array[i + 1]: array[i], array[i + 1] = array[i + 1], array[i] shift = i gi.make_image(array, count, method, i, -1) count += 1 r = shift for i in range(r, l, -1): if array[i] < array[i - 1]: array[i], array[i - 1] = array[i - 1], array[i] shift = i gi.make_image(array, count, method, i, -1) count += 1 l = shift gi.make_endimage(array, count, method)
def lsd_radix_sort(array): print("[log] run LSD radix sort") method = "radix sort (LSD)" count = 0 r = 10 m = 1 rad = array[:] y = array[:] while m <= r: for i in range(len(array)): rad[i] = (array[i] // m) % 10 k = 0 for i in range(10): for j in range(len(array)): if rad[j] == i: y[k] = array[j] k += 1 for i in range(len(array)): array[i] = y[i] gi.make_image(array, count, method, i, -1) count += 1 m *= 10 gi.make_endimage(array, count, method)
def quick_sort(array): print("[log] run quick sort") method = "quick sort" count = 0 sys.setrecursionlimit(10000) array, count = quick(array, 0, len(array) - 1, count, method) gi.make_endimage(array, count, method)
def heap_sort(array): print("[log] run heap sort") method = "heap sort" count = 0 n = len(array) - 1 for i in range((n // 2), -1, -1): array, count = downheap(array, count, method, i, n) for i in range(n, 0, -1): if array[0] > array[i]: array[0], array[i] = array[i], array[0] array, count = downheap(array, count, method, 0, i - 1) gi.make_endimage(array, count, method)
def selection_sort(array): print("[log] run selection sort") method = "selection sort" count = 0 n = len(array) for i in range(n - 1): least = i for j in range(i + 1, n): if array[j] < array[least]: least = j gi.make_image(array, count, method, j, least) count += 1 #sys.exit() array[i], array[least] = array[least], array[i] gi.make_endimage(array, count, method)
def bubble_sort(array): print("[log] run bubble sort") method = "bubble sort" count = 0 n = len(array) for i in range(0, n): rev = range(i, n) for j in reversed(rev): if array[j - 1] > array[j]: temp = array[j - 1] array[j - 1] = array[j] array[j] = temp gi.make_image(array, count, method, j - 1, -1) count += 1 gi.make_endimage(array, count, method)
def gravity_sort(arr): print("[log] run gravity sort") method = "gravity sort" count = 0 mat = arrtomat(arr) x = array.array('i') while mat != x: gi.make_image(arr, count, method, -1, -1) count += 1 x = mat mat = gravity(mat) arr = mattoarr(mat) gi.make_endimage(arr, count, method)
def gnome_sort(array): print("[log] run gnome sort") method = "gnome sort" count = 0 gnome = 1 while gnome < len(array): if gnome >= 1 and array[gnome - 1] > array[gnome]: array[gnome], array[gnome - 1] = array[gnome - 1], array[gnome] gnome -= 1 else: gnome += 1 gi.make_image(array, count, method, gnome - 1, -1) count += 1 gi.make_endimage(array, count, method)
def comb_sort(array): print("[log] run comb sort") method = "comb sort" count = 0 h = len(array) f = False while (h > 1 or f): h = (h * 10) // 13 f = False for i in range(len(array) - h): if array[i] > array[i + h]: array[i], array[i + h] = array[i + h], array[i] gi.make_image(array, count, method, i, i + h) count += 1 gi.make_endimage(array, count, method)
def insertion_sort(array): print("[log] run insertion sort") method = "insertion sort" count = 0 n = len(array) for i in range(1, n): tmp = array[i] if tmp < array[i - 1]: j = i while True: array[j] = array[j - 1] gi.make_image(array, count, method, j, -1) count += 1 j -= 1 if j <= 0 or tmp >= array[j - 1]: break array[j] = tmp #sys.exit() gi.make_endimage(array, count, method)
def oddeven_sort(array): print("[log] run oddeven sort") method = "oddeven sort" count = 0 ischange = True while ischange: ischange = False for i in range(0, len(array) - 1, 2): if array[i] > array[i + 1]: array[i], array[i + 1] = array[i + 1], array[i] ischange = True gi.make_image(array, count, method, i, i + 1) count += 1 for i in range(1, len(array) - 1, 2): if array[i] > array[i + 1]: array[i], array[i + 1] = array[i + 1], array[i] ischange = True gi.make_image(array, count, method, i, i + 1) count += 1 gi.make_endimage(array, count, method)
def shell_sort(array): print("[log] run shell sort") method = "shell sort" count = 0 inc = 4 while inc > 0: for i in range(len(array)): j = i temp = array[i] while j >= inc and array[j - inc] > temp: array[j] = array[j - inc] j = j - inc array[j] = temp gi.make_image(array, count, method, i, -1) count += 1 if inc // 2 != 0: inc = inc // 2 elif inc == 1: inc = 0 else: inc = 1 gi.make_endimage(array, count, method)
def msd_radix_sort(array): print("[log] run MSD radix sort") method = "radix sort (MSD)" count = 0 array, count = msd_radix(array, 0, len(array) - 1, count, method) gi.make_endimage(array, count, method)