0% found this document useful (0 votes)
8 views87 pages

Unit 7 - DS

The document provides an overview of sorting and searching algorithms, explaining key concepts such as in-place vs not-in-place sorting, stable vs unstable sorting, and adaptive vs non-adaptive algorithms. It details specific sorting methods like Bubble Sort and Selection Sort, including their algorithms and time complexities. Additionally, it introduces Heap data structures and the process of Heap Sort for organizing data in ascending order.

Uploaded by

vrajpatel.2060
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)
8 views87 pages

Unit 7 - DS

The document provides an overview of sorting and searching algorithms, explaining key concepts such as in-place vs not-in-place sorting, stable vs unstable sorting, and adaptive vs non-adaptive algorithms. It details specific sorting methods like Bubble Sort and Selection Sort, including their algorithms and time complexities. Additionally, it introduces Heap data structures and the process of Heap Sort for organizing data in ascending order.

Uploaded by

vrajpatel.2060
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/ 87

UNIT- 7

Sorting and
Searching

Computer Science & Engineering


Introduction
 Sorting refers to arranging data in a particular format.
Sorting algorithm specifies the way to arrange data in
a particular order. Most common orders are in
numerical or lexicographical order.
 The importance of sorting lies in the fact that data
searching can be optimized to a very high level, if data
is stored in a sorted manner. Sorting is also used to
represent data in more readable formats. Following are
some of the examples of sorting in real-life scenarios

 Telephone Directory − The telephone directory
stores the telephone numbers of people sorted by
their names, so that the names can be searched easily.
 Dictionary − The dictionary stores words in an
alphabetical order so that searching of any word
In-place Sorting and Not-in-place
Sorting
 Sorting algorithms may require some extra
space for comparison and temporary storage
of few data elements. These algorithms do not
require any extra space and sorting is said to
happen in-place, or for example, within the
array itself. This is called in-place sorting.
Bubble sort is an example of in-place sorting.
 However, in some sorting algorithms, the
program requires space which is more than or
equal to the elements being sorted. Sorting
which uses equal or more space is called not-
in-place sorting. Merge-sort is an example of
not-in-place sorting.
Stable and Not Stable Sorting
If a sorting algorithm, after
sorting the contents, does not
change the sequence of similar
content in which they appear, it
is called stable sorting.
Cont..
If a sorting algorithm, after sorting
the contents, changes the
sequence of similar content in
which they appear, it is
called unstable sorting.

Stability of an algorithm matters when we wish to maintain the


sequence of original elements, like in a tuple for example.
Adaptive and Non-Adaptive
Sorting Algorithm
A sorting algorithm is said to be adaptive, if
it takes advantage of already 'sorted'
elements in the list that is to be sorted. That
is, while sorting if the source list has some
element already sorted, adaptive algorithms
will take this into account and will try not to
re-order them.
 A non-adaptive algorithm is one which does
not take into account the elements which are
already sorted. They try to force every single
element to be re-ordered to confirm their
sortedness.
Internal and External
Sorting
 Any sort algorithm that uses main memory
exclusively during the sorting is called as
internal sort algorithms. Internal sorting is
faster than external sorting.
◦ Some example internal sorting algorithms are
Insertion Sort, Bubble Sort, Selection Sort, Heap
Sort, Shell Sort, Bucket Sort, Quick Sort, Radix
Sort.
• Any sort algorithm that uses external
memory, such as tape or disk, during the
sorting is called as external sort algorithms.
◦ Merge Sort is one of the external sort algorithms.
Bubble Sort
Sort the following array in Ascending order
45 34 56 23 12

Pass 1 :

34
45 34 34 34
swap

34
45 45 45 45
56 56 56
23 23
23 23 23
56 swap 56
12

swap
12 12 12 12
56

if(A[j] > A[j+1])


swap(A[j],A[j+1
])
Bubble Sort
Pass 2 : Pass 3 : Pass 4 :

34 34 34 23
34 23 12
23

swap
swap
45 45
23 23 23
34 34
12 12
23

swap
swap
23 23
45 45
12 12 12
34 34

swap
12 12 12
45 45 45 45
56 56 56 56 56 56

if(A[j] > A[j+1])


