You are on page 1of 7

Electrical Engineering Department

CS243L: Data Structures and Algorithms

Course Instructor: Momina Azam Dated: November 1, 2021

Lab Engineer: Muhammad Usama Riaz


Semester: 3rd

Session: 2020-2024 Batch: BSEE2020

Lab. 6 Implementation and analysis of counting sort

Name Roll No Lab Report Marks/100 Total Marks


(Scaled out of 10)
Muhammad Hamza
Shahid BSEE18078

Checked on:

Signature:
6.1 Objective
The goal of this handout is to learn about how counting sort is used to sort an integer array and
how it is better in some cases.
6.2 Equipment and Component
Component Value Quantity
Description
Computer Available in lab 1

6.3 Conduct of Lab


1. Students are required to perform this experiment individually.
2. In case the lab experiment is not understood, the students are advised to seek help from
the course instructor, lab engineers, assigned teaching assistants (TA) and lab attendants.

6.4 Theory and Background


Counting sort is in a class of algorithms called ‘no comparison’ sorting algorithms.
Counting sort is a sorting algorithm that sorts the elements of an array by counting the
number of occurrences of each unique element in the array. The count is stored in an
auxiliary (Other) array and the sorting is done by mapping the count as an index of the
auxiliary array.

6.4.1 How Counting Sort Works?

1. Find out the maximum element (let it be max) from the given array.

Given

array
2. Initialize an array of length with all elements 0. This array is used for storing the count of

the elements in the array.

Count

Array.
3. Store the count of each element at their respective index count array
in

For example: if the count of element 3 is 2 then, 2 is stored in the 3rd position count array. If
of element "5" is not present in the array, then 0 is stored in 5th position.
Count

of each element stored

4. Store cumulative sum of the elements of the count array. It helps in placing the elements into

the correct index of the sorted array.

Cumulative count

5. Find the index of each element of the original array in the count array. This gives the

cumulative count. Place the element at the index calculated as shown in figure below.

Counting sort

6. After placing each element at its correct position, decrease its count by one.
Lab Task A

Write the function that takes an array of positive integers and sort it out using counting sort, please
comment your code properly explaining on every step. Please take snapshot of output and paste it.
[20 Marks]

#include <iostream>
using namespace std;

int countDigit(int[], int, int);


int main()
{
int input[7] = { 4,2,2,8,3,3,1 };
int maximum = input[0];
int countNum;
cout << "Input Array: ";
for (int i = 0; i < 7; i++) {
cout << input[i] << ", ";
}
cout << endl;
for (int i = 0; i < 7; i++) {
if (input[i] > maximum) {
maximum = input[i];
}
}
int* countArray = new int[maximum + 1];
for (int i = 0; i < maximum + 1; i++) {
countArray[i] = 0;
}
cout << "Count Array: ";
for (int i = 0; i < 7; i++) {
countNum = countDigit(input, 7, input[i]);
countArray[input[i]] = countNum;
}
for (int i = 0; i < maximum + 1; i++) {
cout << countArray[i] << ", ";
}
cout << endl;
int* commulativeSum = new int[maximum + 1];
commulativeSum[0] = countArray[0];
for (int i = 1; i < maximum + 1; i++) {
commulativeSum[i] = commulativeSum[i - 1] + countArray[i];
}
cout << "Commulative Sum Array: ";
for (int i = 0; i < maximum + 1; i++) {
cout << commulativeSum[i] << ", ";
}
cout << endl;
int* output = new int[7];
for (int i = 0; i < 7; i++) {
output[commulativeSum[input[i]] - 1] = input[i];
commulativeSum[input[i]]--;
}
cout << "Output Array: ";
for (int i = 0; i < 7; i++) {
cout << output[i] << ", ";
}
return 0;
}
int countDigit(int in[], int size, int num) {
int count = 0;
for (int i = 0; i < size; i++) {
if (in[i] == num) {
count++;
}
}
return count;
}

Lab Task B

a) Work out the time complexity of the algorithm in Q 1 in terms of number of steps.

b) Briefly discuss whether the counting sort algorithm is an in-place sorting algorithm or not.

A sorting algorithm is said to be adaptive if it will do less amount of work when an already sorted
array is handed to it for sorting. Is counting sort adaptive? Can you name some adaptive algorithms
that we have covered in class?
If two elements in the array have the same key it does not matter what order they are copied in
into the final sorted array, however if these duplicate keys are copied in the same order in which they
occur in the input array the algorithm is said to be stable. Is Counting Sort Algorithm stable? [10
Marks]
a) The time complexity of the counting sort algorithm is O(n+ k ), n is the number of elements in the
array
Student Name Registration# Batch: EE-2020

Assessment Rubrics
Method: Lab reports and instructor observation during lab sessions.
Outcome assessed:
a. Ability to conduct experiments, as well as to analyze and interpret data (P)
b. Ability to function on multi-disciplinary teams (A)
c. Ability to use the techniques, skills, and modern engineering tools necessary for engineering practice (P)

Performance metric Mapping (task no. Max Exceeds expectation Meets expectation Does not meet expectation Obtained
and description) marks marks
1. Realization of 1 Functionality 40 Executes without errors excellent Executes without errors, user Does not execute due to syntax errors,
experiment (a) user prompts, good use of prompts are understandable, runtime errors, user prompts are
symbols, spacing in output. minimum use of symbols or spacing misleading or non-existent. No testing has
Through testing has been in output. Some testing has been been completed (20-0)
completed (45-41) completed (40-21)
2. Teamwork (b) 1 Group 5 Actively engages and cooperates Cooperates with other group Distracts or discourages other group
Performance with other group member(s) in member(s) in a reasonable manner members from conducting the experiment
effective manner (5-4) but conduct can be improved (3-2) (1-0)
3. Conducting 1 On Spot 10 Able to make changes (5-4) Partially able to make changes (3-2) Unable to make changes (1-0)
experiment (a, c) Changes
2 Viva 10 Answered all questions (5-4) Few incorrect answers (3-2) Unable to answer all questions (1-0)
4. Laboratory safety 1 Code 5 Observes lab safety rules; Generally, observes safety rules and Disregards lab safety and disciplinary rules
and disciplinary rules commenting adheres to the lab disciplinary disciplinary guidelines with minor (1-0)
(a) guidelines aptly (5-4) lapses (3-2)
5. Data collection (c) 1 Code Structure 5 Excellent use of white space, Includes name, and assignment, Poor use of white space (indentation, blank
creatively organized work, white space makes the program lines) making code hard to read,
excellent use of variables and fairly easy to read. Title, organized disorganized and messy (1-0)
constants, correct identifiers for work, good use of variables (3-2)
constants, No line-wrap (5-4)
6. Data analysis (a, c) 1 Algorithm 20 Solution is efficient, easy to A logical solution that is easy to A difficult and inefficient solution (1-0)
understand, and maintain (5-4) follow but it is not the most
efficient (3-2)
7. Computer use (c) 1 Documentation 5 Timely documented (5-4) Late documented (3-2) Not documented (1-0)

Max Marks (total): 100 Obtained Marks (Total):

Lab Engineer Signature: ________________________

You might also like