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

Unit-5 Searching SortingAlgorithms Final

Uploaded by

wohak23915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit-5 Searching SortingAlgorithms Final

Uploaded by

wohak23915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Unit-V

SEARCHING AND SORTING

Prepared By:
Dhaval R. Gandhi
Lecturer in I.T.
Learning Outcomes
 Searching an element into List:
 Linear Search
 Binary Search
 Sorting Methods:
 Bubble Sort
 Selection Sort
 Quick Sort
 Insertion Sort
 Merge Sort
Searching
 Searching is a technique to find the particular element
is present or not in the given list.
 There are two types of searching -
 Linear Search
 Binary Search
 Both techniques are widely used to search an element
in the given list.
LINEAR Searching
 Linear search is a method of finding elements within a list.
 It is also called a sequential search.
 It is the simplest searching algorithm because it searches
the desired element in a sequential manner.
 Linear search work on both sorted as well as unsorted list.
 This method of searching works as follows:
 It compares each and every element with the value that we
are searching for. If both are matched, the element is
found, and the algorithm returns the key's index position.
LINEAR Search example
 Let's understand the following steps to find the element
key = 7 in the given list.
 Step - 1: Start the search from the first element and Check
key = 7 with each element of list x.

 Step - 2: If element is found, return the index position of


the key.
 Step - 3: If element is not found, return element is not
present.
LINEAR Search example
Linear Search Algorithm

LinearSearch(list, key)
for each item in the list
if item == value
return its index position
return -1
Linear Search program

Element found at index: 4


LINEAR Search
 Time complexity of linear search algorithm -
 Base Case - O(1)
 Average Case - O(n)
 Worst Case -O(n)
 Linear search algorithm is suitable for smaller list (<100)
because it check every element to get the desired number.
 Suppose there are 10,000 element list and desired
element is available at the last position, this will consume
much time by comparing with each element of the list.
 To get the fast result, we can use the binary search
algorithm.
Binary Search
 A binary search is an algorithm to find a particular element
in the list. Suppose we have a list of thousand elements, and
we need to get an index position of a particular element.
 We can find the element's index position very fast using the
binary search algorithm.
 The elements in the list must be sorted to apply the binary
search algorithm. If elements are not sorted then sort them
first.
 In the binary search algorithm, we can find the element
position using the following methods.
 Recursive Method
 Iterative Method
Binary Search
 The divide and conquer approach technique is followed by
the recursive method.
 A set of statements is repeated multiple times to find an
element's index position in the iterative method.
 Binary search is more effective than the linear search
because we don't need to search each list index.
Binary Search
Steps for find X from list of N elements:-
 Determine the Lower and Upper limit of the list by
assigning Lower index of the list to LOW and Upper Index of
the list to HIGH.
 LOW = 0 and HIGH = len(list1) - 1
 Now calculate the MIDDLE position By:
MIDDLE = (LOW + HIGH)/2.
 Then we compare the value of the MIDDLE element with X.
 If list1[Middle] > X so it will exist in the lower interval of
the list, so HIGH=M-1.
 If list1[Middle] < X so it will exist in the upper interval of
the list, so Low=M+1.
Binary Search Example

 LOW = 0, HIGH = 9
 MIDDLE = (LOW + HIGH)/2 = (0+9)/2 = 4.5 = 4
 l[4]=123 < 189 so, LOW = MIDDLE + 1 = 4 +1 =5.
Binary Search Example
 LOW = 5, HIGH = 9
 Middle=(LOW+HIGH)/2 = (5+9)/2 = 14/2 = 7
 So, l[7]=235>189 so,HIGH = MIDDLE - 1 = 7 - 1 =6.

 LOW = 5, HIGH = 6
 Middle=(LOW+HIGH)/2 = (5+6)/2 = 11/2 = 5.5 = 5
 Here the value of MIDDLE element is 189 which is equal
to 189. So the element is found in the list.
Binary Search Example
 Search 45
