0% found this document useful (0 votes)
4 views2 pages

sorting_algorithms

The document provides pseudo code for four sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, and Merge Sort. Each algorithm is defined with its own procedure, detailing the steps for sorting an array. The pseudo code outlines the logic and flow for each sorting method, emphasizing their unique approaches to organizing data.

Uploaded by

Mark Ritchings
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

sorting_algorithms

The document provides pseudo code for four sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, and Merge Sort. Each algorithm is defined with its own procedure, detailing the steps for sorting an array. The pseudo code outlines the logic and flow for each sorting method, emphasizing their unique approaches to organizing data.

Uploaded by

Mark Ritchings
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like