0% found this document useful (0 votes)
31 views11 pages

1.4 Analysis of Sorting - Searching

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

1.4 Analysis of Sorting - Searching

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

ANALYSIS OF SORTING AND

SEARCHING

AAD CSE SRM-AP 1


Sorting
A sorting algorithm is an algorithm that
puts elements of a list in a certain order.
Insertion Sort.
Bubble Sort.
Selection Sort.
Quick Sort.
Merge Sort.
Heap Sort.
AAD CSE SRM-AP 2
Insertion Sort : Demo
Consider the array A=[5,2,4,6,1,3] for
sorting. The following diagram shows
the steps in sorting.

AAD CSE SRM-AP 3


Insertion Sort : Algorithm
INSERTION-SORT(A)
for j ← 2 to A.length
key ← A[j]
// Insert A[j] into the sorted sequence A[1 .. j -1].
i← j –1
while i> 0 and A[i] > key
A[i+ 1] ← A[i]
i← i–1
A[i+ 1] ← key

AAD CSE SRM-AP 4


Primitive Operations of Insertion Sort

AAD CSE SRM-AP 5


Cases of Insertion sort
Best case Analysis: When elements are in sorted
order tj=1

Worst case Analysis: When elements are in


reversely sorted tj=n

AAD CSE SRM-AP 6


AAD CSE SRM-AP 7
Searching
Searching is a process of finding a
particular element among several given
elements.
The search is successful if the required
element is found.
Otherwise, the search is unsuccessful.

AAD CSE SRM-AP 8


Linear Search
Linear Search is the simplest searching
algorithm.
It traverses the array sequentially to locate
the required element.
It searches for an element by comparing it
with each element of the array one by one.
So, it is also called as Sequential Search.

AAD CSE SRM-AP 9


Linear Search Algorithm

Best Case:
 T(n)=c1+c2+c4+c6=O(1)

Worst Case:
 T(n)=c1++c4+c5+c6=O(n)

Average Case:
 T(n)=c1++c4+c5+c6=O(n)

AAD CSE SRM-AP 10


AAD CSE SRM-AP 11

You might also like