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

2 and 3

Uploaded by

Vaikul Gandi
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)
14 views

2 and 3

Uploaded by

Vaikul Gandi
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/ 33

Data Structure & Algorithm Lab (N-PCCCS302P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.

Practical No. 2
Aim: Develop a program to perform searching:
a) Develop a program to perform searching by applying Linear
Search technique.
b) Develop a program to perform searching by applying Binary
Search technique.

Name of Student: Harsh S. Buwade


Roll No.: CS23048
Semester/Year: 3/2
Academic Session: 2024-2025
Date of Performance: 05/08/2024
Date of Submission: 12/08/2024

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

AIM: Develop a program to perform searching:


a) Develop a program to perform searching by applying Linear Search technique.
b) Develop a program to perform searching by applying Binary Search technique.

OBJECTIVE/EXPECTED LEARNING OUTCOME:


 To be able to understand the concept of Searching.
 To be able to implement Linear Search.
 To be able to implement Binary Search.

Hardware and Software Requirement:


Hardware Requirement:
 Processor : Dual Core
 RAM : 1GB
 Hard Disk Drive : > 80 GB

Software Requirement:
 Operating System – Windows 2007
 IDE – Dev C++
 Online Platform: www.onlineGDB.com

THEORY:

What is a linear search?

A linear search is also known as a sequential search that simply scans each element at a time.
Suppose we want to search an element in an array or list; we simply calculate its length and
do not jump at any item.
Let's consider a simple example.
Suppose we have an array of 10 elements as shown in the below figure:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

The above figure shows an array of character type having 10 values. If we want to search 'E',
then the searching begins from the 0th element and scans each element until the element, i.e.,
'E' is not found. We cannot directly jump from the 0 th element to the 4th element, i.e., each
element is scanned one by one till the element is not found.

Complexity of Linear search:

As linear search scans each element one by one until the element is not found. If the number
of elements increases, the number of elements to be scanned is also increased. We can say
that the time taken to search the elements is proportional to the number of elements.
Therefore, the worst-case complexity is O(n)

What is a Binary search?

A binary search is a search in which the middle element is calculated to check whether it is
smaller or larger than the element which is to be searched. The main advantage of using
binary search is that it does not scan each element in the list. Instead of scanning each
element, it performs the searching to the half of the list. So, the binary search takes less time
to search an element as compared to a linear search. The one pre-requisite of binary
search is that an array should be in sorted order, whereas the linear search works on both
sorted and unsorted array. The binary search algorithm is based on the divide and conquer
technique, which means that it will divide the array recursively.
There are three cases used in the binary search:
Case 1: data<a[mid] then left = mid+1.
Case 2: data>a[mid] then right=mid-1
Case 3: data = a[mid] // element is found
In the above case, 'a' is the name of the array, mid is the index of the element calculated
recursively, data is the element that is to be searched, left denotes the left element of the
array and right denotes the element that occur on the right side of the array.
Let's understand the working of binary search through an example.
Suppose we have an array of 10 size which is indexed from 0 to 9 as shown in the below
figure:
We want to search for 70 element from the above array.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Step 1: First, we calculate the middle element of an array. We consider two variables, i.e.,
left and right. Initially, left =0 and right=9 as shown in the below figure:

The middle element value can be calculated as:

Therefore, mid = 4 and a[mid] = 50. The element to be searched is 70, so a[mid] is not equal
to data. The case 2 is satisfied, i.e., data>a[mid].

Step 2: As data>a[mid], so the value of left is incremented by mid+1, i.e., left=mid+1. The
value of mid is 4, so the value of left becomes 5. Now, we have got a subarray as shown in
the below figure:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Now again, the mid-value is calculated by using the above formula, and the value of mid
becomes 7. Now, the mid can be represented as:

In the above figure, we can observe that a[mid]>data, so again, the value of mid will be
calculated in the next step.
Step 3: As a[mid]>data, the value of right is decremented by mid-1. The value of mid is 7, so
the value of right becomes 6. The array can be represented as:

The value of mid will be calculated again. The values of left and right are 5 and 6,
respectively. Therefore, the value of mid is 5. Now the mid can be represented in an array as
shown below:

In the above figure, we can observe that a[mid]<data.


Step 4: As a[mid]<data, the left value is incremented by mid+1. The value of mid is 5, so the
value of left becomes 6.
Now the value of mid is calculated again by using the formula which we have already
discussed. The values of left and right are 6 and 6 respectively, so the value of mid becomes 6
as shown in the below figure:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

