0% found this document useful (0 votes)
37 views255 pages

A Continuation of DSA RTU Part 3

Uploaded by

kahice9884
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)
37 views255 pages

A Continuation of DSA RTU Part 3

Uploaded by

kahice9884
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/ 255

COURSE OUTCOME

CO1: Apply the concepts of data structure, data type and array data structure
and analyze the algorithms and determine their time complexity.
CO2: Apply various data structure such as stacks, Linked List, queues, trees
and graphs to solve various computing problems using C-programming
language.
CO3: Implement standard algorithms for searching & sorting and identify
when to choose which technique.
CO4: Apply the data structure that efficiently models the information in a
problem.

1 1
Searching Techniques
(UNIT III)
Linear Search
• The linear search is a sequential search, which uses a loop to step through
an array,starting with the first element.
• It compares each element with the value being searched for, and stops
when either the value is found or the end of the array is encountered.
• If the value being searched is not in the array, the algorithm will
unsuccessfully search to the end of the array.
Linear Search

• Since the array elements are stored in linear order searching the
element in the linear order make it easy and efficient.
• The search may be successful or unsuccessfully. That is, if the required
element is found them the search is successful other wise it is
unsuccessfully.
Advantages

• The linear search is simple - It is very easy to understand and


implement
• It does not require the data in the array to be stored in any
particular order
Disadvantages

• It has very poor efficiency because it takes lots of comparisons


to find a particular record in big files
• The performance of the algorithm scales linearly with the size of the
input
• Linear search is slower then other searching algorithms
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


7
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


8
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


9
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


10
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


1
1
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86: found!


12
Analysis of Linear Search

How long will our search take?

Inthe best case, the target value is in the first element of the array.
So the search takes some tiny,and constant, amount of time.
Inthe worst case, the target value is in the last element of the array.
So the search takes an amount of time proportional to the length of the
array.
Analysis of Linear Search

Inthe average case, the target value is somewhere in the array.


Infact, since the target value can be anywhere in the array,any element of the
array is equally likely.
So on average, the target value will be in the middle of the array.
So the search takes an amount of time proportional to half the length of the
array
Binary Search

The general term for a smart search through sorted data is a binary search.
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. Ifyou’ve found your target, stop.
4. Ifyour target is less than the middle data value, the new search region is the lower half
of the data.
5. Ifyour target is greater than the middle data value, the new
search region is the higher half of the data.
6. Continue from Step 2.
Binary Search Example

-86 -37 -23 0 5 18 21 64 97

low middle high

Searching for 18.


16
Binary Search Example

-86 -37 -23 0 5 18 21 64 97

low high
middle
Searching for 18.
17
Binary Search Example

-86 -37 -23 0 5 18 21 64 97

low high
middle

Searching for 18: found!


17
Time Complexity of Binary Search

How fast is binary search?


Think about how it operates: after you examine a value, you cut the search region in half.
So,the first iteration of the loop, your search region is the whole
array.
The second iteration, it’s half the array.
The third iteration, it’s a quarter of the array.
...
The kthiteration, it’s (1/2k-1)of thearray.
1
9
Need to Sort Array

Binary search only works if the array is already sorted.


Itturns out that sorting is a huge issue in computing – and the
subject of our next lecture.

2
0
Sorting Algorithms

Bubble Sort

SelectionSort

Insertion Sort
 Sorting Algorithm isanalgorithmmadeupofaseriesofinstructionsthattakesan
array asinput,andoutputsasortedarray.

 Therearemanysortingalgorithms,suchas:
 SelectionSort, BubbleSort, InsertionSort, Merge Sort, Heap Sort,
QuickSort,RadixSort,CountingSort,BucketSort,ShellSort,CombSort,
PigeonholeSort,Cycle Sort
Analysis and Design of Algorithms
Bubble
Sort

Analysis and Design of Algorithms


Bubble Sort is thesimplest sorting algorithm
that works by repeatedly swapping the
adjacent elementsif theyare in wrong order.

Analysis and Design of Algorithms


Algorithm:

 Step1: Compare eachpair of adjacent elementsin the list

 Step2: Swaptwoelementif necessary


 Step3: Repeat this process for all the elements until
