0% found this document useful (0 votes)
21 views50 pages

Ds Fifth Unit Aug 2020

Uploaded by

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

Ds Fifth Unit Aug 2020

Uploaded by

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

Dr.

Sanjeev Tayal
(HOD)
BCA & B.Sc(C.S)
UNIT V

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Searching
 Searching is the process of finding some particular
element in the list. If the element is present in the list,
then the process is called successful and the process
returns the location of that element, otherwise the
search is called unsuccessful.
There are two popular search methods that are widely
used in order to search some item into the list.
However, choice of the algorithm depends upon the
arrangement of the list.
 Linear Search
 Binary Search
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Linear Search
 Linear search is the simplest search algorithm and
often called sequential search. In this type of
searching, we simply traverse the list completely and
match each element of the list with the item whose
location is to be found. If the match found then
location of the item is returned otherwise the
algorithm return NULL.
Linear search is mostly used to search an unordered
list in which the items are not sorted. The algorithm of
linear search is given as follows.

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Algorithm
 Liner-search(A,N, item)
where A is array with N element & item is the value to be
search
Step1:-input N element of an array A
Step2:- set flag=0
Step3:- repeat for i = 1 to N
If(A[i]== item) then
set flag= 1
set loc= i
end if, end for loop
Step4:-if flag=0 then
set loc=0 and print “item is not found
Else
Print “item found at given position i ”
Step5:-stop
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Complexity of algorithm
Complexity Best Case Average Case Worst Case

Time O(1) O(n) O(n)

Space
O(1)

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Binary Search
 Binary search is the search technique which works
efficiently on the sorted lists. Hence, in order to search an
element into some list by using binary search technique,
we must ensure that the list is sorted.
Binary search follows divide and conquer approach in
which, the list is divided into two halves and the item is
compared with the middle element of the list. If the match
is found then, the location of middle element is returned
otherwise, the array divided into two part one starting
from 0 to N/2-1 and second partition(right side) N/2+1 to
N-1.this process continues until the search element is equal
to the middle element .

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Binary search algorithm is given below.
Binary-search(A ,N , item)
where A is array with N element & item is the value to be search .Beg
denotes first element and End denotes last element & Mid denotes middle value
in the array .
Step1:- input N element with sorted element in array A.
Step2:-set Beg=1 and End=N
Step3:-set Mid=(Beg +End )/2
Step4:- Repeat while[(Beg<=End) and (A[Mid]!=item)]
if(item < A[mid]) then
set End=Mid-1
else
set Beg=Mid+1
end if
Step5:- set Mid=(Beg + End)/2
end while loop step 4
Step6:- if(A[mid]== item) then
print ” item is found at location mid”
else
print “ item is not found”
endif
Step 7:- stop
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Complexity
SN Performance Complexity

1 Worst case O(log n)

2 Best case O(1)

3 Average Case O(log n)

4 Worst case space O(1)


complexity

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Sorting
 Sorting refers to arranging a list of element in a particular
