Lesson 21 - Learning Goals


21.1 Learn why sorting is useful


21.2 Learn several different Sorting Techniques

21.3 Learn how to compare different sorting algorithms


Sorting

Introduction


Order notation

For sorting:


Bubble sort

A(1) small

A(2) .

A(3) .

… .

A(n) big

Bubble sort Algorithm

for i = 1 to n do

for j = n downto i + 1 do

if A[j] < A[j-1] then

Swap(A[j] , A[j-1])


Insertion Sort

Algorithm for Insertion Sort

A[0] = -MaxInt

for i = 2 to n do begin

j = i

while A[j] < A[j-1] do begin

Swap(A[j], A[j-1])

j = j - 1

end

end


Selection sort

Algorithm for Selection Sort

for i = 1 to n - 1 do begin

j = i

Min = A[i]

for k = i + 1 to n do

if A[k] < Min then begin

Min = A[k]

j = k

end

end

Swap( A[i], A[j])

end



Shell Sort

Better algorithms


Go back to lecture menu

Go back to main page


Copyright © 1996 McGill University. All rights reserved.