SlideShare a Scribd company logo
1 of 64
Data Structures
Recurrences
Divide-and-Conquer Problems
problem
Divide
subproblemsubproblem subproblem
Conquer subproblem subproblem
subproblem
Combine problem
Recurrences
Divide-and-Conquer Problems
•Binary Search
• Merge Sort
• Quick Sort
• Selection Sort
• Search Maximum
• Multiplying Large Integers
• Multiplying Chain of Matrices
Divide the problems into a number of sub
problems.
Conquer the sub problems by solving
them recursively. If the sub-problem
sizes are small enough, just solve the
problems in a straight forward manner.
Combine the solutions to the sub
problems into the solution for the original
problem.
Divide and Conquer Approach
Divide the n element sequence to be
sorted into two subsequences of n/2
elements each.
Conquer: Sort the two subsequences to
produce the sorted answer.
Combine: Merge the two sorted sub
sequences to produce the sorted answer.
Merge Sort
Merge Sort
Base Case: When the sequences to be sorted has length
1.
108 56 1214 89 3466Unsorted
108 56 1466
Divide
10866
Divide
66
Divide
66
BCase
66
Merge
108
Divide
108
BCase
108
Merge
66 108
Merge
56 14
Divide
56
Divide
56
BCase
56
Merge
14
Divide
14
BCase
14
Merge
14 56
Merge
56 66 10814
Merge
1289 34
Divide
1289
Divide
89
Divide
89
BCase
89
Merge
12
Divide
12
BCase
12
Merge
8912
Merge
34
Divide
34
BCase
34
Merge
3412 89
Merge
14 34 8956 66 10812Sorted
Merge Sort Algorithm
MergeSort(A, i, j)
if j > i then
mid ← (i + j)/2
MergeSort(A, i, mid )
MergeSort(A, mid + 1, j )
Merge(A, i, mid, j )
Merge Algorithm
The basic merging algorithms takes
Two input arrays, A[] and B[],
An output array C[]
And three counters aptr, bptr and cptr. (initially set
to the beginning of their respective arrays)
The smaller of A[aptr] and B[bptr] is copied to the next
entry in C i.e. C[cptr].
The appropriate counters are then advanced.
When either of input list is exhausted, the remainder of
the other list is copied to C.
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
bptr
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
14 > 12 therefore
C[]
cptr
C[cptr] = 12
12
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
14 > 12 therefore
cptr++
cptr
C[]
bptr
C[cptr] = 12
12
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
14 > 12 therefore
cptr++
cptr
C[]
bptr
bptr++
C[cptr] = 12
12
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
14 < 34 therefore
cptr
C[]
bptr
12
C[cptr] = 14
14
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
14 < 34 therefore
cptr++
cptr
C[]
bptr
12
C[cptr] = 14
14
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
14 < 34 therefore
cptr++
cptr
C[]
bptr
aptr
aptr++
12
C[cptr] = 14
14
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
56 > 34 therefore
cptr
C[]
bptr
12 14
C[cptr] = 34
34
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
56 > 34 therefore
cptr++
cptr
C[]
bptr
12
C[cptr] = 34
14 34
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
56 > 34 therefore
cptr++
cptr
C[]
bptr
bptr++
12
C[cptr] = 34
14 34
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
56 < 89 therefore
cptr
C[]
bptr
12 14 34
C[cptr] = 56
56
Merge Algorithm
56 66 10814
A[]
aptr
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
56 < 89 therefore
cptr++
cptr
C[]
bptr
12 14 34
C[cptr] = 56
56
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
56 < 89 therefore
cptr++
cptr
C[]
bptr
aptr
aptr++
12 14 34
C[cptr] = 56
56
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
66 < 89 therefore
cptr
C[]
bptr
aptr
12 14 34 56
C[cptr] = 66
66
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
66 < 89 therefore
cptr++
cptr
C[]
bptr
aptr
12 14 34
C[cptr] = 66
56 66
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
66 < 89 therefore
cptr++
cptr
C[]
bptr
aptr
aptr++
12 14 34
C[cptr] = 66
56 66
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
108 > 89 therefore
cptr
C[]
bptr
aptr
12 14 34 56 66
C[cptr] = 89
89
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
108 > 89 therefore
cptr++
cptr
C[]
bptr
aptr
12 14 34 56 66
C[cptr] = 89
89
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
108 > 89 therefore
cptr++
cptr
C[]
bptr
aptr
bptr++
12 14 34 56 66
C[cptr] = 89
89
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
Array B is now
finished, copy
remaining elements of
array A in array C
cptr
C[]
bptr
aptr
12 14 34 56 66 89
Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
Array B is now
finished, copy
remaining elements of
array A in array C
cptr
C[]
bptr
aptr
12 14 34 56 66 89 108
Computation Tree
108 56 1214 89 3466
108 56 1466 1289 34
10866 56 14 1289 34
66 108 56 14 89 12
N = 7
lg 7  = 3
Tree Depth = 3
Problem with Merge Sort
Merging two sorted lists requires linear extra
memory.
Additional work spent copying to the
temporary array and back through out the
algorithm.
This problem slows down the sort considerably.
Quick Sort
Division of arrays in such a way that
The sorted sub arrays do not need to be later
merged.
This can be done by rearranging the elements in
A(1:n) such that
A(i) <= A(j) for all i between 1 and m and all j
between m+1 and n.
Thus the elements in A(1:m) and A(m+1: n)
may be independently sorted. No merge is
Quick Sort Algorithm
To sort an array S
1. If the number of elements in S is 0 or 1, then
return.
2. Pick any element V in S. This is called pivot.
3. Partition S-{V} (the remaining elements in S)
into two disjoint groups:
S1= {x ∈ S – {V} | x <= V},
S2= {x ∈ S – {V} | x >= V}
4. Return {quickSort(S1) followed by v followed
by quickSort(S ) }
Quick Sort Partitioning
Rearrange the array such that
Element V (pivot) is in its final position
None of elements in A[1] .. A[m] is > V
None of elements in A[m+1] .. A[n] is < V
pivot
elements lower
than pivot
elements higher
than pivot
← unsorted → ← unsorted →
Quick Sort Partitioning
Partition step/strategy is a design
decision.
Picking the Pivot
Best strategy would be to pick the median
of the array.
Median of three partitioning.
Quick Sort Partitioning
Picking the Pivot:
A = 8, 1, 4, 9, 6, 3, 5, 2, 7, 0
Pick three elements randomly and the
median of these three numbers is the
pivot.
Quick Sort Partitioning
1 4 39 6 58 2 7 0
Left = 8, Right = 0,
Center = (Left + Right)/2 = 4
• 4th
elements of the array started with 0 is the
pivot => 6
1 4 39 6 58 2 7 0
Quick Sort Partitioning
Replace the pivot with the last element
1 4 39 6 58 2 7 0
1 4 39 0 58 2 7 6
i j
Two indicies, i starts from the first element.
j starts from the second last element
Move all the smaller elements to the left and all
the larger elements to the right (small and large is
relative to the pivot).
Quick Sort Partitioning
1 4 39 0 58 2 7 6
i j
A[ j ] > pivot therefore decrement j
1 4 39 0 58 2 7 6
i j
A[i] > pivot
Quick Sort Partitioning
Swap A[i] and A[ j ]
1 4 39 0 58 2 7 6
i j
A[ j ] < pivot
1 4 39 0 52 8 7 6
i j
A[ i ] < pivot increment i until A[ i ] becomes
greater than pivot
Quick Sort Partitioning
1 4 39 0 52 8 7 6
i j
1 4 39 0 52 8 7 6
i j
i
1 4 39 0 52 8 7 6
j
A[ i ] > pivot.
A[ j ] > pivot therefore decrement j until A[ j ] < pivot
Quick Sort Partitioning
i
1 4 39 0 52 8 7 6
j
A[ j ] < pivot. Swap A[ i ] and A [ j ]
i
1 4 35 0 92 8 7 6
j
A[ i ] < pivot increment i until A[ i ] becomes
greater than pivot
Quick Sort Partitioning
i
1 4 35 0 92 8 7 6
j
i
1 4 35 0 92 8 7 6
j
i
1 4 35 0 92 8 7 6
j
A[ i ] > pivot.
A[ j ] > pivot therefore decrement j until A[ j ] < pivot
Quick Sort Partitioning
i
1 4 35 0 92 8 7 6
j
A[ j ] < pivot.
i and j crosses each other therefore swap A[ i ]
with pivot.
All less then pivot
1 4 35 0 62 8 7 9
All greater then
pivot
Quick Sort Partitioning
All less then pivot
Apply same strategy on
this sublist separately
1 4 35 0 62 8 7 9
All greater then
pivot
Apply same
strategy on this
sublist
separately
Quick Sort Algorithm
QuickSort(A, left, right) {
if right > left then {
Pivot = Partition(A, left, right);
QuickSort(A, left, pivot-1);
QuickSort(A, pivot+1, right);
}
}
Partition Algorithm
Partition(a[ ], left, right ) {
int i, j;
int Pivot = (left+right) / 2;
Swap(A[Pivot], A[right]);
i = left; j = right - 1;
Pivot = right;
while ( i < j ) {
while( a[ i ] <= a[Pivot]) i++;
while( a[ j ] >= a[Pivot]) j- -;
if ( i < j ) SWAP(a[i], a[ j ]);
}
Swap(A[ i ], A[Pivot])
return i
}
Selection Sort
Algorithm
To sort an array A[1…n], consisting of n elements,
the selection sort procedure is as follows. Scan
array A[1..n] to find the minimum element.
Swap minimum element with A[1]
Scan sub-array A[2..n] to find the minimum element.
Swap minimum element with A[2]
Scan sub-array A[3..n] to find the minimum element.
Swap minimum element with A[3]
Scan sub-array A[n-1.n] to find the minimum
element. Swap minimum element with A[n-1]
At the end array A[1…n] is sorted
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting
Data Structure Sorting

