2 and 3
2 and 3
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.
Software Requirement:
Operating System – Windows 2007
IDE – Dev C++
Online Platform: www.onlineGDB.com
THEORY:
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:
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.
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)
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.
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:
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:
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:
We can observe in the above figure that a[mid]=data. Therefore, the search is completed, and
the element is found successfully.
ALGORITHM:
FLOWCHART:
CODE:
ALGORITHM:
FLOWCHART:
CODE:
CONCLUSION:
REFERENCE:
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
● RAM : 1GB
Software Requirement:
THEORY:
A sorting algorithm is used to arrange elements of an array/list in a specific order. For
example,
● 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.
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:
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.
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.
o Pivot can either be the rightmost element of the leftmost element of the given array.
To understand the working of quick sort, let's take an unsorted array. It will make the concept
more clear and understandable.
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.
Now, a[pivot] < a[right], so algorithm moves forward one position towards left, i.e. -
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.
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. -
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.
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 -
ALGORITHM:
FLOWCHART:
CODE:
ALGORITHM:
FLOWCHART:
CODE:
CONCLUSION:
REFERENCE: