Unit-5 Searching SortingAlgorithms Final
Unit-5 Searching SortingAlgorithms Final
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.
LinearSearch(list, key)
for each item in the list
if item == value
return its index position
return -1
Linear Search program
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
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:-
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.
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
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,
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
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
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
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)
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