the entire array is sorted

Analysis and Design of Algorithms


 Example1 Assumethefollowing Array:

5 1 4 2

Analysis and Design of Algorithms


 First Iteration:

 Compare

5 1 4 2
 
j j+1

Analysis and Design of Algorithms


 First Iteration:

 Swap

1 5 4 2
 
j j+1

Analysis and Design of Algorithms


 First Iteration:

 Compare

1 5 4 2
 
j j+1

Analysis and Design of Algorithms


 First Iteration:

 Swap

1 4 5 2
 
j j+1

Analysis and Design of Algorithms


 First Iteration:

 Compare

1 4 5 2
 
j j+1

Analysis and Design of Algorithms


 First Iteration:

 Swap

1 4 2 5
 
j j+1

Analysis and Design of Algorithms


1 4 2 5

Analysis and Design of Algorithms


 Second Iteration:

 Compare

1 4 2 5
 
j j+1

Analysis and Design of Algorithms


 Second Iteration:

 Compare

1 4 2 5
 
j j+1

Analysis and Design of Algorithms


 Second Iteration:

 Swap

1 2 4 5
 
j j+1

Analysis and Design of Algorithms


1 2 4 5

Analysis and Design of Algorithms


 Third Iteration:

 Compare

1 2 4 5
 
j j+1

Analysis and Design of Algorithms


1 2 4 5

Analysis and Design of Algorithms


 Array is now sorted

1 2 3 4

Analysis and Design of Algorithms


 Example 2: 5 4 2 1 3 2 4 1 3 5
4 5 2 1 3 2 1 4 3 5
4 2 5 1 3 2 1 3 4 5
4 2 1 5 3 2 1 3 4 5
4 2 1 3 5 1 2 3 4 5

Analysis and Design of Algorithms


4 2 1 3 5 1 2 3 4 5
 What istheoutputof bubble sortafter the1stiteration given the
following sequenceof numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63

b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63

d) 2 4 9 13 18 45 37 63

Analysis and Design of Algorithms


 What istheoutputof bubble sortafter the1stiteration given the
following sequenceof numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63

d) 2 4 9 13 18 45 37 63

Analysis and Design of Algorithms


 Time Complexity: O(n2) asthereare twonestedloops

 Exampleof worstcase

5 4 3 2 1

Analysis and Design of Algorithms


Selection
Sort

Analysis and Design of Algorithms


The selection sort algorithm sorts an array by
repeatedly finding the minimum element
(considering ascending order) from unsorted
part and putting it atthebeginning.

Analysis and Design of Algorithms


Algorithm:

 Step1: Find theminimumvalue in thelist

 Step2: Swapit withthevalue in thecurrent position


 Step3: Repeat this process for all the elements until
the entire array is sorted

Analysis and Design of Algorithms


 Example1 Assumethefollowing Array:

8 12 5 9 2

Analysis and Design of Algorithms


 Compare

8 12 5 9 2
 
i j


min

Analysis and Design of Algorithms


 Compare

8 12 5 9 2
 
i j


min

Analysis and Design of Algorithms


 Move

8 12 5 9 2
 
i j

min

Analysis and Design of Algorithms


 Compare

8 12 5 9 2
  
i min j

Analysis and Design of Algorithms


 Compare

8 12 5 9 2
  
i min j

Analysis and Design of Algorithms


 Move

8 12 5 9 2
 
i j

min

Analysis and Design of Algorithms


 Smallest

8 12 5 9 2
 
i min

Analysis and Design of Algorithms


 Swap

8 12 5 9 2
 
i min

Analysis and Design of Algorithms


 Sorted

 Un Sorted

2 12 5 9 8
 
Sorted Un Sorted

Analysis and Design of Algorithms


 Compare

2 12 5 9 8
  
Sorted i j


min

Analysis and Design of Algorithms


 Move

2 12 5 9 8
  
Sorted i j

min

Analysis and Design of Algorithms


 Compare

2 12 5 9 8
   
Sorted i min j

Analysis and Design of Algorithms


 Compare

2 12 5 9 8
   
Sorted i min j

Analysis and Design of Algorithms


 Smallest