More Related Content

What's hot

06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear TimeAndres Mendez-Vazquez
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and SortingMuhammadBakri13
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Sorting and searching
Sorting and searchingSorting and searching
Sorting and searchingkalyanineve
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sortingryokollll
 
Searching/Sorting algorithms
Searching/Sorting algorithmsSearching/Sorting algorithms
Searching/Sorting algorithmsHuy Nguyen
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingMvenkatarao
 
Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsAakash deep Singhal
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sortUmmar Hayat
 

What's hot (20)

Algorithms
AlgorithmsAlgorithms
Algorithms
 
06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear Time
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting and searching
Sorting and searchingSorting and searching
Sorting and searching
 
Quicksort
QuicksortQuicksort
Quicksort
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Mergesort
MergesortMergesort
Mergesort
 
Searching/Sorting algorithms
Searching/Sorting algorithmsSearching/Sorting algorithms
Searching/Sorting algorithms
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
 
Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithms
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 

Viewers also liked

Programacion en C++ para Ciencia e Ingeniería
Programacion en C++ para Ciencia e IngenieríaProgramacion en C++ para Ciencia e Ingeniería
Programacion en C++ para Ciencia e IngenieríaStorti Mario
 
Algoritmos y Estructuras de Datos
Algoritmos y Estructuras de DatosAlgoritmos y Estructuras de Datos
Algoritmos y Estructuras de DatosStorti Mario
 