Binary Search algorithm
binary_search(list1, n):
Step 1: low = 0 ,high = len(list1) - 1 ,mid = 0
Step-2: Repeat upto step-4 while low <= high:
# for get integer result
Step-3: mid = (high + low) // 2
# Check if n is present at mid
Step-4: if list1[mid] < n:
low = mid + 1
# If n is greater, compare to the right of mid
elif list1[mid] > n:
high = mid - 1
# If n is smaller, compared to the left of mid
else:
return mid
Step-5 : Write “Search is not successful”
return -1
Binary Search program

Element is present at index 4


Binary Search
 The complexity of the binary search algorithm is O(1) for the best
case. This happen if the element that element we are looking find in
the first comparison.
 The O(logn) is the worst and the average case complexity of the
binary search.
 A binary search algorithm is the most efficient and fast way to
search an element in the list.
 It skips the unnecessary comparison. As the name suggests, the
search is divided into two parts.
 It focuses on the side of list, which is close to the number that we are
searching.
Practice

 Consider the list l1 [ ] = {64,105,211,245,295,370,404,489,512,525}.


 Search 245 from this array with the help of binary search and state
required number of iterations.
Linear vs binary search
Basis of
comparison
Linear search Binary search
Definition The linear search starts searching It finds the position of the searched
from the first element and compares element by finding the middle
each element with a searched element element of the array.
till the element is not found.
Sorted In a linear search, the elements don't The pre-condition for the binary
data need to be arranged in sorted order. search is that the elements must be
arranged in a sorted order.
Approach It is based on the sequential approach. It is based on the divide and conquer
approach.
Size It is preferable for the small-sized data It is preferable for the large-size data
sets. sets.
Efficiency It is less efficient in the case of large- It is more efficient in the case of
size data sets. large-size data sets.
Worst-case In a linear search, the worst- case In a binary search, the worst-case
scenario scenario for finding the element is scenario for finding the element is
O(n). O(log2n).
Best-case In a linear search, the best-case In a binary search, the best-case
scenario scenario for finding the first element scenario for finding the first element
Sorting
 Sorting is a process that organizes a collection of data into either ascending or
descending order.
 Two Types of Sorting

Internal Sort
External Sort
 An internal sort requires that the collection of data fit entirely in the computer’s
main memory.
 It is useful for sorting fewer amounts of elements.
 We can use an external sort when the collection of data cannot fit in the
computer’s main memory all at once but must reside in secondary storage such as
on a disk.
 It is useful when we have to sort large amount of elements.
Sorting
Features of Sorting:-

 Any significant amount of computer output is generally arranged in some sorted


order so that it can be interpreted.

 Sorting also has indirect uses. An initial sort of the data can significantly enhance
the performance of an algorithm.

 Majority of programming projects use a sort somewhere, and in many cases, the
sorting cost determines the running time.

 A comparison-based sorting algorithm makes ordering decisions only on the basis


of comparisons.
Sorting Algorithms
Internal Sort:-
 Bubble Sort
 Selection Sort
 Insertion Sort
 Quick Sort

External Sort:-
 Merge Sort
 Radix Sort
Bubble Sort
 It is simple sorting method.
 It works fine for smaller number of elements.
 Given a list of n elements, bubble sort requires up to n-1
passes to sort the data.
 During first pass 1st and 2nd element are compared
 If 1st element >2nd element then swapped.
 now 2nd element and third element compared.
 If 2nd element> 3rd element then swapped.
 So after 1st pass largest value is placed at its proper
position.
Bubble sort
 Compare each element (except the last one) with its neighbor to
the right
 If they are out of order, swap them
 This puts the largest element at the very end
 The last element is now in the correct and final place
 Compare each element (except the last two) with its neighbor to
the right
 If they are out of order, swap them
 This puts the second largest element next to last
 The last two elements are now in their correct and final places
 Compare each element (except the last three) with its neighbor to
the right
 Continue as above until you have no unsorted elements on the
left.
Bubble sort

7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8

2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8

2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)

2 7 5 8 4 2 5 4 7 8

2 7 5 4 8

