Pseudo Code for Four Sorting Algorithms
// Bubble Sort
procedure BubbleSort(array)
n = length(array)
for i from 0 to n-1 do
for j from 0 to n-2-i do
if array[j] > array[j+1] then
swap array[j] and array[j+1]
end if
end for
end for
end procedure
// Selection Sort
procedure SelectionSort(array)
n = length(array)
for i from 0 to n-1 do
minIndex = i
for j from i+1 to n-1 do
if array[j] < array[minIndex] then
minIndex = j
end if
end for
swap array[i] and array[minIndex]
end for
end procedure
// Insertion Sort
procedure InsertionSort(array)
n = length(array)
for i from 1 to n-1 do
key = array[i]
j=i-1
while j >= 0 and array[j] > key do
array[j+1] = array[j]
j=j-1
end while
array[j+1] = key
end for
end procedure
// Merge Sort
procedure MergeSort(array)
if length(array) > 1 then
mid = length(array) / 2
left = array[0..mid-1]
right = array[mid..end]
MergeSort(left)
MergeSort(right)
i = 0, j = 0, k = 0
while i < length(left) and j < length(right) do
if left[i] < right[j] then
array[k] = left[i]
i=i+1
else
array[k] = right[j]
j=j+1
end if
k=k+1
end while
while i < length(left) do
array[k] = left[i]
i=i+1
k=k+1
end while
while j < length(right) do
array[k] = right[j]
j=j+1
k=k+1
end while
end if
end procedure