Bca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structureBca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structureRai University
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topicSaddam Hussain
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sortgeeortiz
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 

Viewers also liked (15)

Merge sort
Merge sortMerge sort
Merge sort
 
Working of Merge Sort Code
Working of Merge Sort CodeWorking of Merge Sort Code
Working of Merge Sort Code
 
Programacion en C++ para Ciencia e Ingeniería
Programacion en C++ para Ciencia e IngenieríaProgramacion en C++ para Ciencia e Ingeniería
Programacion en C++ para Ciencia e Ingeniería
 
Algoritmos y Estructuras de Datos
Algoritmos y Estructuras de DatosAlgoritmos y Estructuras de Datos
Algoritmos y Estructuras de Datos
 
Bca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structureBca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structure
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 

Similar to Data Structure Sorting

Similar to Data Structure Sorting (20)

s4_quick_sort.ppt
s4_quick_sort.ppts4_quick_sort.ppt
s4_quick_sort.ppt
 
quicksort (1).ppt
quicksort (1).pptquicksort (1).ppt
quicksort (1).ppt
 
Quick sort.pptx
Quick sort.pptxQuick sort.pptx
Quick sort.pptx
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C ProgrammingInsert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programming
 
Divide-and-conquer
Divide-and-conquerDivide-and-conquer
Divide-and-conquer
 