2 12 5 9 8
  
Sorted i min

Analysis and Design of Algorithms


 Swap

2 12 5 9 8
  
Sorted i min

Analysis and Design of Algorithms


 Sorted

 Un Sorted

2 5 12 9 8
 
Sorted Un Sorted

Analysis and Design of Algorithms


 Compare

2 5 12 9 8
  
Sorted i j

min

Analysis and Design of Algorithms


 Move

2 5 12 9 8
  
Sorted i j

min

Analysis and Design of Algorithms


 Compare

2 5 12 9 8
   
Sorted i min j

Analysis and Design of Algorithms


 Move

2 5 12 9 8
  
Sorted i j

min

Analysis and Design of Algorithms


 Smallest

2 5 12 9 8
  
Sorted i min

Analysis and Design of Algorithms


 Swap

2 5 12 9 8
  
Sorted i min

Analysis and Design of Algorithms


 Sorted

 Un Sorted

2 5 8 9 12
 
Sorted Un Sorted

Analysis and Design of Algorithms


 Compare

2 5 8 9 12
  
Sorted i j

min

Analysis and Design of Algorithms


 Sorted

 Un Sorted

2 5 8 9 12
 
Sorted Un Sorted

Analysis and Design of Algorithms


 Sorted

 Un Sorted

2 5 8 9 12
 
Sorted i

min

Analysis and Design of Algorithms


 Array is now sorted

2 5 8 9 12

Sorted

Analysis and Design of Algorithms


 Example 2: 12 10 16 11 9 7
7 10 16 11 9 12
7 9 16 11 10 12
12 10 16 11 9 7
7 9 10 11 16 12
7 9 10 11 16 12
7 9 10 11 12 16
Analysis and Design of Algorithms
 What istheoutputof selectionsortafter the2nd iteration given the
following sequenceof numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63

b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63

d) 2 4 9 13 18 45 37 63

Analysis and Design of Algorithms


 What istheoutputof selectionsortafter the2nd iteration given the
following sequenceof numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63

b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63

Analysis and Design of Algorithms


 Time Complexity: O(n2) asthereare twonestedloops

 Exampleof worstcase

2 3 4 5 1

Analysis and Design of Algorithms


Insertion
Sort

Analysis and Design of Algorithms


Insertion sort is a simple sorting
algorithm that works the way wesort
playing cardsin our hands.

Analysis and Design of Algorithms


 Algorithm:
 Step1: Compare eachpair of adjacent elementsin thelist
 Step2: Insertelementinto thesortedlist, until it occupiescorrect
position.
 Step3: Swaptwoelementif necessary
 Step4: Repeat thisprocessfor all theelementsuntil theentire
array is sorted
Analysis and Design of Algorithms
 Assumethefollowing Array:

5 1 4 2

Analysis and Design of Algorithms


 Compare

 Store= 1

5 1 4 2
 
j i


j+1

Analysis and Design of Algorithms


 Move

 Store= 1

5 4 2
 
j i


j+1

Analysis and Design of Algorithms


 Move

 Store=

1 5 4 2
 
j+1 i

Analysis and Design of Algorithms


 Compare

 Store= 4

1 5 4 2
 
j i

j+1

Analysis and Design of Algorithms


 Move

 Store= 4

1 5 2
 
j i

j+1

Analysis and Design of Algorithms


 Compare

 Store= 4

1 5 2
  
j j+1 i

Analysis and Design of Algorithms


 Move

 Store=

1 4 5 2
  
j j+1 i

Analysis and Design of Algorithms


 Compare

 Store= 2

1 4 5 2
 
j i

j+1

Analysis and Design of Algorithms


 Move

 Store= 2

1 4 5
 
j i

j+1

Analysis and Design of Algorithms


 Compare

 Store= 2

1 4 5
  
j j+1 i

Analysis and Design of Algorithms


 Move

 Store= 2

1 4 5
  
j j+1 i

Analysis and Design of Algorithms


 Compare

 Store= 2

1 4 5
  
j j+1 i

Analysis and Design of Algorithms


 Compare

 Store=

1 2 4 5
  
j j+1 i

Analysis and Design of Algorithms


 Array is now sorted

