SlideShare a Scribd company logo
1 of 104

Design and Analysis of Algorithm
VADODARA INSTITUTE OF ENGINEERING
ACTIVE LEARNING ASSIGNEMENT
TOPIC: QUICK AND MERGE SORT
COMPUTER ENGINEERING(CE-1)
Presented By:
Heta Patel 150800107041
Maitree Patel 150800107048
Riddhi Patel 150800107053

Contents
 Quick Sort
 Time Complexity of Quick Sort
 Merge Sort
 Time Complexity of Merge Sort

Quicksort
Given an array of n elements (e.g., integers):
 If array only contains one element, return
 Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
Quicksort two sub-arrays
Return results
 Partitioning Array
Given a pivot, partition the elements of the array such
that the resulting array consists of:
One sub-array that contains elements >= pivot
Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.
Partitioning loops through, swapping elements
below/above pivot.
 Quicksort Algorithm
 Quicksort(a,low,high)
(1)If low<high then
 Set mid=partition(A,low,high)
 Call Quicksort(A,low,mid-1)
 Call Quicksort(A,mid+1,high)
(2)End
40 20 10 80 60 50 7 30 100

 partition(A,low,high)
(1) [initialize]
 Set pivot =A[low]
 Set i=low+1
 Set j=high
(2) Repeat step 3,4,5
 While i<=j
(3)While A[i]<pivot
 Then i++
 [end of while]
(4)While A[j]>pivot then
 j- -
 [end while]
40 20 10 80 60 50 7 30 100

(5)If i<j then
 Swap A[i] and A[j]
 [end if]
 [end of main while]
(6)Swap A[j] and pivot
(7)Return j
(8)exit
40 20 10 80 60 50 7 30 100
 Pick Pivot Element
There are a number of ways to pick the pivot element.
In this example, we will use the first element in the
array:
40 20 10 80 60 50 7 30 100
 Example
We are given array of n integers to sort:
40 20 10 80 60 50 7 30 100

40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i

40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i

40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i

40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j

40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j

40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]

40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]

40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
5. Swap data[j] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j

1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
5. Swap data[j] and data[pivot_index]
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 4

Partition Result
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] >data[pivot]

Time Complexity of quick
sort
 T(N) = T(i) + T(N - i -1) + cN
 The time to sort the file is equal to
 the time to sort the left partition with i elements, plus
 the time to sort the right partition with N-i-
1 elements, plus
 the time to build the partitions
 Worst case analysis
The pivot is the smallest element
T(N) = T(N-1) + cN, N > 1
Telescoping:
T(N-1) = T(N-2) + c(N-1)
T(N-2) = T(N-3) + c(N-2)
T(N-3) = T(N-4) + c(N-3)
T(2) = T(1) + c.2
Add all equations:
T(N) + T(N-1) + T(N-2) + … + T(2) =
= T(N-1) + T(N-2) + … + T(2) + T(1) +
c(N) + c(N-1) + c(N-2) + … + c.2
T(N) = T(1) + c(2 + 3 + … + N)
T(N) = 1 + c(N(N+1)/2 -1)
Therefore T(N) = O(N2
)

 t(n) = 2t(n/2) + cn
for best case time complexity is: Θ(nlogn)
For worst case time complexity is: Θ(n²)
For average case time complexity is:
Θ(nlogn)

 Advantages:
 One of the fastest algorithms on average.
 Does not need additional memory (the sorting takes
place in the array - this is called in-place processing).
Compare with mergesort: mergesort needs additional
memory for merging.
 Merge sort
 ALGORITHM
 MERGE-SORT (A, L, H)
 1. IF L< H
2. THEN m= [(L+ H)/2]
3. MERGE (A, L, M)
4. MERGE (A, M + 1, H)
5. MERGE (A, L, H, M)


ALGORITHM
 COMBINE(A,L,H,M)
 I=L, K=L, J=M+1
 WHILE(I<=M && J<=H)
 IF A[I]<A[J]
 TEMP[K]=A[I]
 I++,K++
 ELSE
 TEMP[K]=A[J]
 I++,K++

ALGORITHM
 Whille(I<=M)
 TEMP[K]=A[I]
 I++,K++
 WHILE(J<=H)
 TEMP[K]=A[J]
 J++,K++
 FOR K L TO H
 A[K]=TEMP[K]

40 20 10 80 60 50 7 30 100
EXAMPLE MERGE SORT
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30
40 20
100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30
40 20 10 80
100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30
40 20 10 80 60 50
100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30
40 20 10 80 60 50 7 30
100
100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30
40 20 10 80 60 50 7 30
100
100
30 100
40 20 10 80 60 50 7
30 100
i j
20
20 10 80 60 50 7
30 100
i j
40
20 40
10 80 60 50 7
30 100
20 40
10 80 60 50 7
30 100
i j
20 40 10
10 80 60 50 7
30 100
i j
20 40 10 80
80 60 50 7
30 100
j
20 40 10 80
60 50 7
30 100
i j
10
20 40 10 80
60 50 7
30 100
i j
10 20
20 40 10 80
60 50 7
30 100
i j
10 20 40
20 40 10 80
60 50 7
30 100
j
10 20 40 80
20 40 10 80
60 50 7
30 100
j
10 20 40 80
60 50 7
30 100
ji
10 20 40 80
50
60 50 7
30 100
ji
10 20 40 80
50 60
60 50 7
30 100
ji
10 20 40 80
50 60
7
30 100
10 20 40 80
50 60
7
30 100
ji
10 20 40 80
50 60
7 30
30 100
ji
10 20 40 80
50 60
7 30 100
30 100
ji
10 20 40 80
50 60
7 30 100
10 20 40 80
50 60
7 30 100
ji
10 20 40 80
50 60 7
7 30 100
ji
10 20 40 80
50 60 7 30
7 30 100
ji
10 20 40 80
50 60 7 30
7 30
100
100
ji
10 20 40 80
50 60 7 30 100
10 20 40 80
50 60 7 30 100
ji
10 20 40 80 7
50 60 7 30 100
ji
10 20 40 80 7
50 60 7 30 100
ji
10 20 40 80 7 30
50 60 7 30 100
ji
10 20 40 80 7 30 50
50 60 7 30 100
ji
10 20 40 80 7 30 50 60
50 60 7 30 100
ji
10 20 40 80 7 30 50 60 100
50 60 7 30 100
ji
10 20 40 80 7 30 50 60 100
10 20 40 80 7 30 50 60 100
i j
7
10 20 40 80 7 30 50 60 100
i j
7 10
10 20 40 80 7 30 50 60 100
i j
7 10 20
10 20 40 80 7 30 50 60 100
i j
7 10 20 30
10 20 40 80 7 30 50 60 100
i j
7 10 20 30 40
10 20 40 80 7 30 50 60 100
i j
7 10 20 30 40 50
10 20 40 80 7 30 50 60 100
i j
7 10 20 30 40 50 60
10 20 40 80 7 30 50 60 100
i j
7 10 20 30 40 50 60 80
10 20 40 80 7 30 50 60 100
i j
7 10 20 30 40 50 60 80 100
10 20 40 80 7 30 50 60 100
i j

40 50 60 80 1007 10 20 30
Hence we got he sorted array:

TIME COMPLEXITY OF MERGE
SORT

 How many times are these steps
executed?
 For this, look at the tree below - for each level
from top to bottom Level 2 calls merge method
on 2 sub-arrays of length n/2 each.
 The complexity here is 2 * (cn/2) = cn Level 3
calls merge method on 4 sub-arrays of length
n/4 each. The complexity here is 4 * (cn/4) = cn
and so on ...
 Now, the height of this tree is (logn + 1) for a
given n. Thus the overall complexity is (logn +
1)*(cn). That is O(nlogn) for the merge sort
algorithm.

 t(n) = 2t(n/2) + Cn
for best case time complexity is: Θ(n log(n))
For worst case time complexity is: Θ(n log(n))
For average case time complexity is: Θ(n
log(n))

 The advantages to merge sort is it is always
fast. Even in its worst case its runtime is
O(nlogn).
 Disadvantages of Merge sort are that it is not
in place so merge sort uses a lot of memory.
It uses extra space
 References
 https://www.khanacademy.org/computing/computer-science/algorithms/quick
 https://softwareengineering.stackexchange.com/questions/297160/why-is-m
 http://cs.fit.edu/~pkc/classes/writing/hw13/luis.pdf

Thank you

More Related Content

Similar to Merge sort and Quick sort

Similar to Merge sort and Quick sort (20)

Quicksort
QuicksortQuicksort
Quicksort
 
Unit 5 internal sorting &amp; files
Unit 5  internal sorting &amp; filesUnit 5  internal sorting &amp; files
Unit 5 internal sorting &amp; files
 
Quick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.pptQuick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.ppt
 
quick_sort.ppt
quick_sort.pptquick_sort.ppt
quick_sort.ppt
 
Quicksort algorithm
Quicksort algorithmQuicksort algorithm
Quicksort algorithm
 