swap(A[j],A[j+1
])
Bubble Sort
• It is a simple sorting algorithm that works by
• Comparing each pair of adjacent items and swapping them if
they are in the wrong order.
• The pass through the list is repeated until no swaps are needed,
which indicates that the list is sorted.
• As it only uses comparisons to operate on elements, it is a
comparison sort.
• Although the algorithm is simple, it is too slow for practical
use.
Bubble Sort - Algorithm
# Input: Array A
# Output: Sorted array A

Algorithm: Bubble_Sort(A)

for i ← 1 to n do 𝜽 (𝒏)
for j ← 1 to n-i do
if A[j] > A[j+1] then
thenswap(A[j],A[j+1]
temp← ←A[j]
temp A[j] 𝜽 𝒏 )
( 𝟐

A[j]← ←A[j+1]
)A[j] A[j+1]
A[j+1]← ←temp
A[j+1] temp

The time complexity of bubble sort is 𝜽 (𝒏 )


𝟐
Selection Sort
Sort the following elements in Ascending order
5 1 12 -5 16 2 12 14

Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8

Step 2 :
Unsorted Array (elements 2 to 8) Minj = 1, Minx = 5
Find the smallest value
-5
5 1 12 -5
5 16 2 12 14 from the entire Unsorted
1 2 3 4 5 6 7 8 array

Swap
Index = 4, value = -5
Selection Sort
Step 3 : Minj = 2, Minx = 1
Unsorted Array (elements 3 to 8)
Find min value from
-5 1 12 5 16 2 12 14 Remaining unsorted array
1 2 3 4 5 6 7 8 Index = 2, value = 1

No Swapping as min value is already at right place

Step 4 :
Unsorted Array
(elements 4 to 8) Minj = 3, Minx = 12

Find min value from


-5 1 12
2 5 16 12
2 12 14 Remaining unsorted array
1 2 3 4 5 6 7 8 Index = 6, value = 2

Swap
Selection Sort
Step 5 :
Unsorted Array Minj = 4, Minx = 5
(elements 5 to 8)
Find min value from
-5 1 2 5 16 12 12 14 Remaining unsorted array
Index = 4, value = 5
1 2 3 4 5 6 7 8

No Swapping as min value is already at right place

Step 6 :
Unsorted Array
(elements 6 to 8)
Minj = 5, Minx = 16

-5 1 2 5 12
16 16
12 12 14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
Index = 6, value = 12
Swap
Selection Sort
Step 7 :
Unsorted Array Minj = 6, Minx = 16
(elements 7 to 8)
Find min value from
-5 1 2 5 12 12
16 16
12 14 Remaining unsorted array
Index = 7, value = 12
1 2 3 4 5 6 7 8

Swap

Step 8 :
Unsorted Array
(element 8)
Minj = 7, Minx = 16

-5 1 2 5 12 12 14
16 16
14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
Index = 8, value = 14
Swap
Selection Sort
• Selection sort divides the array or list into two parts,
1. The sorted part at the left end
2. and the unsorted part at the right end.
• Initially, the sorted part is empty and the unsorted part is the
entire list.
• The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element
becomes a part of the sorted array.
• Then it finds the second smallest element and exchanges it
with the element in the second leftmost position.
• This process continues until the entire array is sorted.
Selection Sort - Algorithm
# Input: Array A
# Output: Sorted array A
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i;
minx ← A[i];
for j ← i + 1 to n do
if A[j] < minx then
minj ← j;
minx ← A[j];
A[minj] ← A[i];
A[i] ← minx;
Selection Sort - Example Pass 1 :
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i; minx ← A[i];
i = 1
for j ← i + 1 to n do minj ← 21
if A[j] < minx then minx ← 34
45
minj ← j ; minx ← A[j];
A[minj] ← A[i]; j = 23
A[i] ← minx; No Change

Sort in Ascending order


45
45 34 56 23 12
1 2 3 4 5
Selection Sort Example Pass 1 :
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i ; minx ← A[i];
i = 1
for j ← i + 1 to n do minj ← 5
2
41
if A[j] < minx then minx ← 12
34
23
45
minj ← j ; minx ← A[j];
A[minj] ← A[i]; j = 23 45
A[i] ← minx;

Sort in Ascending order Unsorted Array

45
45 34 56 23 12 45
12 34 56 23 12
45
1 2 3 4 5 1 2 3 4 5
45
12 34
23 56
34 23
34
45
56 45
56
Selection Sort - Analysis
# Input: Array A
# Output: Sorted array A
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i;
minx ← A[i]; 𝜃 ( 𝑛)
for j ← i + 1 to n do
if A[j] < minx then
minj ← j;
minx ← A[j];
𝜃 (𝑛 )
2

A[minj] ← A[i];
A[i] ← minx;
The time complexity of Selection sort is 𝜽 (𝒏 )
𝟐
Introduction to Heap
• A heap data structure is a binary tree with the following two
properties.
1. It is a complete binary tree: Each level of the tree is
completely filled, except possibly the bottom level. At this level
it is filled from left to right.
a a

b c b c

d e f d e f

Binary Tree Complete Binary Tree - Heap


Introduction to Heap
2. It satisfies the heap order property: the data item stored in
each node is greater than or equal to the data item stored in
its children node.

9 9

6 7 6 7

2 4 8 2 4 1

Not a Heap Heap


Heap Sort
1. Build the complete binary tree using given elements.
2. Create Max-heap to sort in ascending order.
3. Once the heap is created, swap the last node with the root
node and delete the last node from the heap.
4. Repeat step 2 and 3 until the heap is empty.
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 10 3 5 1

Step 1 : Build Heap

10
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 10 3 5 1

Step 1 : Build Heap

10 3
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 10 3 5 1

Step 1 : Build Heap

10 3

5
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 10 3 5 1

Step 1 : Build Heap

Now, a binary 4
tree is created
and we have to
10 3
convert it into a
Heap.
5 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 10 3 5 1

Step 1 : Build Heap


Create Max Heap
In a Max Heap, 4
parent node is
always greater
10 3
than or equal
to the child
nodes. 5 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 10 3 5 1

Step 1 : Build Heap


Create Max Heap
In a Max Heap, 4
parent node is 10 is greater than 4
always greater
10 3
than or equal So, swap 10 & 4
to the child
nodes. 5 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 10 3 5 1

Step 1 : Build Heap Swap Create Max Heap


In a Max Heap, 10
parent node is 10 is greater than 4
always greater
4 3
than or equal So, swap 10 & 4
to the child
nodes. 5 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

10 4 3 5 1

Step 1 : Build Heap Swap


Create Max Heap
In a Max Heap, 10
parent node is 5 is greater than 4
always greater
4 3
than or equal So, swap 5 & 4
to the child
nodes. 5 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

10 5 3 4 1

Step 2 : Heap Sort


Max Heap is build
Swap the first 10
and the last
nodes and
5 3
delete the last
node.
4 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

10 5 3 4 1

Step 2 : Heap Sort Swap


Max Heap is build
Swap the first 10
and the last
nodes and
5 3
delete the last
node.
4 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

1 5 3 4 10

Step 2 : Heap Sort


Max Heap is build
Swap the first 1
and the last
nodes and
5 3
delete the last
node.
4 10
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

1 5 3 4 10

Step 2 : Heap Sort


Create Max Heap
In a Max Heap, 1
parent node is
always greater
5 3
than or equal
to the child
nodes. 4
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

5 1 3 4 10

Step 2 : Heap Sort


Create Max Heap
In a Max Heap, 5
parent node is
always greater
1 3
than or equal
to the child
nodes. 4
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

5 4 3 1 10

Step 2 : Heap Sort


Max Heap is build
Swap the first 5
and the last
nodes and
4 3
delete the last
node.
1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

1 4 3 5 10

Step 2 : Heap Sort


Max Heap is build
Swap the first 1
and the last
nodes and
4 3
delete the last
node.
5
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

1 4 3 5 10

Step 2 : Heap Sort


Create Max Heap
In a Max Heap, 1
parent node is
always greater
4 3
than or equal
to the child
nodes.
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

4 1 3 5 10

Step 2 : Heap Sort Max Heap is build


Swap the first 4
and the last
nodes and
1 3
delete the last
node.
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

3 1 4 5 10

Step 2 : Heap Sort


Max Heap is build
Swap the first 3
and the last
nodes and
1 4
delete the last
node.
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

3 1 4 5 10

Step 2 : Heap Sort


Max Heap is build
Swap the first 3
and the last
nodes and
1
delete the last
node.
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1

1 3 4 5 10

Step 2 : Heap Sort


Max Heap is build
Swap the first 1
and the last
nodes and
3
delete the last
node.
Remove the last element from heap and the sorting is over.
Heap Sort - Algorithm
• # Input: Array A
• # Output: Sorted array A
• Algorithm: Heap_Sort(A[1,…,n])
BUILD-MAX-HEAP(A)
for i length[A] downto 2
do exchange A[1] A[i]
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
Build-Max-Heap - Algorithm
Algorithm: BUILD-MAX-HEAP(A) 4 1 3 2 9 7
heap-size[A] ← length[A] 1
4
for i ← [length[A]/2] downto
2 3
1 1 3
do MAX-HEAPIFY(A, i) 4 5 6
heap-size[A] = 6 2 9 7

4 1 7 2 9 3 4 9 7 2 1 3 9 4 7 2 1 3
i=3 1 i=2 1 i=1 1
4 4 94
2 3 2 3 2 3
1 37 19 7 49 7
4 5 6 4 5 6 4 5 6
2 9 3
7 2 19 3 2 1 3
Heap Sort - Algorithm
• # Input: Array A 9 4 7 2 1 3
• # Output: Sorted array A 1
9
• Algorithm: Heap_Sort(A[1,…,n])
2 3
BUILD-MAX-HEAP(A) 7
4
for i length[A] downto 2 6
4 5
do exchange A[1] A[i]2 1 3
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
Heap Sort - Algorithm
• # Input: Array A 3 4 7 2 1 9
• # Output: Sorted array A 1
3
• Algorithm: Heap_Sort(A[1,…,n])
2 3
BUILD-MAX-HEAP(A) 7
4
for i length[A] downto 2 6
4 5
do exchange A[1] A[i]2 1 9
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
Max-Heapify - Algorithm
Algorithm: Max-heapify(A, i, n) 1
3
l LEFT(i) l 2 1 2 3
r RIGHT(i) r 3 4 7
if l ≤ n and A[l] > A[i] Yes 4 5
then largest l largest 2 2 1
else largest i
Yes
if r ≤ n and A[r] > A[largest]
then largest r largest 3
if largest i Yes
then exchange A[i]
A[largest]
MAX-HEAPIFY(A, largest, n)
Heap Sort - Algorithm
• # Input: Array A
1
• # Output: Sorted array A 7

• Algorithm: Heap_Sort(A[1,…,n]) 2 3
4 3
BUILD-MAX-HEAP(A)
4 5
for i length[A] downto 2 2 1
do exchange A[1] A[i]
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
O(𝑛 −1)

O ( log 𝑛 )
Heap Sort - Analysis
• # Input: Array A
• # Output: Sorted array A
• Algorithm: Heap_Sort(A[1,…,n])
BUILD-MAX-HEAP(A)
O(𝑛 log2⁡𝑛)
for i length[A] downto
do exchange A[1] 𝑛A[i]
−1
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)

O(log ⁡𝑛)
Running time of heap sort algorithm is:
Radix Sort
• Radix Sort puts the elements in order by comparing the digits
of the numbers.
• Each element in the -element array has digits, where digit is
the lowest-order digit and digit is the highest order digit.

Algorithm: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort
array A on digit i
• Sort following elements in ascending order using radix sort.
363, 729, 329, 873, 691, 521, 435, 297
Radix Sort - Example
3 6 3
7 2 9
3 2 9
8 7 3
6 9 1
5 2 1
4 3 5
2 9 7

Sort on column 1
Radix Sort - Example
6 9 1
5 2 1
3 6 3
8 7 3
4 3 5
2 9 7
7 2 9
3 2 2

Sort on column 2
Radix Sort - Example
5 2 1
7 2 9
3 2 9
4 3 5
3 6 3
8 7 3
6 9 1
2 9 7

Sort on column 3 The final array is sorted


Radix Sort - Analysis
• Radix sort time complexity
• Let there be digits in input integers. Radix Sort takes time where
is the base for representing numbers. For decimal system, is .
Merge Sort - Example
Unsorted Array
724 521 2 98 529 31 189 451

1 2 3 4 5 6 7 8

Step 1: Split the selected array


1 2 3 4 5 6 7 8
724 521 2 98 529 31 189 451

724 521 2 98 529 31 189 451


1 2 3 4 1 2 3 4
Merge Sort - Example
Select the left subarray and Split Select the right subarray and Split
1 2 3 4 1 2 3 4
724 521 2 98 529 31 189 451

1 2 1 2 Split 1 2 1 2
724 521 2 98 529 31 189 451

1 1 1 1 1 1 1 1
724 521 2 98 529 31 189 451

521 724 2 98 31 529 189 451


Merg
2 98 521 724 e 31 189 451 529

2 31 98 189 451 521 529 724


59

Merge Sort – Excercise


 Sort given numbers into descending order using merge
sort.
38, 27, 43, 3, 9, 82, 10, 67, 71, 54, 97
61

Merge Sort - Algorithm


Procedure: mergesort(T[1,…,n])
if n is sufficiently small then insert(T)
else
array U[1,…,1+n/2],V[1,…,1+n/2]
U[1,…,n/2] ← T[1,…,n/2]
V[1,…,n/2] ← T[n/2+1,…,n]
mergesort(U[1,…,n/2])
mergesort(V[1,…,n/2])
merge(U, V, T)
62

Merge Sort - Algorithm


