def insertion_sort(arr, start, end): for i in range(start+1, end+1): j = i while j > start and arr[j] < arr[j - 1]: handleDrawing(arr, i, -1, j, -1) arr[j], arr[j - 1] = arr[j - 1], arr[j] j -= 1
def bubbleSort(array, *args): size = len(array) for i in range(size): for j in range(size - i - 1): handleDrawing(array, j, j + 1, -1, -1) if array[j] > array[j + 1]: array[j], array[j + 1] = array[j + 1], array[j]
def cycleSort(array, *args): for cycle_start in range(0, len(array) - 1): item = array[cycle_start] pos = cycle_start for i in range(cycle_start + 1, len(array)): if array[i] < item: pos += 1 if pos == cycle_start: continue while array[pos] == item: pos += 1 handleDrawing(array, cycle_start, pos, -1, -1) array[pos], item = item, array[pos] while pos != cycle_start: pos = cycle_start for i in range(cycle_start + 1, len(array)): if array[i] < item: pos += 1 while array[pos] == item: pos += 1 handleDrawing(array, cycle_start, pos, -1, -1) array[pos], item = item, array[pos]
def merge(arr, left, mid, right): left_arr_size = mid - left + 1 right_arr_size = right - mid left_arr, right_arr = [], [] for i in range(left_arr_size): left_arr.append(arr[left + i]) for i in range(right_arr_size): right_arr.append(arr[mid + i + 1]) k, i, j = left, 0, 0 while i < left_arr_size and j < right_arr_size: handleDrawing(arr, left + i, mid + j, left, right) if left_arr[i] <= right_arr[j]: arr[k] = left_arr[i] i += 1 else: arr[k] = right_arr[j] j += 1 k += 1 while i < left_arr_size: arr[k] = left_arr[i] i += 1 k += 1 while j < right_arr_size: arr[k] = right_arr[j] j += 1 k += 1
def counting_Sort(array, exp1): n = len(array) output = [] for i in range(0, n): output.append(array[i]) count = [0] * (10) for i in range(0, n): index = (array[i] / exp1) count[int(index % 10)] += 1 for i in range(1, 10): count[i] += count[i - 1] i = n - 1 while i >= 0: index = (array[i] / exp1) handleDrawing(output, count[int(index % 10)] - 1, -1, int(index % 10), -1) output[count[int(index % 10)] - 1] = array[i] count[int(index % 10)] -= 1 i -= 1 i = 0 if (array != output): pass else: return (0) for i in range(0, len(array)): array[i] = output[i] del (output)
def heapSort(array, *args): heapify(array, len(array)) end = len(array) - 1 while end > 0: handleDrawing(array, -1, -1, 0, end, ) array[end], array[0] = array[0], array[end] end -= 1 siftDown(array, 0, end)
def selectionSort(array, *args): size = len(array) for i in range(size - 1): smallIndex = i for j in range(i, size): handleDrawing(array, j, -1, i, -1) if array[j] < array[smallIndex]: smallIndex = j array[i], array[smallIndex] = array[smallIndex], array[i]
def gnomeSort(a, *args): i, size = 0, len(a) while i < size: if a[i - 1] <= a[i] or i == 0: handleDrawing(a, i, i - 1, -1, -1) i += 1 else: handleDrawing(a, i, i - 1, -1, -1) a[i - 1], a[i] = a[i], a[i - 1] i -= 1
def insertionSort(array, *args): size = len(array) for i in range(1, size): j = i-1 key = array[i] while j >= 0 and array[j] > key: handleDrawing(array, j, -1, i, -1) array[j+1] = array[j] j -= 1 array[j+1] = key
def shellSort(array, *args, gapType="ciura"): gaps = GAPS.get(gapType, "ciura")(len(array)) for gap in gaps: for i in range(gap, len(array)): temp, j = array[i], i while j >= gap and array[j - gap] > temp: handleDrawing(array, j, j - gap, -1, -1) array[j] = array[j - gap] j -= gap handleDrawing(array, -1, -1, i, j) array[j] = temp
def countingSort(array, *args): size = len(array) A = array.copy() C = [0] * size for i in range(size): C[A[i]] += 1 for i in range(1, len(C)): C[i] += C[i - 1] for i in range(0, size): handleDrawing(array, C[A[size - i - 1]] - 1, -1, size - i - 1, -1) array[C[A[size - i - 1]] - 1] = A[size - i - 1] C[A[size - i - 1]] -= 1
def insertionSort(array, *args): size = len(array) for i in range(0, size): j = i - 1 key = array[i] mySortedRows.append(i) while j >= 0 and array[j] > key: handleDrawing(array, j, -1, i, -1, greenRows=mySortedRows) array[j + 1] = array[j] j -= 1 array[j + 1] = key mySortedRows.clear()
def quickSort(array, left, right): if left >= right: return index = left for j in range(left, right): # The four lines below are not part of the algorithm handleDrawing(array, j, right, index, -1) if array[j] < array[right]: array[j], array[index] = array[index], array[j] index += 1 array[index], array[right] = array[right], array[index] quickSort(array, index + 1, right) quickSort(array, left, index - 1)
def combSort(array, *args): size = len(array) gap = size swapped = True while gap != 1 or swapped: gap = get_gap(gap) swapped = False for idx in range(0, size - gap): handleDrawing(array, idx, idx+gap, -1, -1) if array[idx] > array[idx + gap]: array[idx], array[idx + gap] = array[idx + gap], array[idx] swapped = True
def bogoSort(array, *args): is_sorted = False arrayLen = len(array) while not is_sorted: for i in range(arrayLen): j = randint(0, arrayLen - 1) array[i], array[j] = array[j], array[i] for k in range(len(array) - 1): handleDrawing(array, k, k + 1, -1, -1) if array[k] > array[k + 1]: is_sorted = False break is_sorted = True
def oddevenSort(array, *args): sorted = False while not sorted: sorted = True for i in range(1, len(array) - 1, 2): handleDrawing(array, i, i + 1, -1, -1) if array[i] > array[i + 1]: swap(array, i, i + 1) sorted = False for i in range(0, len(array) - 1, 2): handleDrawing(array, i, i + 1, -1, -1) if array[i] > array[i + 1]: swap(array, i, i + 1) sorted = False
def siftDown(array, start, end): root = start while 2 * root + 1 <= end: child = 2 * root + 1 swap = root if array[swap] < array[child]: swap = child if child + 1 <= end and array[swap] < array[child + 1]: swap = child + 1 if swap == root: return else: handleDrawing(array, root, swap, -1, -1) array[root], array[swap] = array[swap], array[root] root = swap
def quickSort(array, left, right): if left >= right: return index = left random_index = randint(left, right) array[right], array[random_index] = array[random_index], array[right] for j in range(left, right): handleDrawing(array, j, right, index, -1) if array[j] < array[right]: array[j], array[index] = array[index], array[j] index += 1 array[index], array[right] = array[right], array[index] quickSort(array, index + 1, right) quickSort(array, left, index - 1)
def partition(array, low, high): pivot = array[low] i = low - 1 j = high + 1 while True: i += 1 while array[i] < pivot: i += 1 j -= 1 while array[j] > pivot: j -= 1 handleDrawing(array, j, high, low, -1) if i >= j: return j array[i], array[j] = array[j], array[i]
def binary_search(arr, val, start, end, current): if start == end: if arr[start] > val: return start else: return start + 1 if start > end: return start mid = round((start + end) / 2) handleDrawing(arr, start, end, mid, current) if arr[mid] < val: return binary_search(arr, val, mid + 1, end, current) elif arr[mid] > val: return binary_search(arr, val, start, mid - 1, current) else: return mid
def stoogeSort(arr, l, h): if l >= h: return if arr[l] > arr[h]: middle = floor((h + l) / 2) handleDrawing(arr, l, h, middle, -1) t = arr[l] arr[l] = arr[h] arr[h] = t if h - l + 1 > 2: t = (int)((h - l + 1) / 3) stoogeSort(arr, l, (h - t)) stoogeSort(arr, l + t, (h)) stoogeSort(arr, l, (h - t))
def helper(arr, n, start): if start == n: return i = start + 1 count = start + 1 while i < n: #next line not a part of algo handleDrawing(arr, i, count - 1, start, n - 1) if arr[i] > arr[count - 1]: arr.insert(count, arr.pop(i)) count += 1 i += 1 if start != 0: op = merge(arr, 0, start - 1, count - 1) helper(arr, n, count)
def bucketSort(array, *args): bucket = [] for i in range(len(array)): bucket.append([]) n = len(bucket) for j in array: index_b = int(j / n) bucket[index_b].append(j) handleDrawing(array, j, -1, index_b, -1) for i in range(len(array)): bucket[i] = sorted(bucket[i]) k = 0 for i in range(len(array)): for j in range(len(bucket[i])): handleDrawing(array, k, -1, i, -1) array[k] = bucket[i][j] k += 1
def cocktailSort(array, *args): n = len(array) swapped = True start = 0 end = n - 1 while (swapped == True): swapped = False for i in range(start, end): handleDrawing(array, i, i + 1, -1, -1) if (array[i] > array[i + 1]): array[i], array[i + 1] = array[i + 1], array[i] swapped = True if (swapped == False): break swapped = False end = end - 1 for i in range(end - 1, start - 1, -1): handleDrawing(array, -1, -1, i, i + 1) if (array[i] > array[i + 1]): array[i], array[i + 1] = array[i + 1], array[i] swapped = True start = start + 1
def pancakeSort(array, *args): for i in range(len(array)): max_index = array.index(max(array[:len(array) - i])) handleDrawing(array, max_index, -1, -1, -1) flip(array, max_index) handleDrawing(array, 0, -1, -1, -1) flip(array, len(array) - 1 - i) handleDrawing(array, -1, -1, len(array) - 1 - i, -1)
def merge(array, left, mid, right): L = array[left:mid + 1] R = array[mid + 1:right + 1] i = 0 j = 0 k = left while i < len(L) and j < len(R): # The two lines below is not part of the algorithm handleDrawing(array, left + i, mid + j, left, right) if L[i] < R[j]: array[k] = L[i] i += 1 else: array[k] = R[j] j += 1 k += 1 while i < len(L): array[k] = L[i] i += 1 k += 1 while j < len(R): array[k] = R[j] j += 1 k += 1
def compAndSwap(array, i, j, dir): if (dir and array[i] > array[j]) or (not dir and array[i] <= array[j]): array[i], array[j] = array[j], array[i] handleDrawing(array, i, -1, j, -1)