Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Sorting Algorithms: Bubble, Modified Bubble, Insertion, Selection, Merge, Heap, Quicksort, Study notes of Data Structures and Algorithms

The pseudocode for various sorting algorithms including Bubble Sort, Modified Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Heap Sort, and Quicksort. Each algorithm is presented with its corresponding pseudocode.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

luber-1
luber-1 🇬🇧

4.8

(12)

294 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bubble Sort
for i = n downto 2 do
for j = 1 to i-1 do
if A[j] > A[j+1]
then A[j] A[j+1]
end if
end for
end for
for i = n downto 2 do
for j = 1 to i-1 do
if A[j] > A[j+1]
then A[j] A[j+1]
end if
end for
end for
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Sorting Algorithms: Bubble, Modified Bubble, Insertion, Selection, Merge, Heap, Quicksort and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Bubble Sort

for i = n downto 2 do for j = 1 to i-1 do if A[j] > A[j+1] then A[j] ↔ A[j+1] end if end for end for

for i = n downto 2 do for j = 1 to i-1 do if A[j] > A[j+1] then A[j] ↔ A[j+1] end if end for end for

Bubble Sort (Modified)

i ← n- while i > 0 do t ← 1 for j = 1 to i do if A[j] > A[j+1] then A[j] ↔ A[j+1] t ← j end if end for i ← t- end while

Cocktail-Shaker Sort

No code.

Insertion Sort (without sentinel)

for i = 2 to n do t ← A[i] j ← i- while j>0 and A[j]>t do A[j+1] ← A[j] j ← j- end while A[j+1] ← t end for

for i = 2 to n do t ← A[i] j ← i- while j>0 and A[j]>t do A[j+1] ← A[j] j ← j- end while A[j+1] ← t end for

Selection Sort

for i = n downto 2 do k ← 1 for j = 2 to i do if A[j] > A[k] then k ← j end for A[k] ↔ A[i] end for

for i = n downto 2 do k ← 1 for j = 2 to i do if A[j] > A[k] then k ← j end for A[k] ↔ A[i] end for

Heap Sort

proc heapsort(A: list, n: list size) {Create heap} for r = bn/2c downto 1 do sift(r,n) end for {Finish Sort} for m = n downto 2 do A[1] ↔ A[m] sift(1,m-1) end for end proc

proc sift(p: root, m: size of list) c ← 2p while c ≤ m do if c < m then if A[c+1] > A[c] then c ← c+1 end if end if if A[c] > A[p] then A[p] ↔ A[c] p ← c c ← 2p else exit while loop end if end while end proc

Quicksort

procedure quicksort(A,p,r) if p<r then q ← partition(A,p,r) quicksort(A,p,q-1) quicksort(A,q+1,r) end if end procedure

function partition(A,p,r) X ← A[r] i ← p- for j = p to r-1 do if A[j] ≤ X then i ← i+ A[i] ↔ A[j] end if end for A[i+1] ↔ A[r] return(i+1) end function