1 2 4 5

Analysis and Design of Algorithms


 Example 2:
1 5 8 3 9 2
5 1 8 3 9 2
1 3 5 8 9 2
5 1 8 3 9 2
1 3 5 8 9 2
1 5 8 3 9 2
1 2 3 5 8 9
Analysis and Design of Algorithms
 What istheoutputof insertion sortafter the1stiteration given the
following sequenceof numbers: 7 3 5 1 9 8 4 6

a) 3 7 5 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Analysis and Design of Algorithms


 What istheoutputof insertion sortafter the1stiteration given the
following sequenceof numbers: 7 3 5 1 9 8 4 6

a) 3 7 5 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Analysis and Design of Algorithms


 What is the output of insertion sort after the 2nd iteration given the
following sequenceof numbers: 7 3 5 1 9 8 4 6

a) 3 5 7 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Analysis and Design of Algorithms


 What is the output of insertion sort after the 2nd iteration given the
following sequenceof numbers: 7 3 5 1 9 8 4 6

a) 3 5 7 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Analysis and Design of Algorithms


 Time Complexity: O(n2)

 Exampleof worstcase

5 4 3 2 1

Analysis and Design of Algorithms


Counting
Sort

Analysis and Design of Algorithms


Counting sort is a sorting technique based on keys
between a specific range. It works by counting the
number of objects having distinct key values (kind of
hashing). Thendoing somearithmetic to calculate the
position of eachobjectin theoutputsequence.

Analysis and Design of Algorithms


 Algorithm:
 Step1: Create a countarray tostorethecountof eachunique
object

 Step2 : Modify countarray by adding the previous number.

 Step3 : Create outputarray by decreasecountarray

Analysis and Design of Algorithms


 Example1 Assumethefollowing Array in range of 0 to5:

1 4 3 2 3 5 2

Analysis and Design of Algorithms


 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

Analysis and Design of Algorithms


 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 0 0 0 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 0 0 0 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 0 0 0 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 1 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 1 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 1 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 1 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 1 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 0
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 1
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 1
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 1
Analysis and Design of Algorithms
 Create a countarray tostorethecountof eachunique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 2 2 1 1
Analysis and Design of Algorithms
 Modify countarray by adding theprevious number:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 2 2 1 1
Analysis and Design of Algorithms
 Modify countarray by adding theprevious number:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 2 2 1 1
Analysis and Design of Algorithms
 Modify countarray by adding theprevious number:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 2 1 1
Analysis and Design of Algorithms
 Modify countarray by adding theprevious number:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 1 1
Analysis and Design of Algorithms
 Modify countarray by adding theprevious number:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 1
Analysis and Design of Algorithms
 Modify countarray by adding theprevious number:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7

1 2 3 4 5 6 7

Analysis and Design of Algorithms


 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7

1 2 3 4 5 6 7

Analysis and Design of Algorithms


 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7

1 2 3 4 5 6 7

Analysis and Design of Algorithms


 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7

1 2 3 4 5 6 7
1
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 6 7

1 2 3 4 5 6 7
1
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 6 7

1 2 3 4 5 6 7
1
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7

1 2 3 4 5 6 7
1 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7

1 2 3 4 5 6 7
1 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7

1 2 3 4 5 6 7
1 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7

1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7

1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7

1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7

1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7

1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7

1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7

1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7

1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7

1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7

1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7

1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7

1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7

1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6

1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6

1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6

1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6

1 2 3 4 5 6 7
1 2 2 3 3 4 5
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 1 3 5 6

1 2 3 4 5 6 7
1 2 2 3 3 4 5
Analysis and Design of Algorithms
 Output each object from the input sequence followed
by decreasing itscountby 1:

1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 1 3 5 6

1 2 3 4 5 6 7
1 2 2 3 3 4 5
Analysis and Design of Algorithms
 Array is now sorted

1 2 2 3 3 4 5

Analysis and Design of Algorithms


0 1 2 3 4

 Example 2: Reduce 0 2 2 4 4