Procedure: merge(U[1,…,m+1],V[1,…,n+1],T[1,
…,m+n])
i ← 1;
j ← 1;
U[m+1], V[n+1];
for k ← 1 to m + n do
if U[i] < V[j]
then T[k] ← U[i]; i ← i + 1;
else T[k] ← V[j]; j ← j + 1;]
A[j+1← temp
Quick Sort - Example
• Quick sort chooses the first element as a pivot element, a
lower bound is the first index and an upper bound is the
last index.
• The array is then partitioned on either side of the pivot.
• Elements are moved so that, those greater than the pivot are
shifted to its right whereas the others are shifted to its left.
• Each Partition is internally sorted recursively.
Pivot
Element

0 1 2 3 4 5 6 7 6 7
42 23 74 11 65 58 94 36 99 87

LB UB
Quick Sort - Example
Procedure pivot(T[i,…,j]; 0 1 2 3 4 5 6 7 8 9
var l)
p ← T[i] 42 23 74 11 65 58 94 36 99 87
k ← i; l ← j+1 k l
Repeat
k ← k+1 until T[k] <= p Swap
or k ≥ j
Repeat 42 23 36
74 11 65 58 94 74
36 99 87
l ← l-1 until T[l] > p k l
While k < l do
Swap T[k] and T[l]
Swap
Repeat k ← k+1 until
T[k] <= p 42
11 23 36 42
11 65 58 94 74 99 87
Repeat l ← l-1 until k l
T[l] > p
Swap T[i] and T[l]
LB = 0, UB = 9 p = 42
k = 0
l = 10
Quick Sort - Example
LB UB

Procedure pivot(T[i,…,j]; 0 1 2 3 4 5 6 7 8 9
var l)
p ← T[i] 11 23 36 42 65 58 94 74 99 87
k ← i; l ← j+1
Repeat
k ← k+1 until T[k] <= p 11 23 36
or k ≥ j
Repeat k l
l ← l-1 until T[l] > p
While k > l do LB UB
Swap T[k] and T[l]
Repeat k ← k+1 until 11 23 36 42 65 58 94 74 99 87
T[k] <= p
Repeat l ← l-1 until 23 36
T[l] > p k l
Swap T[i] and T[l]

11 23 36 42 65 58 94 74 99 87
Quick Sort - Example LB UB
4 5 6 7 8 9
Procedure pivot(T[i,…,j]; 11 23 36 42 65 58 94 72 99 87
var l)
Swap
p ← T[i]
k ← i; l ← j+1
Repeat 65 65
58 58 94 72 99 87
k ← k+1 until T[k] <= p
or k ≥ j k l
Repeat
l ← l-1 until T[l] > p 65 65 94 72 99 87
58
While k > l do
Swap T[k] and T[l]
Repeat k ← k+1 until LB UB
T[k] <= p
Repeat l ← l-1 until
11 23 36 42 65
58 65 94 72 99 87
T[l] > p
Swap T[i] and T[l]
Quick Sort - Example LB UB
Swap
Procedure pivot(T[i,…,j]; var
l) 94 72 87
99 99
87
p ← T[i]
k l
k ← i; l ← j+1 Swap
Repeat
k ← k+1 until T[k] <= p or k ≥ 94 72 94
87 87 99
j
k l
Repeat
l ← l-1 until T[l] > p LB UB
While k > l do
Swap T[k] and T[l] Swap
Repeat k ← k+1 until 72 87
87 72 94 99
T[k] <= p
Repeat l ← l-1 until k l
T[l] > p
Swap T[i] and T[l]

11 23 36 42 58 65 72 87 94 99
68

Quick Sort - Examples


• Sort the following array in ascending order using quick
sort algorithm.
1. 5, 3, 8, 9, 1, 7, 0, 2, 6, 4
2. 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9
3. 9, 7, 5, 11, 12, 2, 14, 3, 10, 6
69

Quick Sort - Algorithm


Procedure: quicksort(T[i,…,j])
{ Sorts subarray T[i,…,j] into ascending order
}
if j – i is sufficiently small
then insert (T[i,…,j])
else
pivot(T[i,…,j],l)
quicksort(T[i,…,l - 1])
quicksort(T[l+1,…,j]
70

Quick Sort - Algorithm


Procedure: pivot(T[i,…,j]; var l)
• Procedure quicksort(T[i,…,j])
p ← T[i]
• {Sorts subarray T[i,…,j] into ascending order}
• ifkj –← i is
i sufficiently small then insert (T[i,…,j])
l ← j + 1
• else
•repeat k ← k+1 until T[k] <= p or k ≥ j
pivot(T[i,…,j],l)
quicksort(T[i,…,
•repeat l ← l-1 l - 1])
until T[l] > p
quicksort(T[l+1,…,j]
•while k > l do
Swap T[k] and T[l]
Repeat k ← k+1 until T[k] <= p
Repeat l ← l-1 until T[l] > p
Swap T[i] and T[l]
Insertion Sort
In insertion sort, every iteration moves an element from unsorted portion to
sorted portion until all the elements are sorted in the list.

Steps for Insertion Sort


Assume that first element in the list is in sorted portion of
1
the list and remaining all elements are in unsorted
portion.
Select first element from the unsorted list and insert that
2
element into the sorted list in order specified.
Repeat the above process until all the elements from the
3
unsorted list are moved into the sorted list.

This algorithm is not suitable for large data sets


Insertion Sort cont.
Complexity of the Insertion Sort Algorithm
To sort a unsorted list with 'n' number of elements we need
to make (1+2+3+......+n-1) =
(n (n-1))/2 number of comparisons in the worst case.
If the list already sorted, then it requires 'n' number of
comparisons.

• Worst Case : Θ(n2)


• Best Case : Ω(n)
• Average Case : Θ(n2)
Insertion Sort Example
Sort given array using Insertion Sort

6 5 3 1 8 7 2 4
Pass - 1 : Select First Record and considered as Sorter Sub-array

6 5 3 1 8 7 2 4
Sorted Unsorted

Pass - 2 : Select Second Record and Insert at proper place in


sorted array

6 5 3 1 8 7 2 4
5 6 3 1 8 7 2 4
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 3 : Select Third record and Insert at proper place in sorted
array

5 6 3 1 8 7 2 4

3 5 6 1 8 7 2 4
Sorted Unsorted

Pass - 4 : Select Forth record and Insert at proper place in sorted


array

3 5 6 1 8 7 2 4

1 3 5 6 8 7 2 4
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 5 : Select Fifth record and Insert at proper place in sorted
array
1 3 5 6 8 7 2 4
8 is at proper position

1 3 5 6 8 7 2 4
Sorted Unsorted

Pass - 6 : Select Sixth Record and Insert at proper place in sorted


array

1 3 5 6 8 7 2 4

1 3 5 6 7 8 2 4
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 7 : Select Seventh record and Insert at proper place in
sorted array

1 3 5 6 7 8 2 4

1 2 3 5 6 7 8 4
Sorted Unsorted
Pass - 8 : Select Eighth Record and Insert at proper place in sorted
array

1 2 3 5 6 7 8 4

1 2 3 4 5 6 7 8
Sorted Unsorted
Linear/Sequential Search
• Suppose, you are given a jar containing
some business cards.
• You are asked to determine whether
the name “Mukesh Ambani" is in the
jar.
• To do this, you decide to simply go
through all the cards one by one.
• How long this takes?
• Can be determined by how many
cards are in the jar, i.e., Size of Input.
Linear Search
• Linear search is a method for finding a particular value
from the given list.
• The algorithm checks each element, one at a time and in
sequence, until the desired element is found.
• Linear search is the simplest search algorithm.
Linear Search - Example
Search for in given array

Comparing value of ith index with the given element one by one, until we get the
required element or end of the array
Step 1: i=0 Step 3: i=2

i i
2≠1 3≠1
Step 2: i=1 Step 4: i=3

𝟏
i i
9≠1 Element found at ith index, i=3
Linear Search Algorithm
# Input : Array A, element
# Output : First index of element in A or
-1 if not found

Algorithm: Linear_Search
for i = 0 to last index of A:
if A[i] equals element:
return i
return -1
Linear Search Time Complexity
• The required element in the given array can be found at,
1. E.g. 2: It is at the first position. Time complexity O(1)
Best Case: minimum comparison is required
2. E.g. 3 or 1: Anywhere after the first position. Time complexity
O((n+1)/2)
Average Case: average number of comparison is
required
3. E.g. 8 or 7: Last position or does not found at all.Time
complexity O(n)
Worst Case: maximum comparison is required Worst
2 9 3 1 8 Case

Best
Case
Average
Case
82

Binary Search
• Binary Search is an extremely well-known instance of
divide-and-conquer approach.
• Let be an array of increasing sorted order; that is
whenever .
• Let be some number. The problem consists of finding in
the array if it is there.
• If is not in the array, then we want to find the position
where it might be inserted.
Binary Search Time Complexity
• Best Case Complexity - In Binary search, best case
occurs when the element to search is found in first
comparison, i.e., when the first middle element itself is the
element to be searched. The best-case time complexity of
Binary search is O(1).
• Average Case Complexity - The average case time
complexity of Binary search is O(logn).
• Worst Case Complexity - In Binary search, the worst
case occurs, when we have to keep reducing the search
space till it has only one element. The worst-case time
complexity of Binary search is O(logn).
Binary Search - Example
Input: sorted array of integer values. .
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


84
85

Binary Search - Example


[0] [1] [2] [3] [4] [5] [6]
𝑥=7
3 6 7 11 32 33 53

IsIs7 7= <midpoint
midpointkey? NO.
value?
YES.
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.


86

Binary Search - Example


[0] [1] [2] [3] [4] [5] [6]
𝑥=7
3 6 7 11 32 33 53

Find approximate midpoint

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

<
=
> value of midpoint? NO.
YES.
87

Binary Search - Example


[0] [1] [2] [3] [4] [5] [6]
𝑥=7
3 6 7 11 32 33 53

Search for the in the area after midpoint.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is = midpoint value? YES.
88

Binary Search – Iterative Algorithm n=7


• Algorithm: Function biniter(T[1,…,n], x)
i 3
if x > T[n] then return n+1
i ← 1; 6
j ← n; 7
while i < j do
k 11
k ← (i + j ) ÷ 2
if x = T [k]
32
return T[k] 33
elseif x < T [k] j 53
then j ← k-1
else i ← k + 1 A[j] ← A[j+1]
A[j+1] ← temp
89

Binary Search – Recursive Algorithm


• Algorithm: Function binsearch(T[1,…,n], x)
if n = 0 or x > T[n] then return n + 1
else return binrec(T[1,…,n], x)
Function binrec(T[i,…,j], x)
if i = j then return i
k ← (i + j) ÷ 2
if x ≤ T[k] then
return binrec(T[i,…,k],x)
else return binrec(T[k + 1,…,j],
x)Aj+1] A[j+1] ← temp

You might also like