Quick sort
Quick sortQuick sort
Quick sort
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
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
 
Merge sort and Quick sort
Merge sort and Quick sortMerge sort and Quick sort
Merge sort and Quick sort
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
quick_sort.ppt
quick_sort.pptquick_sort.ppt
quick_sort.ppt
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
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
 

More from Muhazzab Chouhadry

More from Muhazzab Chouhadry (6)

Install guid to SQL server 2008
Install guid to SQL server 2008Install guid to SQL server 2008
Install guid to SQL server 2008
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Sentence and its types
Sentence and its typesSentence and its types
Sentence and its types
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Data Structure Sorting

  • 3. Recurrences Divide-and-Conquer Problems •Binary Search • Merge Sort • Quick Sort • Selection Sort • Search Maximum • Multiplying Large Integers • Multiplying Chain of Matrices
  • 4. Divide the problems into a number of sub problems. Conquer the sub problems by solving them recursively. If the sub-problem sizes are small enough, just solve the problems in a straight forward manner. Combine the solutions to the sub problems into the solution for the original problem. Divide and Conquer Approach
  • 5. Divide the n element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences to produce the sorted answer. Combine: Merge the two sorted sub sequences to produce the sorted answer. Merge Sort
  • 6. Merge Sort Base Case: When the sequences to be sorted has length 1. 108 56 1214 89 3466Unsorted 108 56 1466 Divide 10866 Divide 66 Divide 66 BCase 66 Merge 108 Divide 108 BCase 108 Merge 66 108 Merge 56 14 Divide 56 Divide 56 BCase 56 Merge 14 Divide 14 BCase 14 Merge 14 56 Merge 56 66 10814 Merge 1289 34 Divide 1289 Divide 89 Divide 89 BCase 89 Merge 12 Divide 12 BCase 12 Merge 8912 Merge 34 Divide 34 BCase 34 Merge 3412 89 Merge 14 34 8956 66 10812Sorted
  • 7. Merge Sort Algorithm MergeSort(A, i, j) if j > i then mid ← (i + j)/2 MergeSort(A, i, mid ) MergeSort(A, mid + 1, j ) Merge(A, i, mid, j )
  • 8. Merge Algorithm The basic merging algorithms takes Two input arrays, A[] and B[], An output array C[] And three counters aptr, bptr and cptr. (initially set to the beginning of their respective arrays) The smaller of A[aptr] and B[bptr] is copied to the next entry in C i.e. C[cptr]. The appropriate counters are then advanced. When either of input list is exhausted, the remainder of the other list is copied to C.
  • 9. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] bptr If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 14 > 12 therefore C[] cptr C[cptr] = 12 12
  • 10. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 14 > 12 therefore cptr++ cptr C[] bptr C[cptr] = 12 12
  • 11. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 14 > 12 therefore cptr++ cptr C[] bptr bptr++ C[cptr] = 12 12
  • 12. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 14 < 34 therefore cptr C[] bptr 12 C[cptr] = 14 14
  • 13. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 14 < 34 therefore cptr++ cptr C[] bptr 12 C[cptr] = 14 14
  • 14. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 14 < 34 therefore cptr++ cptr C[] bptr aptr aptr++ 12 C[cptr] = 14 14
  • 15. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 56 > 34 therefore cptr C[] bptr 12 14 C[cptr] = 34 34
  • 16. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 56 > 34 therefore cptr++ cptr C[] bptr 12 C[cptr] = 34 14 34
  • 17. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 56 > 34 therefore cptr++ cptr C[] bptr bptr++ 12 C[cptr] = 34 14 34
  • 18. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 56 < 89 therefore cptr C[] bptr 12 14 34 C[cptr] = 56 56
  • 19. Merge Algorithm 56 66 10814 A[] aptr 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 56 < 89 therefore cptr++ cptr C[] bptr 12 14 34 C[cptr] = 56 56
  • 20. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 56 < 89 therefore cptr++ cptr C[] bptr aptr aptr++ 12 14 34 C[cptr] = 56 56
  • 21. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 66 < 89 therefore cptr C[] bptr aptr 12 14 34 56 C[cptr] = 66 66
  • 22. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 66 < 89 therefore cptr++ cptr C[] bptr aptr 12 14 34 C[cptr] = 66 56 66
  • 23. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 66 < 89 therefore cptr++ cptr C[] bptr aptr aptr++ 12 14 34 C[cptr] = 66 56 66
  • 24. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 108 > 89 therefore cptr C[] bptr aptr 12 14 34 56 66 C[cptr] = 89 89
  • 25. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 108 > 89 therefore cptr++ cptr C[] bptr aptr 12 14 34 56 66 C[cptr] = 89 89
  • 26. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] 108 > 89 therefore cptr++ cptr C[] bptr aptr bptr++ 12 14 34 56 66 C[cptr] = 89 89
  • 27. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] Array B is now finished, copy remaining elements of array A in array C cptr C[] bptr aptr 12 14 34 56 66 89
  • 28. Merge Algorithm 56 66 10814 A[] 3412 89 B[] If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] Array B is now finished, copy remaining elements of array A in array C cptr C[] bptr aptr 12 14 34 56 66 89 108
  • 29. Computation Tree 108 56 1214 89 3466 108 56 1466 1289 34 10866 56 14 1289 34 66 108 56 14 89 12 N = 7 lg 7  = 3 Tree Depth = 3
  • 30. Problem with Merge Sort Merging two sorted lists requires linear extra memory. Additional work spent copying to the temporary array and back through out the algorithm. This problem slows down the sort considerably.
  • 31. Quick Sort Division of arrays in such a way that The sorted sub arrays do not need to be later merged. This can be done by rearranging the elements in A(1:n) such that A(i) <= A(j) for all i between 1 and m and all j between m+1 and n. Thus the elements in A(1:m) and A(m+1: n) may be independently sorted. No merge is
  • 32. Quick Sort Algorithm To sort an array S 1. If the number of elements in S is 0 or 1, then return. 2. Pick any element V in S. This is called pivot. 3. Partition S-{V} (the remaining elements in S) into two disjoint groups: S1= {x ∈ S – {V} | x <= V}, S2= {x ∈ S – {V} | x >= V} 4. Return {quickSort(S1) followed by v followed by quickSort(S ) }
  • 33. Quick Sort Partitioning Rearrange the array such that Element V (pivot) is in its final position None of elements in A[1] .. A[m] is > V None of elements in A[m+1] .. A[n] is < V pivot elements lower than pivot elements higher than pivot ← unsorted → ← unsorted →
  • 34. Quick Sort Partitioning Partition step/strategy is a design decision. Picking the Pivot Best strategy would be to pick the median of the array. Median of three partitioning.
  • 35. Quick Sort Partitioning Picking the Pivot: A = 8, 1, 4, 9, 6, 3, 5, 2, 7, 0 Pick three elements randomly and the median of these three numbers is the pivot.
  • 36. Quick Sort Partitioning 1 4 39 6 58 2 7 0 Left = 8, Right = 0, Center = (Left + Right)/2 = 4 • 4th elements of the array started with 0 is the pivot => 6 1 4 39 6 58 2 7 0
  • 37. Quick Sort Partitioning Replace the pivot with the last element 1 4 39 6 58 2 7 0 1 4 39 0 58 2 7 6 i j Two indicies, i starts from the first element. j starts from the second last element Move all the smaller elements to the left and all the larger elements to the right (small and large is relative to the pivot).
  • 38. Quick Sort Partitioning 1 4 39 0 58 2 7 6 i j A[ j ] > pivot therefore decrement j 1 4 39 0 58 2 7 6 i j A[i] > pivot
  • 39. Quick Sort Partitioning Swap A[i] and A[ j ] 1 4 39 0 58 2 7 6 i j A[ j ] < pivot 1 4 39 0 52 8 7 6 i j A[ i ] < pivot increment i until A[ i ] becomes greater than pivot
  • 40. Quick Sort Partitioning 1 4 39 0 52 8 7 6 i j 1 4 39 0 52 8 7 6 i j i 1 4 39 0 52 8 7 6 j A[ i ] > pivot. A[ j ] > pivot therefore decrement j until A[ j ] < pivot
  • 41. Quick Sort Partitioning i 1 4 39 0 52 8 7 6 j A[ j ] < pivot. Swap A[ i ] and A [ j ] i 1 4 35 0 92 8 7 6 j A[ i ] < pivot increment i until A[ i ] becomes greater than pivot
  • 42. Quick Sort Partitioning i 1 4 35 0 92 8 7 6 j i 1 4 35 0 92 8 7 6 j i 1 4 35 0 92 8 7 6 j A[ i ] > pivot. A[ j ] > pivot therefore decrement j until A[ j ] < pivot
  • 43. Quick Sort Partitioning i 1 4 35 0 92 8 7 6 j A[ j ] < pivot. i and j crosses each other therefore swap A[ i ] with pivot. All less then pivot 1 4 35 0 62 8 7 9 All greater then pivot
  • 44. Quick Sort Partitioning All less then pivot Apply same strategy on this sublist separately 1 4 35 0 62 8 7 9 All greater then pivot Apply same strategy on this sublist separately
  • 45. Quick Sort Algorithm QuickSort(A, left, right) { if right > left then { Pivot = Partition(A, left, right); QuickSort(A, left, pivot-1); QuickSort(A, pivot+1, right); } }
  • 46. Partition Algorithm Partition(a[ ], left, right ) { int i, j; int Pivot = (left+right) / 2; Swap(A[Pivot], A[right]); i = left; j = right - 1; Pivot = right; while ( i < j ) { while( a[ i ] <= a[Pivot]) i++; while( a[ j ] >= a[Pivot]) j- -; if ( i < j ) SWAP(a[i], a[ j ]); } Swap(A[ i ], A[Pivot]) return i }
  • 47.
  • 48. Selection Sort Algorithm To sort an array A[1…n], consisting of n elements, the selection sort procedure is as follows. Scan array A[1..n] to find the minimum element. Swap minimum element with A[1] Scan sub-array A[2..n] to find the minimum element. Swap minimum element with A[2] Scan sub-array A[3..n] to find the minimum element. Swap minimum element with A[3] Scan sub-array A[n-1.n] to find the minimum element. Swap minimum element with A[n-1] At the end array A[1…n] is sorted