We can observe in the above figure that a[mid]=data. Therefore, the search is completed, and
the element is found successfully.

a) Develop a program to perform searching by applying Linear Search technique.

ALGORITHM:

FLOWCHART:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

CODE:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

INPUT & OUTPUT (With Different Test Cases):

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

b) Develop a program to perform searching by applying Binary Search technique.

ALGORITHM:

FLOWCHART:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

CODE:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

INPUT & OUTPUT (With Different Test Cases):

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

CONCLUSION:

DISCUSSION AND VIVA VOCE:


Q.1) What is Searching?
Q. 2) What are various types of searching?
Q. 3) Can we perform binary search on unsorted array?
Q. 4) How can you perform searching in queue?
Q. 5) How to perform searching in BST?
Q. 6) How to search an element in stack memory?

REFERENCE:

● Data Structures and Algorithms Concepts, Techniques and Applications, G A

Vijayalakshmi Pai, Mc-Graw Hill Education (India) Ltd. 2008.

● Data Structures Using C, E Balgurusamy, Mc-Graw Hill Education (India) Ltd.

● Data Structures Through C, Yashwant Kanetkar, BPB Publications.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.

Practical No. 3
Aim: Build a program to perform sorting:
a) Build a program to perform sorting by applying Selection Sort
b) Build a program to perform sorting by applying Quick Sort

Name of Student: Harsh S. Buwade


Roll No.: CS23048
Semester/Year: 3/2
Academic Session: 2024-2025
Date of Performance: 05/08/2024
Date of Submission: 12/08/2024

AIM: Build a program to perform sorting:


a) Build a program to perform sorting by applying Selection Sort
b) Build a program to perform sorting by applying Quick Sort

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

OBJECTIVE/EXPECTED LEARNING OUTCOME:

● To be able to understand the concept of sorting.

● To be will be able to understand the various sorting techniques.

● To be able to implement Selection sort and Quick sort.

Hardware and Software Requirement:


Hardware Requirement:

● Processor : Dual Core

● RAM : 1GB

● Hard Disk Drive : > 80 GB

Software Requirement:

● Operating System – Windows 2007

● IDE – Dev C++

● Online Platform: www.onlineGDB.com

THEORY:
A sorting algorithm is used to arrange elements of an array/list in a specific order. For
example,

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Here, we are sorting the array in ascending order.


There are various sorting algorithms that can be used to complete this operation. And, we can
use any algorithm based on the requirement.

Different Sorting Algorithms:

● Bubble Sort

● Selection Sort

● Insertion Sort

● Merge Sort

● Quicksort

● Counting Sort

● Radix Sort

● Bucket Sort

● Heap Sort

● Shell Sort

Selection Sort:
Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting
the smallest (or largest) element from the unsorted portion of the list and moving it to the
sorted portion of the list. The algorithm repeatedly selects the smallest (or largest) element
from the unsorted portion of the list and swaps it with the first element of the unsorted part.
This process is repeated for the remaining unsorted portion until the entire list is sorted.

How does Selection Sort Algorithm work?


Lets consider the following array as an example: arr[] = {64, 25, 12, 22, 11}

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

First pass:
For the first position in the sorted array, the whole array is traversed from index 0 to 4
sequentially. The first position where 64 is stored presently, after traversing whole array it is
clear that 11 is the lowest value.
Thus, replace 64 with 11. After one iteration 11, which happens to be the least value in the
array, tends to appear in the first position of the sorted list.

Second Pass:
For the second position, where 25 is present, again traverse the rest of the array in a
sequential manner.
After traversing, we found that 12 is the second lowest value in the array and it should appear
at the second place in the array, thus swap these values.

Third Pass:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Now, for third place, where 25 is present again traverse the rest of the array and find the third
least value present in the array.
While traversing, 22 came out to be the third least value and it should appear at the third
place in the array, thus swap 22 with element present at third position.

Fourth pass:
Similarly, for fourth position traverse the rest of the array and find the fourth least element in
the array. As 25 is the 4th lowest value hence, it will place at the fourth position.

