You are on page 1of 8

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 ReportMarks/100 Total Marks


(Scaled out of 10)
MUHAMMAD UMER BSEE20075
SHAKIR

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 courseinstructor, 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 in count array

For example: if the count of element 3 is 2 then, 2 is stored in the 3rd position of count array. If

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;

void COUNTINGSORTING(int array1[], int size) {

int output[150];
int count[150];
int max,i ;
int m[150];

for (int i = 1; i < size; i++)


{
if (array1[i] > max)
max = array1[i];
}
cout << "MAXIMUM NUMBER IS " << endl;
cout<< max<<endl;

cout << " THE COUNTER ARRAY IS INIHILIZING BY 0" << endl;
for (int i = 0; i <= max; ++i)
{
count[i] = 0;
cout << count[i]<<endl;
}

for (int i = 0; i < size; i++) {

count[array1[i]]++;
}
cout << "COUNTER ARRAY IS " << endl;

for (int i = 1; i <= max; i++) {

cout<<count[i]<<endl;
}
cout << "COMMULATIVE ARRAY IS " << endl;

for (int i = 1; i <= max; i++) {

count[i] += count[i - 1];


cout<<count[i]<<endl;
}

for (int i = size - 1; i >= 0; i--) {


output[count[array1[i]] - 1] = array1[i];
count[array1[i]]--;

}
for (int i = 0; i < size; i++) {
array1[i] = output[i];
}
cout << "THE SORTED ARRAY WE GET IS " << endl;
for (int i = 0; i < size; i++)
cout << array1[i] << endl;
}

int main() {
int array2[150] ;
int INPUT,i;
cout << "PLESE Enter the size of array" << endl;
cin >> INPUT;
cout << "PLEASE Enter the elemnts of array" << endl;
for (i = 0; i < INPUT; i++)
{
cin >> array2[i];
}
COUNTINGSORTING(array2, INPUT);

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]
Complexity

Time Complexity

Best O(n+k)

Worst O(n+k)

Average O(n+k)

Space Complexity O(max)

Stability Yes

Time Complexities
There are mainly four main loops. (Finding the greatest value can be done
outside the function.)

for-loop time of counting

1st O(max)

2nd O(size)

3rd O(max)

4th O(size)

Overall complexity = O(max)+O(size)+O(max)+O(size) = O(max+size)

• Worst Case Complexity: O(n+k)

• Best Case Complexity: O(n+k)

• Average Case Complexity: O(n+k)

In all the above cases, the complexity is the same because no matter how the
elements are placed in the array, the algorithm goes through n+k times.
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