quicksort (1).ppt
quicksort (1).pptquicksort (1).ppt
quicksort (1).ppt
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
 
Quick & Merge Sort.ppt
Quick & Merge Sort.pptQuick & Merge Sort.ppt
Quick & Merge Sort.ppt
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Slides ads ia
Slides ads iaSlides ads ia
Slides ads ia
 
IA-advanced-R
IA-advanced-RIA-advanced-R
IA-advanced-R
 
Arrays
ArraysArrays
Arrays
 
Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Sorting
SortingSorting
Sorting
 
DSSchapt13.ppt
DSSchapt13.pptDSSchapt13.ppt
DSSchapt13.ppt
 
AA_Sorting.SI.ppt
AA_Sorting.SI.pptAA_Sorting.SI.ppt
AA_Sorting.SI.ppt
 

More from Maitree Patel

MACs based on Hash Functions, MACs based on Block Ciphers
MACs based on Hash Functions, MACs based on Block CiphersMACs based on Hash Functions, MACs based on Block Ciphers
MACs based on Hash Functions, MACs based on Block CiphersMaitree Patel
 
Form using html and java script validation
Form using html and java script validationForm using html and java script validation
Form using html and java script validationMaitree Patel
 
Software engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designSoftware engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designMaitree Patel
 
Introduction of Memory Management
Introduction of Memory Management Introduction of Memory Management
Introduction of Memory Management Maitree Patel
 
Scheduling Definition, objectives and types
Scheduling Definition, objectives and types Scheduling Definition, objectives and types
Scheduling Definition, objectives and types Maitree Patel
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer ProtocolMaitree Patel
 
Virtual circuit and Datagram network
Virtual circuit and Datagram networkVirtual circuit and Datagram network
Virtual circuit and Datagram networkMaitree Patel
 
Gauss Quadrature Formula
Gauss Quadrature FormulaGauss Quadrature Formula
Gauss Quadrature FormulaMaitree Patel
 
Static Import and access modifiers
Static Import and access modifiersStatic Import and access modifiers
Static Import and access modifiersMaitree Patel
 

More from Maitree Patel (11)

MACs based on Hash Functions, MACs based on Block Ciphers
MACs based on Hash Functions, MACs based on Block CiphersMACs based on Hash Functions, MACs based on Block Ciphers
MACs based on Hash Functions, MACs based on Block Ciphers
 
Form using html and java script validation
Form using html and java script validationForm using html and java script validation
Form using html and java script validation
 
Software engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designSoftware engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit design
 
Dotnet :Attributes
Dotnet :AttributesDotnet :Attributes
Dotnet :Attributes
 
Advance Java
Advance JavaAdvance Java
Advance Java
 
Introduction of Memory Management
Introduction of Memory Management Introduction of Memory Management
Introduction of Memory Management
 
Scheduling Definition, objectives and types
Scheduling Definition, objectives and types Scheduling Definition, objectives and types
Scheduling Definition, objectives and types
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer Protocol
 
Virtual circuit and Datagram network
Virtual circuit and Datagram networkVirtual circuit and Datagram network
Virtual circuit and Datagram network
 
Gauss Quadrature Formula
Gauss Quadrature FormulaGauss Quadrature Formula
Gauss Quadrature Formula
 
Static Import and access modifiers
Static Import and access modifiersStatic Import and access modifiers
Static Import and access modifiers
 

Recently uploaded

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 