26
Bubble Sort Algorithm
Step-1 : length <- len(list1)
Step-2 : Repeat upto step 4 for I in range(length)
Step-3 : Repeat Step 4 for J in range(length-I-1)
Step-4 : if list1[J] > list1[J+1] then
temp <- list1[J]
list1[J] <- list1[J+1]
list1[J+1]<- temp
Step-5 : Exit
Bubble Sort Program

The unsorted list is: [5, 3, 8, 6, 7, 2]


The sorted list is: [2, 3, 5, 6, 7, 8]
Bubble sort
40 30 50 20 10 30 40 20 10 50 30 20 10 40 50 20 10 30 40 50

30 40 50 20 10 30 40 20 10 50 20 30 10 40 50 10 20 30 40 50

30 40 50 20 10 30 20 40 10 50 20 10 30 40 50 (done)

30 40 20 50 10 30 20 10 40 50

30 40 20 10 50

29
Bubble Sort – Analysis
 Best-case:  O(n)
 Array is already sorted in ascending order.
 The number of moves: 0  O(1)
 The number of key comparisons: (n-1)  O(n)
 Worst-case:  O(n2)
 Array is in reverse order:
 Outer loop is executed n-1 times,
 The number of moves: 3*(1+2+...+n-1) = 3 * n*(n-1)/2  O(n2)
 The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 
O(n2)
 Average-case:  O(n2)
 We have to look at all possible initial data organizations.
 So, Bubble Sort Time Complexity is O(n2)
5 2 35 9 3 -2 52 0
Selection Sort
 It is simple sorting method.
 It works fine for smaller number of elements.
 Given a list of n elements, selection sort requires up to n-1
passes to sort the data.
 We find the smallest element from the list and swap it with
the first element at the beginning of list. so element with
smallest value is placed at first position in array.
 During second pass array is searched from second record
to find second smallest element. When this element is
found it is swapped with second element in array.
 During each successive pass smallest element is placed at
its proper position.
Selection sort
 Given an array of length n,

Search elements 0 through n-1 and select the smallest


Swap it with the element in location 0
Search elements 1 through n-1 and select the smallest
Swap it with the element in location 1
Search elements 2 through n-1 and select the smallest
Swap it with the element in location 2
Search elements 3 through n-1 and select the smallest
Swap it with the element in location 3
Continue in this fashion until there’s nothing left to
search
Selection Sort
 The selection sort might swap an array
7 2 8 5 4
element with itself--this is harmless, and
not worth checking for
2 7 8 5 4
 Analysis:
 The outer loop executes n-1 times
2 4 8 5 7
 The inner loop executes about n/2
times on average (from n to 2 times)
2 4 5 8 7
 Work done in the inner loop is
constant (swap two array elements)
2 4 5 7 8  Time required is roughly (n-1)*(n/2)
 You should recognize this as O(n2)

35
selection Sort Algorithm
Step-1 : length <- len(list1)
Step-2 : Repeat upto step 6 for I in range(length)
Step-3 : min <- list1[I]
pos <-I
Step-4 : Repeat Step 4 for J in range(I+1,length)
Step-5 : if list1[J] < min then
min <- list1[J]
pos <- J
Step-6 : temp <- list1[I]
list1[I] <- list1[POS]
list1[POS]<- temp
Step-7 : Exit
selection Sort Example
selection Sort Algorithm
selection Sort Algorithm

The unsorted list is: [5, 3, 8, 6, 7, 2]


pass number 1[2, 3, 8, 6, 7, 5]
pass number 2[2, 3, 8, 6, 7, 5]
pass number 3[2, 3, 5, 6, 7, 8]
pass number 4[2, 3, 5, 6, 7, 8]
pass number 5[2, 3, 5, 6, 7, 8]
pass number 6[2, 3, 5, 6, 7, 8]
The sorted list is: [2, 3, 5, 6, 7, 8]
Sorted Unsorted

23 78 45 8 32 56 Original List

8 78 45 23 32 56 After pass 1

8 23 45 78 32 56 After pass 2

After pass 3
8 23 32 78 45 56

8 23 32 45 78 56 After pass 4

After pass 5
8 23 32 45 56 78
Selection Sort -- Analysis
 In general, we compare keys and move items (or exchange items) in
