0% found this document useful (0 votes)
20 views4 pages

Lab07 Sorting

Uploaded by

Marwah Al-Helali
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)
20 views4 pages

Lab07 Sorting

Uploaded by

Marwah Al-Helali
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/ 4

CCP6214 Algorithm Design and Analysis

Lab 7 Sorting
Learning Outcome
• Understand how Selection Sort, Heap Sort, and Bucket Sort work, and identify its time complexities.
• Understand how Quick Sort works, and identify its time complexity.

1. [30 min] The following Selection Sort algorithm is used to sort the array of 7 integers below.
Array: 38 9 38 37 155 197 65

void selectionSort (int a[], int n) {


for (int i = n-1; i > 0; i--) {
// find the max element in the unsorted a[i .. n-1].
int maxIndex = i; // assume the max is at the last of unsorted.
// test against elements before i to find the largest.
for (int j = 0; j < i; j++) {
// if this element is larger, then it is the new max.
if (a[j] > a[maxIndex])
// found new max; remember its index.
maxIndex = j;
}
// maxIndex is the index of the max element,
// swap it with the current position.
if (maxIndex != i)
swap (a[i], a[maxIndex]);
}
}

(a) Fill up the table below based on the content at the end of each outer "for" loop iteration or i.

i maxIndex Array
6
5
4
3
2
1

(b) Implement a program with selectionSort to prove that your answer in (a) is correct.

// filename: selectionSort.cpp

1
(c) Fill up the table below and find the time complexity for Selection Sort.

Algorithm Number of primitive operations


for (int i = n-1; i > 0; i--)
int maxIndex = i;
for (int j = 0; j < i; j++)
if (a[j] > a[maxIndex])
maxIndex = j;
if (maxIndex != i)
swap (a[i], a[maxIndex]);
T(n)

2. [40 min] The following Heap Sort algorithm is used to sort the same array in Question 1.
Array: 38 9 38 37 155 197 65

Algorithm Heapsort (S, n)


Input sequence S of n elements
Output sorted sequence S
pq  priority queue (S)
for i  n-1 to 1
pq.dequeue (S[i])

(a) Fill up the table below based on the content at the end of each "for" loop iteration or i.

Heap on pq Sorted Subarray


initial
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1

(b) Implement the heapSort using the following program. Verify the output.

// filename: heapSort.cpp
#include <iostream>
using namespace std;

template <typename T>


void printArray (T A[], int n) {
for (int i = 0; i < n; i++)
cout << A[i] << " ";
cout << endl;
}

void heapify (int A[], int arraySize, int j) {


int max;
int left = 2*j+1;
int right = 2*j+2;

2
if (left < arraySize && A[left] > A[j])
max = left;
else
max = j;
if (right < arraySize && A[right] > A[max])
max = right;
if (max != j) {
swap (A[j], A[max]);
heapify(A, arraySize, max);
}
}

void heapSort (int A[], int arraySize) {


for (int j = (arraySize-1)/2; j >= 0; j--)
heapify (A, arraySize, j);
for (int i = arraySize-1; i >= 1; i--) {
swap (A[0], A[i]);
heapify (A, --arraySize, 0);
}
}

int main () {
const int n = 7;
int A[n] = {38, 9, 38, 37, 155, 197, 65};
cout << "Input: ";
printArray (A, n);
heapSort (A, n);
cout << "After Heap sort: ";
printArray (A, n);
}

3
3. [20 min] Radix Sort is used to sort the same array in Question 1.
Array: 38 9 38 37 155 197 65

Algorithm radixSort(S, N)
Input sequence S of d-tuples such that (0, …, 0)  (x1, …, xd) and
(x1, …, xd)  (N - 1, …, N - 1) for each tuple (x1, …, xd) in S
Output sequence S sorted in lexicographic order
for i  d downto 1
bucketSort(S, N)

(a) Fill up the table below based on the content at the end of each "for" loop iteration or i. Use N = 10.

Original array
38
9
38
37
155
197
65

(b) What is the time complexity for Radix Sort?

4. [30 min] Perform Quick Sort on the array in Question 1. Use the first element as pivot. Assume that the
partition is stable.
Array: 38 9 38 37 155 197 65

(a) Write the result in a Quick Sort tree.


38 9 38 37 155 197 65

(b) Write the result in an array output. Show the steps.

You might also like