2 4 1 1 3 1 2 3 4 5
Range=[0-4] Output 1 2 4
0 1 2 3 4
0 1 2 3 4
Count 0 2 1 1 1 Reduce 0 1 2 4 4
0 1 2 3 4 1 2 3 4 5
Add 0 2 3 4 5 Output 1 1 2 4
1 2 3 4 5 0 1 2 3 4
Output 2 Reduce 0 0 2 4 4
0 1 2 3 4 1 2 3 4 5

Reduce 0 2 2 4 5 Output 1 1 2 3 4
1 2 3 4 5 1 2 3 4 5

Output
Analysis and Design of Algorithms
2 4 Sorted 1 1 2 3 4
 Time Complexity: O(n+k) wheren isthenumberof elementsin input
array, and kistherange of input.

 Exampleof worstcase

 Rangebetween1 to 10K

10 5 10k 5k 200
Analysis and Design of Algorithms
Radix
Sort

Analysis and Design of Algorithms


 Radix sortis an algorithm thatsortsnumbersby processing digits of
eachnumber either starting from theleastsignificant digit (LSD) or
starting from themostsignificant digit (MSD).

 Theidea of Radix Sort is todo digit by digit sort starting from least
significant digit to mostsignificant digit. Radix sort uses counting
sortasa subroutine to sort.

Analysis and Design of Algorithms


Algorithm:

 Step1: Taketheleastsignificant digit of each element

 Step2 : Sortthelistof elementsbased on that digit

 Step3 : Repeatthesortwitheachmoresignificantdigit

Analysis and Design of Algorithms


 Assumethefollowing Array:

170 45 75 90 802 24 2 66

Analysis and Design of Algorithms


 TheSorted listwill appear after three steps

170 45 75 90 802 24 2 66

170 90 802 2 24 45 75 66

802 2 24 45 66 170 75 90

2 24 45 66 75 90 170 802
Analysis and Design of Algorithms
 Step1: Sorting by leastsignificant digit (1splace)

170 45 75 90 802 24 2 66

170 90 802 2 24 45 75 66

Analysis and Design of Algorithms


 Step2: Sorting by nextdigit (10s place)

170 90 802 2 24 45 75 66

802 2 24 45 66 170 75 90

Analysis and Design of Algorithms


 Step3: Sorting by mostsignificant digit (100s place)

802 2 24 45 66 170 75 90

2 24 45 66 75 90 170 802

Analysis and Design of Algorithms


 Array is now sorted

2 24 45 66 75 90 170 802

Analysis and Design of Algorithms


 Example 2

1 2 3 1 2 3 1 2 3 1 2 3
5 8 3 5 8 3 6 2 5 1 5 4
1 5 4 1 5 4 1 5 4 4 5 6
5 6 7 1s 6 2 5 10s 4 5 6 100s 5 6 7
6 8 9 4 5 6 5 6 7 5 8 3
6 2 5 5 6 7 5 8 3 6 2 5
4 5 6 6 8 9 6 8 9 6 8 9
Analysis and Design of Algorithms
Time Complexity: O(n+k/d) wheren is thenumber of
elementsin input array, kis therange of input, and d
isnumberof digits.

Analysis and Design of Algorithms


Merge
Sort

Analysis and Design of Algorithms


Merge Sort is a Divide and Conquer algorithm. It
divides input array in twohalves, callsitself for the two
halvesand thenmergesthetwosortedhalves.

Analysis and Design of Algorithms


Algorithm:
 Step1: Divide thelistrecursively into twohalvesuntil it can
no morebedivided

 Step2 : Merge (Conquer) thesmallerlistsinto newlistin


sorted order

Analysis and Design of Algorithms


 Assumethefollowing Array:

85 24 63 45 17 31 96 50

Analysis and Design of Algorithms


 Divide
85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

Analysis and Design of Algorithms


 Divide
85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

Analysis and Design of Algorithms


 Divide
85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85 45 63

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85 45 63

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85 45 63 17 31

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85 45 63 17 31

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 45

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 45 63

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 45 63 85

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge

24 45 63 85 17 31 50 96

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Sort&Merge
17 24 31 45 50 63 85 96

24 45 63 85 17 31 50 96

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
 Array is now sorted

17 24 31 45 50 63 85 96

Analysis and Design of Algorithms


 Example 2