a sorting algorithm (which uses key comparisons).
  So, to analyze a sorting algorithm we should count the
number of key comparisons and the number of moves.
Ignoring other operations does not affect our final result.
 In selection Sort function, the outer for loop executes n-1 times.
 We invoke swap function once at each iteration.
  Total Swaps: n-1
  Total Moves: 3*(n-1) (Each swap has three moves)
Selection Sort – Analysis (cont.)
 The inner for loop executes the size of the unsorted part minus 1 (from 1 to n-1),
and in each iteration we make one key comparison.
  # of key comparisons = 1+2+...+n-1 = n*(n-1)/2
  So, Selection sort is O(n2)
 The best case, the worst case, and the average case of the selection sort algorithm
are same.  all of them are O(n2)
 This means that the behavior of the selection sort algorithm does not depend
on the initial organization of data.
 Since O(n2) grows so rapidly, the selection sort algorithm is appropriate only
for small n.
 Although the selection sort algorithm requires O(n2) key comparisons, it only
requires O(n) moves.
 A selection sort could be a good choice if data moves are costly but key
comparisons are not costly (short keys, long records).
5 2 35 9 3 -2 52 0
Insertion Sort
Insertion sort is a simple sorting algorithm that is
appropriate for small inputs.
Most common sorting technique used by card
players.
The list is divided into two parts: sorted and
unsorted.
In each pass, the first element of the unsorted part
is picked up, transferred to the sorted sublist, and
inserted at the appropriate place.
A list of n elements will take at most n-1 passes to
sort the data.
Insertion Sort
We require N Pass to sort N elements
During Pass-1, 1st element of the list is scanned
which is trivially sorted.
During Pass-2, 2nd element of the list is scanned
and compared with 1st element in the list. If 2nd
element is smaller than 1st element then they are
interchanged.
During pass-3 ,3rd element of the list is scanned
and it is compared with 2nd and 1st element
respectively.
Same process is repeated upto N elements.
Insertion sort Example

48
Insertion Sort Algorithm
Step-1 : length <- len(list1)
Step-2 : Repeat upto step 5 for I in range(length)
Step-3 : key <- list1[I]
Step-4 : Repeat Step 5 for J in range(I,0,-1)
Step-5 : if key < list1[J-1] then
temp <- list1[J]
list1[J] <- list1[J-1]
list1[J-1] <- temp
Step-6 : Exit
Insertion Sort Example
Insertion Sort program
Sorted Unsorted

23 78 45 8 32 56 Original List

23 78 45 8 32 56 After pass 1

23 45 78 8 32 56 After pass 2

After pass 3
8 23 45 78 32 56

8 23 32 45 78 56 After pass 4

After pass 5
8 23 32 45 56 78
Insertion Sort – Analysis
 Running time depends on not only the size of the array but also the contents
of the array.
 Best-case:  O(n)
 Array is already sorted in ascending order.
 Inner loop will not be executed.
 The number of moves: 2*(n-1)  O(n)
 The number of key comparisons: (n-1)  O(n)
 Worst-case:  O(n2)
 Array is in reverse order:
 Inner loop is executed i-1 times, for i = 2,3, …, n
 The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2 
O(n2)
 The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 
O(n2)
 Average-case:  O(n2)
 We have to look at all possible initial data organizations.
 So, Insertion Sort is O(n2)
Analysis of insertion sort
 Which running time will be used to characterize this algorithm?
 Best, worst or average?
 Worst:
 Longest running time (this is the upper limit for the algorithm)
 It is guaranteed that the algorithm will not be worse than this.
 Sometimes we are interested in average case. But there are some
problems with the average case.
 It is difficult to figure out the average case. i.e. what is average
input?
 Are we going to assume all possible inputs are equally likely?
 In fact for most algorithms average case is same as the worst
case.
5 2 35 9 3 -2 52 0
Simple Merge sort
 Merge sort is used to merge two sorted list in a single sorted
list.
 In a simple merge sort we compare the elements of both lists
and the element which has the smallest value is placed in a
new list.
 This process is repeated until all the elements from both
