0% found this document useful (0 votes)
13 views11 pages

Workshop 3 Sorting

Uploaded by

farouk.senro
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)
13 views11 pages

Workshop 3 Sorting

Uploaded by

farouk.senro
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/ 11

Workshop 3 Sorting

Problem 1: Bubble Sort


Description: Implement the Bubble Sort algorithm to sort an array of integers in ascending
order.

Sample Input:

[5, 2, 9, 1, 5, 6]

Sample Output:

[1, 2, 5, 5, 6, 9]

Algorithm Implementation:
ALGORITHM bubble_sort
VAR
arr: ARRAY_OF[0..n-1]:INTEGER
n, i, j, temp: INTEGER

BEGIN
// Input
arr := [5, 2, 9, 1, 5, 6]
n := arr.length(arr)

// Sorting using Bubble Sort


FOR i FROM 0 TO n - 1 DO
FOR j FROM 0 TO n - i - 2 DO
IF arr[j] > arr[j + 1] THEN
// Swap if out of order
temp := arr[j]
arr[j] := arr[j + 1]
arr[j + 1] := temp
END IF
END FOR
END FOR

// Output
WRITE(arr)
END

Problem 2: Merge Sort


Description: Implement the Merge Sort algorithm to sort an array of integers in ascending
order.

Sample Input:

[8, 3, 1, 7, 0, 10, 2]

Sample Output:

[0, 1, 2, 3, 7, 8, 10]




Algorithm Implementation:
ALGORITHM merge_sort
VAR
arr: ARRAY OF INTEGER
n: INTEGER
BEGIN
// Input
arr := [8, 3, 1, 7, 0, 10, 2]
n := LENGTH(arr)

// Sorting using Merge Sort


merge_sort_recursive(arr, 0, n - 1)

// Output
WRITE(arr)
END

FUNCTION merge_sort_recursive(arr: ARRAY OF INTEGER, left: INTEGER,


right: INTEGER)
VAR
mid: INTEGER
BEGIN
IF left < right THEN
// Find the middle point
mid := (left + right) / 2

// Recursive calls for left and right halves


merge_sort_recursive(arr, left, mid)
merge_sort_recursive(arr, mid + 1, right)

// Merge the sorted halves


CALL merge(arr, left, mid, right)
END IF
END

FUNCTION merge(arr: ARRAY_OF INTEGER, left: INTEGER, mid: INTEGER,


right: INTEGER)
VAR
i, j, k, n1, n2: INTEGER
L, R: ARRAY OF INTEGER
BEGIN
// Sizes of two subarrays to be merged
n1 := mid - left + 1
n2 := right - mid

// Create temporary arrays


L := ARRAY_OF [n1]:INTEGER
R := ARRAY_OF [n2]:INTEGER
// Copy data to temporary arrays L[] and R[]
FOR i FROM 0 TO n1 - 1 DO
L[i] := arr[left + i]
END FOR
FOR j FROM 0 TO n2 - 1 DO
R[j] := arr[mid + 1 + j]
END FOR

// Merge the temporary arrays back into arr[left..right]


i := 0
j := 0
k := left
WHILE i < n1 AND j < n2 DO
IF L[i] <= R[j] THEN
arr[k] := L[i]
i := i + 1
ELSE
arr[k] := R[j]
j := j + 1
END IF
k := k + 1
END WHILE

// Copy the remaining elements of L[], if there are any


WHILE i < n1 DO
arr[k] := L[i]
i := i + 1
k := k + 1
END WHILE

// Copy the remaining elements of R[], if there are any


WHILE j < n2 DO
arr[k] := R[j]
j := j + 1
k := k + 1
END WHILE
END

Problem 3: Selection Sort


Description: Implement the Selection Sort algorithm to sort an array of integers in
ascending order.



Sample Input:

[4, 1, 8, 3, 7, 5]

Sample Output:

[1, 3, 4, 5, 7, 8]


Algorithm Implementation:

ALGORITHM selection_sort
VAR
arr: ARRAY OF INTEGER
n, i, j, min_index, temp: INTEGER
BEGIN
// Input
arr := [4, 1, 8, 3, 7, 5]
n := arr.length

// Sorting using Selection Sort


FOR i FROM 0 TO n - 2 DO
min_index := i
FOR j FROM i + 1 TO n - 1 DO
IF arr[j] < arr[min_index] THEN
// Update the index of the minimum element
min_index := j
END IF
END FOR

// Swap the found minimum element with the first element


temp := arr[i]
arr[i] := arr[min_index]
arr[min_index] := temp
END FOR

// Output
write(arr)
END

Problem 4: Insertion Sort


Description: Implement the Insertion Sort algorithm to sort an array of integers in
ascending order.

Sample Input:

[6, 2, 9, 4, 3, 8]

Sample Output:

[2, 3, 4, 6, 8, 9]

Algorithm Implementation:

ALGORITHM insertion_sort
VAR
arr: ARRAY OF INTEGER
n, i, key, j: INTEGER
BEGIN
// Input
arr := [6, 2, 9, 4, 3, 8]
n := LENGTH(arr)
// Sorting using Insertion Sort
FOR i FROM 1 TO n - 1 DO
key := arr[i]
j := i - 1

// Move elements of arr[0..i-1] that are greater than key to


one position ahead of their current position
WHILE j >= 0 AND arr[j] > key DO
arr[j + 1] := arr[j]
j := j - 1
END WHILE

arr[j + 1] := key
END FOR

// Output
WRITE(arr)
END

You might also like