0% found this document useful (0 votes)
4 views

Practice Question of OOP

The document outlines the lab tasks for a Data Structures and Algorithms course at the University of Central Punjab, focusing on simple sorting and searching algorithms in C++. It includes instructions for coding practices, pseudo code for Selection Sort, Linear Search, and Binary Search, and tasks for implementing these algorithms in C++. Students are required to create a Student class and perform searches based on CGPA after sorting the array using the implemented Selection Sort algorithm.

Uploaded by

shabiiff0317
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Practice Question of OOP

The document outlines the lab tasks for a Data Structures and Algorithms course at the University of Central Punjab, focusing on simple sorting and searching algorithms in C++. It includes instructions for coding practices, pseudo code for Selection Sort, Linear Search, and Binary Search, and tasks for implementing these algorithms in C++. Students are required to create a Student class and perform searches based on CGPA after sorting the array using the implemented Selection Sort algorithm.

Uploaded by

shabiiff0317
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

University of Central Punjab

Faculty of Information Technology

Data Structures and Algorithms


Spring 2025
Lab 01
Topic ● Simple sorting algorithms — Selection sort

● Simple searching algorithms — Linear search, binary search

● Working with classes and multiple files.

The basic purpose of this lab is to revise some preliminary concepts of C++ that has
been covered in the course of Introduction to Computing and Programming
Objectiv
Fundamentals and Object Oriented Programming.
e

Instructions:
• Indent your code.
• Comment your code.
• Use meaningful variable names.
• Plan your code carefully on a piece of paper before you implement it.
• Name of the program should be same as the task name. i.e. the first program should be
Task_1.cpp
• void main() is not allowed. Use int main()
• You have to work in multiple files. i.e separate .h and .cpp files
• You are not allowed to use system("pause")
• You are not allowed to use any built-in functions
• You are required to follow the naming conventions as follow:
o Variables: firstName; (no underscores allowed)
o Function: getName(); (no underscores allowed)
o ClassName: BankAccount (no underscores allowed)

Students are required to complete the following tasks in lab timings.


Selection Sort
Selection sort is a sorting algorithm, in which we repeatedly find the next largest (or
smallest) element in the array and move it to its final position in the sorted array. Assume
that we wish to sort the array in increasing (ascending) order, i.e. the smallest element at
the beginning of the array and the largest element at the end. We begin by selecting the
smallest element and moving it to the lowest index position. We can do this by swapping
the element at the lowest index and the smallest element. We then reduce the effective size
of the array by one element and repeat the process on the greater (sub)array. The process
stops when the effective size of the array becomes 1 (an array of 1 element is already
sorted).

Pseudo code
Input: An unsorted array, A of N elements
Output: Sorted array, A

For I = 0:N-1
SmallSub = I
For J = I+1:N-1
If A[J] < A[SmallSub]
SmallSub = J
End-If
End-For
Swap ( A[I], A[SmallSub] )
End-For

Linear Search
In computer science, linear search or sequential search is a method for finding
a particular value in a list that consists of checking every one of its elements,
one at a time and in sequence, until the desired one is found.

Pseudo code
Input: An unsorted array, A of N elements and value to be searched
Output: Index of searched element or -1 if not found
For I = 0:N-1
If ( A[I] == Value )
Return [I];
End-if
End-For
return -1;

Binary Search
Binary search is another searching algorithm, used to search a specific value (or index of
value) from the sorted array.
In binary search, we first compare the value to be searched with the item in the middle
position of the array. If there's a match, we can return immediately. If the key is less than
the middle key, then the item sought must lie in the lower half of the array; if it's greater
than the item sought must lie in the upper half of the array. So we repeat the procedure on
the lower (or upper) half of the array.

Pseudo Code
Input: An Sorted array, A of N elements and value to be searched
Output: Index of searched element or -1 if not found

low = 0, high = N-1;


while (low <= high)
mid = (low + high)/2;
If ( A[mid] == Value )
Return mid;
Else-if ( A[mid] < Value )
low = mid + 1;
Else
high = mid – 1;
End-if
End-For
return -1;

Task 1
Implement the Selection Sort Algorithm with the help of pseudo code given
above.
Task 2
Create a C++ class named Student, with the following private attributes:
1. regNo: char*
2. CGPA: double
Your task is to create an array of 10 Students. Initialize all objects of the array
with different values and perform the following Search algorithms on them on
the basis of CGPA.
a) Linear Search
b) Binary Search

These functions MUST be implemented as PUBLIC member functions of the


class Student with the help of pseudo code given above.

NOTE: In order to perform Binary Search, the array must be sorted. So you
can use your selection sort function implemented in above task for this
purpose.

You might also like