lists are placed in a new list.
 First we compare the elements of both the table and the
element which has the smallest value placed in new table.
Simple merge Sort Algorithm
Step-1:I <- 0,J <- 0
N1 <- len(list1)
N2 <- len(list2)
Step-2:Repeat Step 3 while I <-(N1-1) and J <-(N2-1)
Step-3:if list1[I] < list2[J] then
list3.append(list1[I])
I <- I + 1
else
list3.append(list2[I])
J <- J + 1
Step-4: Repeat Step 5 while I <-(N1-1)
Step-5: list3.append(list1[I])
I <- I + 1
Step-6: Repeat Step 7 while J <-(N2-1)
Step-7: list3.append(list2[J])
J <- J + 1
Simple merge Sort Algorithm

list1 before sorting [28, 31, 37, 39]


list2 before sorting [24, 34, 43, 53, 85, 89]
[24, 28, 31, 34, 37, 39]
[24, 28, 31, 34, 37, 39, 43, 53, 85, 89]
after sorting [24, 28, 31, 34, 37, 39, 43, 53, 85, 89]
Merge sort
 Merge sort algorithm is one of two important divide-and-
conquer sorting algorithms.
 It is a recursive algorithm.
Divides the list into halves,
Sort each halve separately, and
Then merge the sorted halves into one sorted array.
Merge Sort Example

30 24 7 12 14 4 20 21 33 38 10 55 9 23 28 16
Split the array into
two or more parts
30 24 7 12 14 4 20 21 33 38 10 55 9 23 28 16

Sort each part


individually
4 7 12 14 20 21 24 30 9 10 16 23 28 33 38 55

Merge

4 7 9 10 12 14 16 20 21 23 24 28 30 33 38 55
Mergesort - Example
Mergesort - Example
merge Sort Algorithm
Step-1: if len(nlist)>1:
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]
Step-2:mergeSort(lefthalf)
mergeSort(righthalf)
Step-3: i=0,j=0,k=0;
Step-4: while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
nlist[k]=lefthalf[i]
i=i+1
else:
nlist[k]=righthalf[j]
j=j+1
k=k+1
merge Sort Algorithm
Step-5 : while i < len(lefthalf):
nlist[k]=lefthalf[i]
i=i+1
k=k+1
Step-6: while j < len(righthalf):
nlist[k]=righthalf[j]
j=j+1
k=k+1
Step-7: Exit
merge Sort Algorithm
Mergesort – Analysis of Merge (cont.)
0 k-1 0 k-1
 Merging two sorted arrays of size k
...... ......
0 2k-1

 Best-case: ......
 All the elements in the first array are smaller (or larger) than all
the elements in the second array.
 The number of moves: 2k + 2k
 The number of key comparisons: k
 Worst-case:
 The number of moves: 2k + 2k
 The number of key comparisons: 2k-1
Mergesort - Analysis
Levels of recursive calls to mergesort, given an array of eight items
Mergesort - Analysis
2m level 0 : 1 merge (size 2m-1)
2m-1 2m-1 level 1 : 2 merges (size 2m-2)

level 2 : 4 merges (size 2m-3)


2m-2 2m-2 2m-2 2m-2
. .
. .
. .
level m-1 : 2m-1 merges (size 20)
20 20 level m
.................
Mergesort - Analysis
 Worst-case –
The number of key comparisons:
= 20*(2*2m-1-1) + 21*(2*2m-2-1) + ... + 2m-1*(2*20-1)
= (2m - 1) + (2m - 2) + ... + (2m – 2m-1) ( m terms )

m 1
= m*2m – 2
i 0
i

= m*2m – 2m – 1
Using m = log n
= n * log2n – n – 1

 O (n * log2n )
Merge sort – Analysis
 Mergesort is extremely efficient algorithm with respect to
time.
Both worst case and average cases are O (n * log2n )
 But, mergesort requires an extra array whose size equals to
the size of the original array.
 If we use a linked list, we do not need an extra array
But, we need space for the links
And, it will be difficult to divide the list into half ( O(n) )
Comparison of Sorting
Algorithms

You might also like