Analysis and Design of Algorithms


Time Complexity: O(n * log(n) )

Analysis and Design of Algorithms


Definitions of heap:

A heap is a data structure that stores a collection of objects (with


keys), and has the following properties:

 Complete Binary tree


 Heap Order
The heap sort algorithm has two major
steps :

i. The first major step involves transforming the complete tree into a heap.

ii. The second major step is to perform the actual sort by extracting the
largest or lowerst element from the root and transforming the remaining
tree into a heap.
Types of heap

 Max Heap
 Min Heap
Max Heap Example
19

12 16

1 4 7

19 12 16 1 4 7

Array A
Min heap example
1

4 16

7 12 19

1 4 16 7 12 19

Array A
1-Max heap :
max-heap Definition:
is a complete binary tree in which the value in each internal
node is greater than or equal to the values in the children of
that node.

Max-heap property:
 The key of a node is ≥ than the keys of its children.
Max heap Operation
 A heap can be stored as an array A.
 Root of tree is A[1]
 Left child of A[i] = A[2i]
 Right child of A[i] = A[2i + 1]
 Parent of A[i] = A[ i/2 ]

21
2
4 1 3 2 16 9 10 14 8 7
Example Explaining : A
i=5 i=4 i=3
1 1 1

4 4 4
2 3 2 3 2 3

1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
2 9 10
16 9 10 8
2 9 10
16 9 10 8 14 16 9 10
8 9 10

14 8 7 14 8 7 2 8 7

i=2 i=1
1 1 1

4 4 16
2 3 2 3 2 3

1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
14 9 10
16 9 3 8
14 9 10
7 9 3 8 8 7 9 3
8 9 10

2 8 7 2 8 1 2 4 1
21
3
Build Max-heap
Heapify() Example

16

4 10

14 7 9 3

2 8 1

A= 16 4 10 14 7 9 3 2 8 1

David Luebke
214
11/20/2017
Heapify() Example

16

4 10

14 7 9 3

2 8 1

A= 16 4 10 14 7 9 3 2 8 1

David Luebke
215
11/20/2017
Heapify() Example

16

4 10

14 7 9 3

2 8 1

A= 16 4 10 14 7 9 3 2 8 1

David Luebke
216
11/20/2017
Heapify() Example

16

14 10

4 7 9 3

2 8 1

A= 16 14 10 4 7 9 3 2 8 1

David Luebke
217
11/20/2017
Heapify() Example

16

14 10

4 7 9 3

2 8 1

A= 16 14 10 4 7 9 3 2 8 1

David Luebke
218
11/20/2017
Heapify() Example

16

14 10

4 7 9 3

2 8 1

A= 16 14 10 4 7 9 3 2 8 1

David Luebke
219
11/20/2017
Heapify() Example

16

14 10

8 7 9 3

2 4 1

A= 16 14 10 8 7 9 3 2 4 1

David Luebke
220
11/20/2017
Heapify() Example

16

14 10

8 7 9 3

2 4 1

A= 16 14 10 8 7 9 3 2 4 1

David Luebke
221
11/20/2017
Heapify() Example

16

14 10

8 7 9 3

2 4 1

A= 16 14 10 8 7 9 3 2 4 1

David Luebke
222
11/20/2017
Heap-Sort
sorting strategy:
1. Build Max Heap from unordered array;
2. Find maximum element A[1];
3. Swap elements A[n] and A[1] :
now max element is at the end of the array! .
4. Discard node n from heap
(by decrementing heap-size variable).
5. New root may violate max heap property, but its children are max heaps.
Run max_heapify to fix this.
6. Go to Step 2 unless heap is empty.
HeapSort() Example
 A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
1
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10
2 4 1

20
HeapSort() Example
 A = {14, 8, 10, 4, 7, 9, 3, 2, 1, 16}
1
14
2 3
8 10
4 5 6 7
4 7 9 3
8 9
2 1 16
i = 10

21
HeapSort() Example
 A = {10, 8, 9, 4, 7, 1, 3, 2, 14, 16}
1
10
2 3
8 9
4 5 6 7
4 7 1 3
8
2 14 16
i=9 10

