Skip to content

Instantly share code, notes, and snippets.

@ad-1
Last active April 13, 2023 20:56
Show Gist options
  • Select an option

  • Save ad-1/bf3d6cd997063e52b72fc0b37e802996 to your computer and use it in GitHub Desktop.

Select an option

Save ad-1/bf3d6cd997063e52b72fc0b37e802996 to your computer and use it in GitHub Desktop.
Sorting algorithm visualisation
def bubble_sort(self, unsorted, n):
""" bubble sort algorithm """
# iterate over unsorted array up until second last element
for i in range(0, n - 1):
# swapped conditions monitors for finalised list
swapped = False
# iterate over remaining unsorted items
for j in range(0, n - 1 - i):
# compare adjacent elements
if unsorted[j].value > unsorted[j + 1].value:
# swap elements
swap(unsorted, j, j + 1)
swapped = True
# no swaps have occured so terminate
if not swapped:
break
@index.setter
def index(self, i):
""" update coordinates when index changes """
self._index = i
# update horizontal coordinates
self.x1 = self._index * self.width
self.x2 = self.x1 + self.width
# dispatch updates to subscriber (visualiser)
self.subscriber.update_bar(self)
def insertion_sort(unsorted, n):
""" insertion sort algorithm """
# iterate over unsorted array
for i in range(1, n):
# get element value
val = unsorted[i].value
# insertion "hole" is at index i
hole = i
# loop backwards until a value greater than current value is found
while hole > 0 and unsorted[hole - 1].value > val:
# swap elements towards correct position
unsorted[hole].value = unsorted[hole - 1].value
# move backwards
hole -= 1
# insert value into correct position
unsorted[hole].value = val
def __lt__(self, other):
# compare object with "other" object of same type
return self.value < other.value
def divide(self, unsorted, lower, upper):
""" recrusive function to divide array into 2 sub arrays for sorting """
# recursive base case reached
if upper <= lower:
return
# get middle element for split
mid = (lower + upper) // 2
# divide array at midpoints
divide(unsorted, lower, mid)
divide(unsorted, mid + 1, upper)
# merge sorted arrays
merge(unsorted, lower, mid, mid + 1, upper)
def merge(unsorted, l_lower, l_upper, r_lower, r_upper):
""" merging two sorted arrays to one sorted array """
# extract left and right indices
i, j = l_lower, r_lower
# initialise temporary array
temp = []
# move through indices
while i <= l_upper and j <= r_upper:
# determine which value to put in temp next
if unsorted[i].value <= unsorted[j].value:
temp.append(unsorted[i])
i += 1
else:
temp.append(unsorted[j])
j += 1
# one of the above conditions finishes first
# therefore, handle the unfinished case
while i <= l_upper:
temp.append(unsorted[i])
i += 1
while j <= r_upper:
temp.append(unsorted[j])
j += 1
# assign elements from temp array
for y, k in enumerate(range(l_lower, r_upper + 1)):
unsorted[k] = temp[y]
def quick_sort(self, unsorted, start, end):
""" quick sort recursive algorithm """
# stop when left index has reached/exceeded right index
if start >= end:
return
# determine next pivot position
i_pivot = partition(unsorted, start, end - 1)
# recursive call to left partition
quick_sort(unsorted, start, i_pivot)
# recursive call to right portion
quick_sort(unsorted, i_pivot + 1, end)
def partition(self, unsorted, start, end):
""" arrange (left array < pivot) and (right array > pivot) """
# select pivot value as last element in unsorted segment
pivot = unsorted[end]
# assign pivot index to left index
i_pivot = start
# iterate from start to end of current segment
for i in range(start, end):
# compare current value to the pivot value
if unsorted[i].value <= pivot.value:
# swap current value with pivot value
swap(unsorted, i, i_pivot)
# increase pivot index
i_pivot += 1
# put pivot in correct position by swapping with item from left
swap(unsorted, i_pivot, end)
# return next pivot index
return i_pivot
def selection_sort(self, unsorted, n):
# iterate over array
for i in range(0, n):
# initialise with first value
current_min = unsorted[i]
# min_index initialiser
min_index = i
# iterate over remaining unsorted items
for j in range(i, n):
# check if jth value is less than current min
if unsorted[j] < current_min:
# update minimum value and index
current_min = unsorted[j]
min_index = j
# swap ith and jth values
swap(unsorted, i, min_index)
def swap(arr, a, b):
""" swap elements a and b in an array """
temp = arr[a]
arr[a] = arr[b]
arr[b] = temp
# return arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment