0% found this document useful (0 votes)
28 views13 pages

Lab Sorting

The document describes different sorting algorithms including insertion sort, selection sort, bubble sort, merge sort, heap sort and radix sort through pseudocode and examples. Tracing examples are also provided to demonstrate how each algorithm would sort a sample data set in ascending and descending order.

Uploaded by

Aini Ayunni
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)
28 views13 pages

Lab Sorting

The document describes different sorting algorithms including insertion sort, selection sort, bubble sort, merge sort, heap sort and radix sort through pseudocode and examples. Tracing examples are also provided to demonstrate how each algorithm would sort a sample data set in ascending and descending order.

Uploaded by

Aini Ayunni
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/ 13

Sorting

Pseudocode (from lecture’s slide):

InsertionSort(data[])
for i=1 to data.length-1
tmp = data[i];
move all elements data[j] greater than tmp by one position;
place tmp in its proper position;

SelectionSort(data[])
for i=0 to data.length-2
select the smallest element among data[i], i+1, data[data.length-1];
swap it with data[i];

BubbleSort(data[])
for i=0 to data.length-2
for j=data.length-1 down to i+1
if elements in position j and j-1 are out of order
swap them;

MergeSort(data[])
if first < last
mid = (first+last) / 2;
mergesort(data[], first, mid);
mergesort(data[], mid+1, last);
merge(data[], first, last);
HeapSort(data[])
transform data[] into a heap;
for i=data.length-1 down to 2
swap the root with the element in position i;
restore the heap property for the tree data[0], … , data[i-1];

RadixSort(data[])
for d=1 to the position of the left most digit of the longest number
distribute all numbers in data[] among queues 0 through 9
according to the d-th digit;
put all integers in data[];

Heap Sort Example (from lecture’s slide):

(Given) (Final Answer)

Steps can refer to pseudocode. (Got some root changing process)

Garis putus-putus bermakna nombor tu telah ada pada tempatnya (will vanish in heap graph)
Sort Tracing:

1) Trace insertion sort, selection sort, bubble sort, merge sort and radix sort ascendingly using
these data manually:-

Insertion sort:

Pass 1: 9 5 20 1 4 3 2 7 13 25
compare 9 & 5 -> swap
Pass 2: 5 9 20 1 4 3 2 7 13 25
compare 9 & 20 -> stop
Pass 3: 5 9 20 1 4 3 2 7 13 25
compare 1 & 20 -> swap
compare 1 & 9 -> swap
compare 1 & 5 -> swap
Pass 4: 1 5 9 20 4 3 2 7 13 25
compare 4 & 20 -> swap
compare 4 & 9 -> swap
compare 4 & 5 -> swap
compare 4 & 1 -> stop
Pass 5: 1 4 5 9 20 3 2 7 13 25
compare 3 & 20 -> swap
compare 3 & 9 -> swap
compare 3 & 5 -> swap
compare 3 & 4 -> swap
compare 3 & 1 -> stop
Pass 6: 1 3 4 5 9 20 2 7 13 25
compare 2 & 20 -> swap
compare 2 & 9 -> swap
compare 2 & 5 -> swap
compare 2 & 4 -> swap
compare 2 & 3 -> swap
compare 2 & 1 -> stop
Pass 7: 1 2 3 4 5 9 20 7 13 25
compare 7 & 20 -> swap
compare 7 & 9 -> swap
compare 7 & 5 -> stop
Pass 8: 1 2 3 4 5 7 9 20 13 25
compare 13 & 20 -> swap
compare 13 & 9 -> stop
1 2 3 4 5 7 9 13 20 25
Selection sort:

Pass 1: 9 5 20 1 4 3 2 7 13 25
smallest element 1 and swap with 9
Pass 2: 1 5 20 9 4 3 2 7 13 25
smallest element 2 and swap with 5
Pass 3: 1 2 20 9 4 3 5 7 13 25
smallest element 3 and swap with 20
Pass 4: 1 2 3 9 4 20 5 7 13 25
smallest element 4 and swap with 9
Pass 5: 1 2 3 4 9 20 5 7 13 25
smallest element 5 and swap with 9
Pass 6: 1 2 3 4 5 20 9 7 13 25
smallest element 7 and swap with 20
Pass 7: 1 2 3 4 5 7 9 20 13 25
smallest element 13 and swap with 20
1 2 3 4 5 7 9 13 20 25
Bubble sort:

Pass 1: 9 5 20 1 4 3 2 7 13 25
compare 9 & 5 -> swap
compare 9 & 20 -> no swap
compare 20 & 1 -> swap
compare 20 & 4 -> swap
compare 20 & 3 -> swap
compare 20 & 2 -> swap
compare 20 & 7 -> swap
compare 20 & 13 -> swap
compare 20 & 25 -> no swap
Pass 2: 5 9 1 4 3 2 7 13 20 25
compare 5 & 9 -> no swap
compare 9 & 1 -> swap
compare 9 & 4 -> swap
compare 9 & 3 -> swap
compare 9 & 2 -> swap
compare 9 & 7 -> swap
compare 9 & 13 -> no swap
compare 13 & 20 -> no swap
compare 20 & 25 -> no swap
Pass 3: 5 1 4 3 2 7 9 13 20 25
compare 5 & 1 -> swap
compare 5 & 4 -> swap
compare 5 & 3 -> swap
compare 5 & 2 -> swap
compare 5 & 7 -> no swap
compare 7 & 9 -> no swap
compare 9 & 13 -> no swap
compare 13 & 20 -> no swap
compare 20 & 25 -> no swap
Pass 4: 1 4 3 2 5 7 9 13 20 25
compare 1 & 4 -> no swap
compare 4 & 3 -> swap
compare 4 & 2 -> swap
compare 4 & 5 -> no swap
compare 5 & 7 -> no swap
compare 7 & 9 -> no swap
compare 9 & 13 -> no swap
compare 13 & 20 -> no swap
compare 20 & 25 -> no swap
Pass 5: 1 3 2 4 5 7 9 13 20 25
compare 1 & 3 -> no swap
compare 3 & 2 -> swap
compare 3 & 4 -> no swap
compare 4 & 5 -> no swap
compare 5 & 7 -> no swap
compare 7 & 9 -> no swap
compare 9 & 13 -> no swap
compare 13 & 20 -> no swap
compare 20 & 25 -> no swap
1 2 3 4 5 7 9 13 20 25
Merge sort:
Radix sort:

Pass 1: Sorted for Units Place Digit (Sa)

Bucket
0 1 2 3 4 5 6 7 8 9
20 1 2 3 4 5 7 9
13 25

20 1 2 3 13 4 5 25 7 9
Pass 2: Sorted for Tens Place Digits (Puluh)

Bucket
0 1 2 3 4 5 6 7 8 9
1 13 20
2 25
3
4
5
7
9

1 2 3 4 5 7 9 13 20 25
2) Trace insertion sort, selection sort, bubble sort, merge sort and radix sort descending using
these data manually:-

Insertion sort:

Pass 1: 9 5 20 1 4 3 2 7 13 25
compare 9 & 5 -> stop
Pass 2: 9 5 20 1 4 3 2 7 13 25
compare 20 & 5 -> swap
compare 20 & 9 -> swap & stop
Pass 3: 20 9 5 1 4 3 2 7 13 25
compare 1 & 5 -> stop
Pass 4: 20 9 5 1 4 3 2 7 13 25
compare 4 & 1 -> swap
compare 4 & 5 -> stop
Pass 5: 20 9 5 4 1 3 2 7 13 25
compare 3 & 1 -> swap
compare 3 & 4 -> stop
Pass 6: 20 9 5 4 3 1 2 7 13 25
compare 2 & 1 -> swap
compare 2 & 3 -> stop
Pass 7: 20 9 5 4 3 2 1 7 13 25
compare 7 & 1 -> swap
compare 7 & 2 -> swap
compare 7 & 3 -> swap
compare 7 & 4 -> swap
compare 7 & 5 -> swap
compare 7 & 9 -> stop
Pass 8: 20 9 7 5 4 3 2 1 13 25
compare 13 & 1 -> swap
compare 13 & 2 -> swap
compare 13 & 3 -> swap
compare 13 & 4 -> swap
compare 13 & 5 -> swap
compare 13 & 7 -> swap
compare 13 & 9 -> swap
compare 13 & 20 -> stop
Pass 9: 20 13 9 7 5 4 3 2 1 25
compare 25 & 1 -> swap
compare 25 & 2 -> swap
compare 25 & 3 -> swap
compare 25 & 4 -> swap
compare 25 & 5 -> swap
compare 25 & 7 -> swap
compare 25 & 9 -> swap
compare 25 & 13 -> swap
compare 25 & 20 -> swap & stop
25 20 13 9 7 5 4 3 2 1
Selection sort:

