You are on page 1of 9

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 416: Data Structures and Algorithms (Lab)

Lab 2 Manual
Searching and Sorting Algorithms

Instructor & Demonstrator: Engr. Nauman Ahmad Tariq

Student Name USMAN ALI

Roll No. ELEN51F20R020

Date Performed

Marks obtained

Instructor Signature
CLO-1 Illustrate understanding of key concepts of Data structures
and Algorithms using Dev C++ Platform
CLO-2 Design a programming application by applying concepts of
Data Structures and algorithms leading to the solution of a moderate
scale-programming problem.
CLO-3 Write lab notes, effective communication and the analysis of
the given problem to perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or
individually with respect to the contribution.

OBJECTIVES:
• Implement Linear Search Algorithm in C++
• Implement Binary Search Algorithm in C++
• Implement Bubble Sort Algorithm in C++

ABOUT THE EXPERIMENT:


LINEAR SEARCH
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all
items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise
the search continues till the end of the data collection.
A simple approach is to do linear search, i.e

• Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
• If x matches with an element, return the index.
• If x doesn’t match with any of elements, return -1.

Algortihm:
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Binary Search
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly
dividing in half the portion of the list that could contain the item, until you've narrowed down the possible
locations to just one.

Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].

A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another approach
to perform the same task is using Binary Search.

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of the interval,
narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value
is found or the interval is empty.

Algorithm:
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n

while x not found


if upperBound < lowerBound
EXIT: x does not exists.

set midPoint = lowerBound + ( upperBound - lowerBound ) / 2

if A[midPoint] < x
set lowerBound = midPoint + 1

if A[midPoint] > x
set upperBound = midPoint - 1

if A[midPoint] = x
EXIT: x found at location midPoint
end while

end procedure

Bubble Sort
Bubble Sort is a simple algorithm which is used to sort a given set of n elements provided in form of an array
with n number of elements. Bubble Sort compares all the element one by one and sort them based on their
values.

If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first
element of the array with the second element, if the first element is greater than the second element, it will
swap both the elements, and then move on to compare the second and the third element, and so on.

If we have total n elements, then we need to repeat this process for n-1 times.

It is known as bubble sort, because with every complete iteration the largest element in the given array,
bubbles up towards the last place or the highest index, just like a water bubble rises up to the water surface.

Sorting takes place by stepping through all the elements one-by-one and comparing it with the adjacent
element and swapping them if required.

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if
they are in wrong order.

Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap
them.

Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.

Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Algorithm
Following are the steps involved in bubble sort (for sorting a given array in ascending order):

1. Starting with the first element (index = 0), compare the current element with the next element of the
array.
2. If the current element is greater than the next element of the array, swap them.
3. If the current element is less than the next element, move to the next element. Repeat Step 1.
C++ Programs Codes:
1. LINEAR SEARCH:
#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int x) {


for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i; }
}
return -1;
}

int main() {
int n, x;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the value to search for: ";
cin >> x;
int result = linearSearch(arr, n, x);
if (result != -1) {
cout << "Element " << x << " found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;}

2. BINARY SEARCH:
#include <iostream>
using namespace std;

int binarySearch(int arr[], int n, int x) {


n=100;
int lowerBound = 10;
int upperBound = n - 1;

while (lowerBound <= upperBound) {


int midPoint = lowerBound + (upperBound - lowerBound) / 2;

if (arr[midPoint] == x) {
return midPoint;

} else if (arr[midPoint] < x) {


lowerBound = midPoint + 1;
} else {
upperBound = midPoint - 1;
}
}

return -1;
}

int main() {
int n, x;

cout << "Enter the number of elements in the sorted array: ";
cin >> n;

int arr[n];

cout << "Enter " << n << " sorted elements:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Enter the value to search for: ";


cin >> x;

int result = binarySearch(arr, n, x);

if (result != -1) {
cout << "Element " << x << " found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

return 0;
}
3. BUBBLE SORT:
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


bool swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}

int main() {
int n;

cout << "Enter the number of elements in the array: ";


cin >> n;

int arr[n];

cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

bubbleSort(arr, n);

cout << "Sorted array in ascending order:" << endl;


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
Output:
1. Linear search:

2. Binary search:

3. Bubble sort:
Assessment Rubric for Lab 2
Method of Evaluation: Lab report.

Outcomes Assessed:

CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using Dev
C++ Platform.
CLO-2 Design a programming application by applying concepts of Data Structures and
algorithms leading to the solution of a moderate scale-programming problem.
CLO-3 Write lab notes, effective communication and analysis of the given problem to
perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually with respect to the
contribution.
Performance 5 Excellent 4 Good 3 Satisfactory 2-1 Needs Marks
Improvement
Realization of Fully understand & Close to fully Partially Unable to
Experiment able to illustrate understand & able understands & able understand & able
CLO-1 Linear, Binary to illustrate Linear, to illustrate Linear, to illustrate Linear,
Search and Bubble Binary Search and Binary Search and Binary Search and
Sort Algorithm Bubble Sort Bubble Sort Bubble Sort
Algorithm Algorithm Algorithm
Conducting Completely able to Close to Partially able to Unable to
Experiment successfully completely able to successfully successfully
CLO-2 design and compile successfully design and compile design and compile
C++ programs for design and compile C++ programs for C++ programs for
Linear, Binary C++ programs for Linear, Binary Linear, Binary
Search and Bubble Linear, Binary Search and Bubble Search and Bubble
Sort Algorithm Search and Bubble Sort Algorithm Sort Algorithm.
Sort Algorithm
Data Collection Completely Close to Partially Unable to
and Data Analysis understand & able completely understand & able understand & able
CLO-3 to demonstrate understand & able to demonstrate to demonstrate
syntax of Linear, to demonstrate syntax of Linear, syntax of Linear,
Binary Search and syntax of Linear, Binary Search and Binary Search and
Bubble Sort Binary Search and Bubble Sort Bubble Sort
Algorithm Bubble Sort Algorithm Algorithm
Algorithm
Individual/Team Completely able to Close to Partially able to Unable to execute
Work execute different completely able to execute different different programs
CLO-4 programs for execute different programs for for Linear, Binary
Linear, Binary programs for Linear, Binary Search and Bubble
Search and Bubble Linear, Binary Search and Bubble Sort Algorithm
Sort Algorithm Search and Bubble Sort Algorithm
Sort Algorithm

Total

You might also like