Recently uploaded (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 

Merge sort and Quick sort

  • 1.  Design and Analysis of Algorithm VADODARA INSTITUTE OF ENGINEERING ACTIVE LEARNING ASSIGNEMENT TOPIC: QUICK AND MERGE SORT COMPUTER ENGINEERING(CE-1) Presented By: Heta Patel 150800107041 Maitree Patel 150800107048 Riddhi Patel 150800107053
  • 2.  Contents  Quick Sort  Time Complexity of Quick Sort  Merge Sort  Time Complexity of Merge Sort
  • 3.  Quicksort Given an array of n elements (e.g., integers):  If array only contains one element, return  Else pick one element to use as pivot. Partition elements into two sub-arrays: Elements less than or equal to pivot Elements greater than pivot Quicksort two sub-arrays Return results
  • 4.  Partitioning Array Given a pivot, partition the elements of the array such that the resulting array consists of: One sub-array that contains elements >= pivot Another sub-array that contains elements < pivot The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements below/above pivot.
  • 5.  Quicksort Algorithm  Quicksort(a,low,high) (1)If low<high then  Set mid=partition(A,low,high)  Call Quicksort(A,low,mid-1)  Call Quicksort(A,mid+1,high) (2)End 40 20 10 80 60 50 7 30 100
  • 6.   partition(A,low,high) (1) [initialize]  Set pivot =A[low]  Set i=low+1  Set j=high (2) Repeat step 3,4,5  While i<=j (3)While A[i]<pivot  Then i++  [end of while] (4)While A[j]>pivot then  j- -  [end while] 40 20 10 80 60 50 7 30 100
  • 7.  (5)If i<j then  Swap A[i] and A[j]  [end if]  [end of main while] (6)Swap A[j] and pivot (7)Return j (8)exit 40 20 10 80 60 50 7 30 100
  • 8.  Pick Pivot Element There are a number of ways to pick the pivot element. In this example, we will use the first element in the array: 40 20 10 80 60 50 7 30 100
  • 9.  Example We are given array of n integers to sort: 40 20 10 80 60 50 7 30 100
  • 10.  40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 11.  40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i
  • 12.  40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i
  • 13.  40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i
  • 14.  40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j
  • 15.  40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j
  • 16.  40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j]
  • 17.  40 20 10 30 60 50 7 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j]
  • 18.  40 20 10 30 60 50 7 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1.
  • 19.  40 20 10 30 60 50 7 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1.
  • 20.  40 20 10 30 60 50 7 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1.
  • 21.  40 20 10 30 60 50 7 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1.
  • 22.  40 20 10 30 60 50 7 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1.
  • 23.  40 20 10 30 60 50 7 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1.
  • 24.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 25.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 26.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 27.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 28.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 29.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 30.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 31.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 32.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 33.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 5. Swap data[j] and data[pivot_index] 40 20 10 30 7 50 60 80 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j
  • 34.  1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 5. Swap data[j] and data[pivot_index] 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j pivot_index = 4
  • 35.  Partition Result 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] >data[pivot]
  • 36.  Time Complexity of quick sort  T(N) = T(i) + T(N - i -1) + cN  The time to sort the file is equal to  the time to sort the left partition with i elements, plus  the time to sort the right partition with N-i- 1 elements, plus  the time to build the partitions
  • 37.  Worst case analysis The pivot is the smallest element T(N) = T(N-1) + cN, N > 1 Telescoping: T(N-1) = T(N-2) + c(N-1) T(N-2) = T(N-3) + c(N-2) T(N-3) = T(N-4) + c(N-3) T(2) = T(1) + c.2 Add all equations: T(N) + T(N-1) + T(N-2) + … + T(2) = = T(N-1) + T(N-2) + … + T(2) + T(1) + c(N) + c(N-1) + c(N-2) + … + c.2 T(N) = T(1) + c(2 + 3 + … + N) T(N) = 1 + c(N(N+1)/2 -1) Therefore T(N) = O(N2 )
  • 38.   t(n) = 2t(n/2) + cn for best case time complexity is: Θ(nlogn) For worst case time complexity is: Θ(n²) For average case time complexity is: Θ(nlogn)
  • 39.   Advantages:  One of the fastest algorithms on average.  Does not need additional memory (the sorting takes place in the array - this is called in-place processing). Compare with mergesort: mergesort needs additional memory for merging.
  • 40.  Merge sort  ALGORITHM  MERGE-SORT (A, L, H)  1. IF L< H 2. THEN m= [(L+ H)/2] 3. MERGE (A, L, M) 4. MERGE (A, M + 1, H) 5. MERGE (A, L, H, M) 
  • 41.  ALGORITHM  COMBINE(A,L,H,M)  I=L, K=L, J=M+1  WHILE(I<=M && J<=H)  IF A[I]<A[J]  TEMP[K]=A[I]  I++,K++  ELSE  TEMP[K]=A[J]  I++,K++
  • 42.  ALGORITHM  Whille(I<=M)  TEMP[K]=A[I]  I++,K++  WHILE(J<=H)  TEMP[K]=A[J]  J++,K++  FOR K L TO H  A[K]=TEMP[K]
  • 43.  40 20 10 80 60 50 7 30 100 EXAMPLE MERGE SORT
  • 44. 40 20 10 80 60 50 7 30 100
  • 45. 40 20 10 80 60 50 7 30 100 40 20 10 80
  • 46. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100
  • 47. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20
  • 48. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80
  • 49. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50
  • 50. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100
  • 51. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 40 20 100
  • 52. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 40 20 10 80 100
  • 53. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 40 20 10 80 60 50 100
  • 54. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 40 20 10 80 60 50 7 30 100 100
  • 55. 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 100 40 20 10 80 60 50 7 30 40 20 10 80 60 50 7 30 100 100 30 100
  • 56. 40 20 10 80 60 50 7 30 100 i j
  • 57. 20 20 10 80 60 50 7 30 100 i j 40
  • 58. 20 40 10 80 60 50 7 30 100
  • 59. 20 40 10 80 60 50 7 30 100 i j
  • 60. 20 40 10 10 80 60 50 7 30 100 i j
  • 61. 20 40 10 80 80 60 50 7 30 100 j
  • 62. 20 40 10 80 60 50 7 30 100 i j
  • 63. 10 20 40 10 80 60 50 7 30 100 i j
  • 64. 10 20 20 40 10 80 60 50 7 30 100 i j
  • 65. 10 20 40 20 40 10 80 60 50 7 30 100 j
  • 66. 10 20 40 80 20 40 10 80 60 50 7 30 100 j
  • 67. 10 20 40 80 60 50 7 30 100 ji
  • 68. 10 20 40 80 50 60 50 7 30 100 ji
  • 69. 10 20 40 80 50 60 60 50 7 30 100 ji
  • 70. 10 20 40 80 50 60 7 30 100
  • 71. 10 20 40 80 50 60 7 30 100 ji
  • 72. 10 20 40 80 50 60 7 30 30 100 ji
  • 73. 10 20 40 80 50 60 7 30 100 30 100 ji
  • 74. 10 20 40 80 50 60 7 30 100
  • 75. 10 20 40 80 50 60 7 30 100 ji
  • 76. 10 20 40 80 50 60 7 7 30 100 ji
  • 77. 10 20 40 80 50 60 7 30 7 30 100 ji
  • 78. 10 20 40 80 50 60 7 30 7 30 100 100 ji
  • 79. 10 20 40 80 50 60 7 30 100
  • 80. 10 20 40 80 50 60 7 30 100 ji
  • 81. 10 20 40 80 7 50 60 7 30 100 ji
  • 82. 10 20 40 80 7 50 60 7 30 100 ji
  • 83. 10 20 40 80 7 30 50 60 7 30 100 ji
  • 84. 10 20 40 80 7 30 50 50 60 7 30 100 ji
  • 85. 10 20 40 80 7 30 50 60 50 60 7 30 100 ji
  • 86. 10 20 40 80 7 30 50 60 100 50 60 7 30 100 ji
  • 87. 10 20 40 80 7 30 50 60 100
  • 88. 10 20 40 80 7 30 50 60 100 i j
  • 89. 7 10 20 40 80 7 30 50 60 100 i j
  • 90. 7 10 10 20 40 80 7 30 50 60 100 i j
  • 91. 7 10 20 10 20 40 80 7 30 50 60 100 i j
  • 92. 7 10 20 30 10 20 40 80 7 30 50 60 100 i j
  • 93. 7 10 20 30 40 10 20 40 80 7 30 50 60 100 i j
  • 94. 7 10 20 30 40 50 10 20 40 80 7 30 50 60 100 i j
  • 95. 7 10 20 30 40 50 60 10 20 40 80 7 30 50 60 100 i j
  • 96. 7 10 20 30 40 50 60 80 10 20 40 80 7 30 50 60 100 i j
  • 97. 7 10 20 30 40 50 60 80 100 10 20 40 80 7 30 50 60 100 i j
  • 98.  40 50 60 80 1007 10 20 30 Hence we got he sorted array:
  • 100.   How many times are these steps executed?  For this, look at the tree below - for each level from top to bottom Level 2 calls merge method on 2 sub-arrays of length n/2 each.  The complexity here is 2 * (cn/2) = cn Level 3 calls merge method on 4 sub-arrays of length n/4 each. The complexity here is 4 * (cn/4) = cn and so on ...  Now, the height of this tree is (logn + 1) for a given n. Thus the overall complexity is (logn + 1)*(cn). That is O(nlogn) for the merge sort algorithm.
  • 101.   t(n) = 2t(n/2) + Cn for best case time complexity is: Θ(n log(n)) For worst case time complexity is: Θ(n log(n)) For average case time complexity is: Θ(n log(n))
  • 102.   The advantages to merge sort is it is always fast. Even in its worst case its runtime is O(nlogn).  Disadvantages of Merge sort are that it is not in place so merge sort uses a lot of memory. It uses extra space
  • 103.  References  https://www.khanacademy.org/computing/computer-science/algorithms/quick  https://softwareengineering.stackexchange.com/questions/297160/why-is-m  http://cs.fit.edu/~pkc/classes/writing/hw13/luis.pdf