22
HeapSort() Example
 A = {9, 8, 3, 4, 7, 1, 2, 10, 14, 16}
1
9
2 3
8 3
4 5 6 7
4 7 1 2

10 14 16
i=8 9 10

23
HeapSort() Example
 A = {8, 7, 3, 4, 2, 1, 9, 10, 14, 16}
1
8
2 3
7 3
4 5 6
4 2 1 9
i=7
10 14 16
8 9 10

24
HeapSort() Example
 A = {7, 4, 3, 1, 2, 8, 9, 10, 14, 16}
1
7
2 3
4 3
4 5
1 2 8 9
i=6 7
10 14 16
8 9 10

25
HeapSort() Example
 A = {4, 2, 3, 1, 7, 8, 9, 10, 14, 16}
1
4
2 3
2 3
4 i=5

1 7 8 9
6 7
10 14 16
8 9 10

26
HeapSort() Example
 A = {3, 2, 1, 4, 7, 8, 9, 10, 14, 16}
1
3
2 3
2 1

i=4 4 7 8 9
5 6 7
10 14 16
8 9 10

27
HeapSort() Example
 A = {2, 1, 3, 4, 7, 8, 9, 10, 14, 16}
1
2
2 i=3

1 3
4
4 7 8 9
5 6 7
10 14 16
8 9 10

28
HeapSort() Example
 A = {1, 2, 3, 4, 7, 8, 9, 10, 14, 16} >>orederd
1
1
i =2 3

2 3
4
4 7 8 9
5 6 7
10 14 16
8 9 10

29
Heap Sort pseducode

Heapsort(A as array)
BuildHeap(A)
for i = n to 1 swap(A[1], A[i]) n
= n - 1 Heapify(A, 1)

B u i l d H e a p ( A a s a r r a y ) n =
elements_in(A) for i = f loor(n/2)
to 1
Heapify(A,i)
Heapify(A as array, i as int) left = 2i
right = 2i+1

if (left <= n) and (A[left] > A[i]) max = left


else
max = i

if (right<=n) and (A[right] > A[max]) max = right

if (max != i) swap(A[i], A[max])


Heapify(A, max)
2-Min heap :
min-heap Definition:
is a complete binary tree in which the value in each internal
node is lower than or equal to the values in the children of that
node.

Min-heap property:
 The key of a node is <= than the keys of its children.
Min heap Operation
 A heap can be stored as an array A.
 Root of tree is A[0]
 Left child of A[i] = A[2i+1]
 Right child of A[i] = A[2i + 2]
 Parent of A[i] = A[( i/2)-1]

33
Min Heap

19

12 16

1 4 7

19 12 16 1 4 7

Array A
Min Heap phase 1

19

1 16

12 4 7
Min Heap phase 1

19 7

12 4 16
Min Heap phase 1

4 7

12 19 16

1,
Min Heap phase 2

16

4 7

12 19
Min Heap phase 2

16 7

12 19
Min Heap phase 2

12 7

16 19

1,4
Min Heap phase 3

19

12 7

16
Min Heap phase 3

12 19

16

1,4,7
Min Heap phase 4

16

12 19
Min Heap phase 4

12

16 19

1,4,7,12
Min Heap phase 5

19

16
Min Heap phase 5

16

19

1,4,7,12,16
Min Heap phase 7

19

1,4,7,12,16,19
Min heap final tree
1

4 7

12 16 19

1 4 7 12 16 19

Array A
Insertion
 Algorithm
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
 General strategy is percolate up (or bubble up): if the parent of the
element is smaller than the element, then interchange the parent and
child.

OR

Restore the min-heap property if violated


 General strategy is percolate up (or bubble up): if the parent of the
element is larger than the element, then interchange the parent and child.
19 19

12 16 12 16

4 7 1 4 7 17
1
Insert 17
19

12 17
swap

1 4 7 16

Percolate up to maintain the heap property


Conclusion
 The primary advantage of the heap sort is its efficiency. The execution time
efficiency of the heap sort is O(n log n).

 The memory efficiency of the heap sort, unlike the other n log n sorts, is constant,
O(1), because the heap sort algorithm is not recursive.

You might also like