Pass 1: 9 5 20 1 4 3 2 7 13 25
largest element 25 and swap with 9
Pass 2: 25 5 20 1 4 3 2 7 13 9
largest element 20 and swap with 5
Pass 3: 25 20 5 1 4 3 2 7 13 9
largest element 13 and swap with 5
Pass 4: 25 20 13 1 4 3 2 7 5 9
largest element 9 and swap with 1
Pass 5: 25 20 13 9 4 3 2 7 5 1
largest element 7 and swap with 4
Pass 6: 25 20 13 9 7 3 2 4 5 1
largest element 5 and swap with 3
Pass 7: 25 20 13 9 7 5 2 4 3 1
largest element 4 and swap with 2
Pass 8: 25 20 13 9 7 5 4 2 3 1
largest element 3 and swap with 2
25 20 13 9 7 5 4 3 2 1
Bubble sort:

Pass 1: 9 5 20 1 4 3 2 7 13 25
compare 9 & 5 -> no swap
compare 5 & 20 -> swap
compare 5 & 1 -> no swap
compare 1 & 4 -> swap
compare 1 & 3 -> swap
compare 1 & 2 -> swap
compare 1 & 7 -> swap
compare 1 & 13 -> swap
compare 1 & 25 -> swap
Pass 2: 9 20 5 4 3 2 7 13 25 1
compare 9 & 20 -> swap
compare 9 & 5 -> no swap
compare 5 & 4 -> no swap
compare 4 & 3 -> no swap
compare 3 & 2 -> no swap
compare 2 & 7 -> swap
compare 2 & 13 -> swap
compare 2 & 25 -> swap
compare 2 & 1 -> no swap
Pass 3: 20 9 5 4 3 7 13 25 2 1
Correct ones stop here (answer’s too long,
compare 5 & 1 -> swap
compare 5 & 4 -> swap lack of time, bet you know the drill)
compare 5 & 3 -> swap
compare 5 & 2 -> swap
compare 5 & 7 -> no swap
compare 7 & 9 -> no swap
compare 9 & 13 -> no swap
compare 13 & 20 -> no swap
compare 20 & 25 -> no swap
Pass 4: 25 20 9 5 4 3 2 7 13 1
compare 1 & 4 -> no swap
compare 4 & 3 -> swap
compare 4 & 2 -> swap
compare 4 & 5 -> no swap
compare 5 & 7 -> no swap
compare 7 & 9 -> no swap
compare 9 & 13 -> no swap
compare 13 & 20 -> no swap
compare 20 & 25 -> no swap
Pass 5: 25 20 9 5 4 3 2 13 7 1
compare 1 & 3 -> no swap
compare 3 & 2 -> swap
compare 3 & 4 -> no swap
compare 4 & 5 -> no swap
compare 5 & 7 -> no swap
compare 7 & 9 -> no swap
compare 9 & 13 -> no swap
compare 13 & 20 -> no swap
compare 20 & 25 -> no swap
Pass 6: 25 20 9 5 4 3 13 7 2 1
compare 25 & 20 -> no swap
compare 20 & 9 -> no swap
compare 9 & 5 -> no swap
compare 5 & 4 -> no swap
compare 4 & 3 -> no swap
compare 3 & 13 -> swap
compare 3 & 7 -> swap
compare 3 & 2 -> no swap
compare 2 & 1 -> no swap
Pass 7: 25 20 9 5 4 13 7 3 2 1
compare 25 & 20 -> no swap
compare 20 & 9 -> no swap
compare 9 & 5 -> no swap
compare 5 & 4 -> no swap
compare 4 & 13 -> swap
compare 4 & 7 -> swap
compare 4 & 3 -> no swap
compare 3 & 2 -> no swap
compare 2 & 1 -> no swap
Pass 8: 25 20 9 5 13 7 4 3 2 1
compare 25 & 20 -> no swap
compare 20 & 9 -> no swap
compare 9 & 5 -> no swap
compare 5 & 13 -> swap
compare 5 & 7 -> swap
compare 5 & 4 -> no swap
compare 4 & 3 -> no swap
compare 3 & 2 -> no swap
compare 2 & 1 -> no swap
Pass 9: 25 20 9 13 7 5 4 3 2 1
compare 25 & 20 -> no swap
compare 20 & 9 -> no swap
compare 9 & 13 -> swap
compare 9 & 7 -> no swap
compare 7 & 5 -> no swap
compare 5 & 4 -> no swap
compare 4 & 3 -> no swap
compare 3 & 2 -> no swap
compare 2 & 1 -> no swap
25 20 13 9 7 5 4 3 2 1
Merge sort:
Radix sort:

Pass 1: Sorted for Units Place Digit (Sa)

Bucket
9 8 7 6 5 4 3 2 1 0
9 7 5 4 3 2 1 20
25 13

9 7 25 5 4 13 3 2 1 20
Pass 2: Sorted for Tens Place Digits (Puluh)

Bucket
9 8 7 6 5 4 3 2 1 0
20 13 1
25 2
3
4
5
7
9

25 20 13 9 7 5 4 3 2 1

You might also like