Fifth Pass:
At last the largest value present in the array automatically get placed at the last position in the
array. The resulted array is the sorted array.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Quick Sort:
Sorting is a way of arranging items in a systematic manner. Quicksort is the widely used
sorting algorithm that makes n log n comparisons in average case for sorting an array of n
elements. It is a faster and highly efficient sorting algorithm. This algorithm follows the
divide and conquer approach. Divide and conquer is a technique of breaking down the
algorithms into subproblems, then solving the subproblems, and combining the results back
together to solve the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into
two sub-arrays such that each element in the left sub-array is less than or equal to the pivot
element and each element in the right sub-array is larger than the pivot element.
Conquer: Recursively, sort two subarrays with Quicksort.
Combine: Combine the already sorted array.
Quicksort picks an element as pivot, and then it partitions the given array around the picked
pivot element. In quick sort, a large array is divided into two arrays in which one holds values
that are smaller than the specified value (Pivot), and another array holds the values that are
greater than the pivot.
After that, left and right sub-arrays are also partitioned using the same approach. It will
continue until the single element remains in the sub-array.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Choosing the pivot:


Picking a good pivot is necessary for the fast implementation of quicksort. However, it is
typical to determine a good pivot. Some of the ways of choosing a pivot are as follows -
o Pivot can be random, i.e. select the random pivot from the given array.

o Pivot can either be the rightmost element of the leftmost element of the given array.

o Select median as the pivot element.

Working of Quick Sort Algorithm:


Now, let's see the working of the Quicksort Algorithm.

To understand the working of quick sort, let's take an unsorted array. It will make the concept
more clear and understandable.

Let the elements of array are -

In the given array, we consider the leftmost element as pivot. So, in this case, a[left] = 24,
a[right] = 27 and a[pivot] = 24.

Since, pivot is at left, so algorithm starts from right and move towards left.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Now, a[pivot] < a[right], so algorithm moves forward one position towards left, i.e. -

Now, a[left] = 24, a[right] = 19, and a[pivot] = 24.

Because, a[pivot] > a[right], so, algorithm will swap a[pivot] with a[right], and pivot moves
to right, as -

Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm starts
from left and moves to right.

As a[pivot] > a[left], so algorithm moves one position to right as -

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Now, a[left] = 9, a[right] = 24, and a[pivot] = 24. As a[pivot] > a[left], so algorithm moves
one position to right as -

Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As a[pivot] < a[left], so, swap a[pivot] and
a[left], now pivot is at left, i.e. -

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Since, pivot is at left, so algorithm starts from right, and move to left. Now, a[left] = 24,
a[right] = 29, and a[pivot] = 24. As a[pivot] < a[right], so algorithm moves one position to
left, as -

Now, a[pivot] = 24, a[left] = 24, and a[right] = 14. As a[pivot] > a[right], so, swap a[pivot]
and a[right], now pivot is at right, i.e. -

Now, a[pivot] = 24, a[left] = 14, and a[right] = 24. Pivot is at right, so the algorithm starts
from left and move to right.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

Now, a[pivot] = 24, a[left] = 24, and a[right] = 24. So, pivot, left and right are pointing the
same element. It represents the termination of procedure.

Element 24, which is the pivot element is placed at its exact position.

Elements that are right side of element 24 are greater than it, and the elements that are left
side of element 24 are smaller than it.

Now, in a similar manner, quick sort algorithm is separately applied to the left and right sub-
arrays. After sorting gets done, the array will be -

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

a) Build a program to perform sorting by applying Selection Sort

ALGORITHM:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

FLOWCHART:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

CODE:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

INPUT & OUTPUT (With Different Test Cases):

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

b) Build a program to perform sorting by applying Quick Sort

ALGORITHM:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

FLOWCHART:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

CODE:

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

INPUT & OUTPUT (With Different Test Cases):

CONCLUSION:

DISCUSSION AND VIVA VOCE:


Q.1) What is sort order?
Q.2) What is stable and unstable sorting algo?
Q.3) How to find efficiency of a sorting algo?
Q.4) What are sorting algorithms available?
Q.5) Explain heap sort and bucket sort algorithm.

REFERENCE:

● Data Structures and Algorithms Concepts, Techniques and Applications, G A

Vijayalakshmi Pai, Mc-Graw Hill Education (India) Ltd. 2008.

● Data Structures Using C, E Balgurusamy, Mc-Graw Hill Education (India) Ltd.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur


Data Structure & Algorithm Lab (N-PCCCS302P)

● Data Structures Through C, Yashwant Kanetkar, BPB Publications.

Department of Computer Science & Engineering, S.B.J.I.T.M.R., Nagpur

You might also like