order. i.e the element are arranged in increasing or
decreasing order of their key values
when the data is in some sorted order searching a data is
easier . Many type of sorting some sorting given below
1. Bubble sort
2. Insertion sort
3. Selection sort
4. Quick sort
5. Marge sort
6. Heap sort
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Bubble Sort
In Bubble sort, Each
element of the array is
compared with its
adjacent element. The
algorithm processes the
list in passes. A list with n
elements requires n-1
passes for sorting.
Consider an array A of n
elements whose elements
are to be sorted by using
Bubble sort.
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Algorithm Bubble Sort:-
Bubble sort (A,N)
where A be an array of n element .temp is temporary variable
Step1:- input N element of an array A
Step2:-Repeat for J=1 to N
Step3:- Repeat for k=1 to N-J
step4.:- if([A[K]>A[K+1] then swap A[k] & A[K+1]
temp=A[k]
A[k]=A[K+1]
A[K+1]=temp
End of if end both for loop step2 and 3.
Step5:- Display the sorted element of array A
Step6:- Exit
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Complexity
Scenario Complexity

Space O(1)
Worst case running time O(n2)
Average case running time O(n)
Best case running time O(n2)

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Insertion Sort
Insertion sort is the simple
sorting algorithm which is
commonly used in the
daily lives while ordering a
deck of cards. In this
algorithm, we insert each
element onto its proper
place in the sorted array.
This is less efficient than
the other sort algorithms
like quick sort, merge sort,
etc.

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Algorithm Insertion Sort
Insertion-sort(A,N)
let A be an array of N element
Step1:- input N element of an array A
Step2:-Repeat for J=2 to N
set tem=A[J]
k=J-1
Step3:- Repeat while( (k>=1) and (A[k]>temp))
set A[k+1]= A[k]
set k=k-1
end of while loop step3
Step4:-set A[k+1] =temp
end of for loop step2
Step5:-Display the sorted element of array A
Step6:- Exit

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Complexity
Complexity Best Case Average Worst Case
Case

Time Ω(n) θ(n2) o(n2)

Space o(1)

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Selection Sort
 In selection sort, the smallest
value among the unsorted
elements of the array is
selected in every pass and
inserted to its appropriate
position into the array.
First, find the smallest element
of the array and place it on the
first position. Then, find the
second smallest element of the
array and place it on the
second position. The process
continues until we get the
sorted array.
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Algorithm Selection Sort
Selection-sort(A,N)
let A be an array of N element
Step1:- input N element of an array A
Step2:- Repeat for J=1 to N
set min=J
Step3:-Repeat for k=J+1 to N
if(A[k]<A[min]) then
set min=k
end if end for loop step 3
Step4:-swap A[J] & A[min]
temp=A[J] , A[J]=A[min], A[min]=temp
end of step for loop 2
Step5:- Display the sorted element of array A
Step6:- Exit
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Complexity
Complexity Best Case Average Worst Case
Case

Time Ω(n) θ(n2) o(n2)

Space o(1)

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Quick Sort
 Quick sort is the algorithm based on theory of divide
and conquer. Quick sort is also know as “partition
exchange sort” .it is very fast method of sorting .this is
developed by tony Hoare in 1959.
In this method of sorting ,the given list is divided into
sub-list and each sub-list is sorting individually .lastly
combines the results of all sub-list find the complete
sorted list
In the process of dividing the original list first we take
an element called ‘pivot’ then arrange all small element
in the left of pivot and all greater element from pivot
are arranged in the right of pivot.
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Algorithm of the quick sort
 Quick-Sort(A,F,L)
A-Represent the list of elements
F-Represent the position of the first element in the list
L-Represent the position of the last element in the list
Step 1:-[initially]
low=F
High=L
Mid=A[(F+L)/2] [middle element]
Step2:-Repeat through step 7 while(low<=high)
Step3:-Repeat through step4 while(A[low]<mid)
Step4:-low=low+1
Step5:-Repeat step6 while(A[high]>mid)
Step6:-high=high-1

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Step7:- if(low<=high)
a. Temp=A[low]
b. A[low]=A[high]
c. A[high]=temp
d. Low++
e. High- -
Step8:-if(low<L)Quick-Sort(A , low, L)
Step9:-if(F<high)Quick-Sort(A , F, high)
Step:-Exit

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Complexity
Complexity Best Case Average Case Worst Case

Time O(n) for 3 way O(n log n) O(n2)


Complexity partition or
O(n log n) simple
partition

Space O(log n)
Complexity

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Merge sort
Merge sort is the algorithm which follows divide and
conquer approach. Consider an array A of n number of
elements. The algorithm processes the elements in 3
steps.
1. If A Contains 0 or 1 elements then it is already sorted,
otherwise, Divide A into two sub-array of equal number
of elements.
2. Conquer means sort the two sub-arrays recursively using
the merge sort.
3. Combine the sub-arrays to form a single final sorted array
maintaining the ordering of the array.
The main idea behind merge sort is that, the short list
takes less time to be sorted.
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Algorithm Merge Sort
Merge-sort(A[],B[],C[],size1,size2)
Where A & B are sorted array element with size1 & size 2. And C is also a linear array
with initially no elements. and i , j, k hold the position of array A,B,C
Step1:- [initialization]
set i=1,j=1,k=1
Step2:-[compare the element]
repeat step3
while(i<=size1 and j<=size2)
Step3:- if(A[i]< B[j])
a. C[k]=A[i]
b. i++,k++
else
a. C[k]=B[j]
b. j++,k++
end if step3 end while step2
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Step4:- [copy of remaining element]
Repeat step 4
if(i<=size1)
while(i<=size1)
C[k]=A[i]
i++,k++ end while loop & end if
Step5:-[copy of remaining element]
Repeat step 5
if(j<=size2)
while(j<=size2)
C[k]=B[j]
j++,k++ end while loop & end if
Step6:- Display sorted element C
Step7:- Exit

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Complexity
Complexity Best case Average Case Worst Case

Time O(n log n) O(n log n) O(n log n)


Complexity

Space O(n)
Complexity

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Heap Sort
 Heap is a considered as a complete binary tree with the property
that the value of each node is at least as large as the value of its
children nodes .this implies that the root of the heap has the
largest key value
Heap sort processes the elements by creating the min heap
or max heap using the elements of the given array. Min heap or
max heap represents the ordering of the array in which root
element represents the minimum or maximum element of the
array. At each step, the root element of the heap gets deleted and
stored into the sorted array and the heap will again be heapified.
The heap sort basically recursively performs two main
operations.
1. Build a heap H, using the elements of ARR.
2. Repeatedly delete the root element of the heap formed in
phase 1.
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Algorithm
HEAP_SORT(ARR, N)
 Step 1: [Build Heap H]
Repeat for i=0 to N-1
CALL INSERT_HEAP(ARR, N, ARR[i])
[END OF LOOP]
 Step 2: Repeatedly Delete the root element
Repeat while N > 0
CALL Delete_Heap(ARR,N,VAL)
SET N = N+1
[END OF LOOP]
 Step 3: END

Complexity of heap sort :- nlog2n


Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Step1:-

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Step2:-

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Hashing
 Hashing is the process of mapping large amount of data
item to smaller table with the help of hashing function.
 Hashing is also known as Hashing Algorithm or Message
Digest Function.
 It is a technique to convert a range of key values into a
range of indexes of an array.
 It is used to facilitate the next level searching method when
compared with the linear or binary search. Hashing allows
to update and retrieve any data entry in a constant time
O(1).Constant time O(1) means the operation does not
depend on the size of the data.
 Hashing is used with a database to enable items to be
retrieved more quickly.
 It is used in the encryption and decryption of digital
signatures.
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Hash Function-
Hash function is a function that maps any big number or
string to a small integer value.
 Hash function takes the data item as an input and returns a small
integer value as an output.
 It transfers the digital signature and then both hash value and
signature are sent to the receiver. Receiver uses the same hash
function to generate the hash value and then compares it to that
received with the message.
 The small integer value is called as a hash value.
 Hash value of the data item is then used as an index for storing it
into the hash table.
Hash Key Value-
 Hash key value is a special value that serves as an index for a data
item.
 It indicates where the data item should be be stored in the hash
table.
 Hash key value is generated using a hash function.
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Hash table
1 Hash table or hash map is a data structure used to store key-value pairs.
2 It is a collection of items stored to make it easy to find them later.
3 It uses a hash function to compute an index into an array of buckets or
slots from which the desired value can be found.
4.It is an array of list where each list is known as bucket.
5.It contains value based on the key.
6.Hash table is used to implement the map interface and extends
Dictionary class.
7. Hash table is synchronized and contains only unique elements.

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Types of Hash Functions-

There are various types of hash functions available such as-


 Mid Square Hash Function
 Division Hash Function
 Folding Hash Function .

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
The above figure shows the hash table with the size of n
= 10. Each position of the hash table is called as Slot. In the
above hash table, there are n slots in the table, names = {0, 1, 2,
3, 4, 5, 6, 7, 8, 9}. Slot 0, slot 1, slot 2 and soon. Hash table
contains no items, so every slot is empty.
Suppose we have integer items {26, 70, 18, 31, 54, 93}. One
common method of determining a hash key is the division
method of hashing and the formula is :
Hash Key = Key Value % Number of Slots in the Table
Division method or reminder method takes an item and
divides it by the table size and returns the remainder as its
hash value.
 https://www.tutorialride.com/data-structures/graphs-in-
data-structure.htm
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Data Item Value % No. of Slots Hash Value
26 26 % 10 = 6 6
70 70 % 10 = 0 0
18 18 % 10 = 8 8
31 31 % 10 = 1 1
54 54 % 10 = 4 4
93 93 % 10 = 3 3

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Collision
 It is a situation in which the hash function returns the
same hash key for more than one record, it is called as
collision or clash. Sometimes when we are going to
resolve the collision it may lead to a overflow condition
and this overflow and collision condition makes the
poor hash function.
1) Chaining
It is a method in which additional field with data
i.e. chain is introduced. A chain is maintained at the
home bucket. In this when a collision occurs then a
linked list is maintained for colliding data.

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Example: Let us consider a hash table of size 10 and we
apply a hash function of H(key)=key % size of table. Let
us take the keys to be inserted are 31,33,77,61. In the above
diagram we can see at same bucket 1 there are two records
which are maintained by linked list or we can say by
chaining method.

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


2) Linear probing
It is very easy and simple method to
resolve or to handle the collision. In
this collision can be solved by placing
the second record linearly down,
whenever the empty place is found.
Example: Let us consider a hash table
of size 10 and hash function is defined
as H(key)=key % table size. Consider
that following keys are to be inserted
that are 56,64,36,71.
In this diagram we can see that 56 and 36 need to be placed at same
bucket but by linear probing technique the records linearly placed
downward if place is empty i.e. it can be seen 36 is placed at index 7.

Dr. Sanjeev Tayal (HOD) B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) B.Sc(C.S)

You might also like