Bubble Sort Visualizer
An interactive way to understand sorting algorithms.
Pseudo-code
procedure bubbleSort(A)
n = length(A)
repeat
swapped = false
for i = 1 to n-1
if A[i-1] > A[i]
swap(A[i-1], A[i])
swapped = true
n = n - 1
until not swapped
procedure selectionSort(A)
n = length(A)
for i = 0 to n-1
minIndex = i
for j = i+1 to n-1
if A[j] < A[minIndex]
minIndex = j
if minIndex != i
swap(A[i], A[minIndex])
procedure insertionSort(A)
for i = 1 to length(A)-1
key = A[i]
j = i - 1
while j >= 0 and key < A[j]
A[j+1] = A[j]
j = j - 1
A[j+1] = key
procedure quickSort(A, low, high)
if low < high
pivotIndex = partition(A, low, high)
quickSort(A, low, pivotIndex - 1)
quickSort(A, pivotIndex + 1, high)
procedure partition(A, low, high)
pivot = A[high]
i = low - 1
for j = low to high - 1
if A[j] < pivot
i++
swap(A[i], A[j])
swap(A[i+1], A[high])
return i + 1
procedure mergeSort(A, l, r)
if l >= r then return
m = l + (r - l) / 2
mergeSort(A, l, m)
mergeSort(A, m + 1, r)
merge(A, l, m, r)
procedure merge(A, l, m, r)
Create L[] from A[l..m]
Create R[] from A[m+1..r]
i=0, j=0, k=l
while i < len(L) and j < len(R)
if L[i] <= R[j]...
else...
Copy remaining elements

No comments
Post a Comment