SlideShare a Scribd company logo
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
}
Data Structure Sorting
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

PPT
(Data Structure) Chapter11 searching & sorting
PPTX
Lecture 13 data structures and algorithms
PPTX
Sorting ppt
PPTX
6.queue
PDF
Sorting
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PDF
Sorting
PPTX
Presentation
(Data Structure) Chapter11 searching & sorting
Lecture 13 data structures and algorithms
Sorting ppt
6.queue
Sorting
358 33 powerpoint-slides_14-sorting_chapter-14
Sorting
Presentation

What's hot (20)

PDF
Algorithms
PDF
06 Analysis of Algorithms: Sorting in Linear Time
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PDF
Chapter 14 Searching and Sorting
PPTX
Searching & Sorting Algorithms
PPT
Unit 7 sorting
PPT
Counting sort(Non Comparison Sort)
PPTX
Sorting algorithms
PPSX
Sorting and searching
PPT
Quicksort
PPT
Lect11 Sorting
PPTX
Sorting algorithms
PPT
Mergesort
PDF
Searching/Sorting algorithms
PPTX
Unit 7 sorting
PPT
Introduction to Data Structures Sorting and searching
PPTX
Lecture 7 data structures and algorithms
PPTX
Unit 2 algorithm
PPT
3.8 quick sort
PPT
Insertion sort bubble sort selection sort
Algorithms
06 Analysis of Algorithms: Sorting in Linear Time
Data Structures - Lecture 8 [Sorting Algorithms]
Chapter 14 Searching and Sorting
Searching & Sorting Algorithms
Unit 7 sorting
Counting sort(Non Comparison Sort)
Sorting algorithms
Sorting and searching
Quicksort
Lect11 Sorting
Sorting algorithms
Mergesort
Searching/Sorting algorithms
Unit 7 sorting
Introduction to Data Structures Sorting and searching
Lecture 7 data structures and algorithms
Unit 2 algorithm
3.8 quick sort
Insertion sort bubble sort selection sort
Ad

Viewers also liked (15)

PPT
Merge sort
PDF
Working of Merge Sort Code
PDF
Programacion en C++ para Ciencia e Ingeniería
PDF
Algoritmos y Estructuras de Datos
PPTX
Bca ii dfs u-4 sorting and searching structure
PPT
Bubble sort a best presentation topic
PPT
chapter - 6.ppt
PPTX
Insertion sort
PPT
Sorting
PPT
Binary tree
PDF
Quick Sort , Merge Sort , Heap Sort
PPT
Sorting Algorithms
PPTX
Bubble Sort
PPT
Data Structures - Searching & sorting
PPT
Quick Sort
Merge sort
Working of Merge Sort Code
Programacion en C++ para Ciencia e Ingeniería
Algoritmos y Estructuras de Datos
Bca ii dfs u-4 sorting and searching structure
Bubble sort a best presentation topic
chapter - 6.ppt
Insertion sort
Sorting
Binary tree
Quick Sort , Merge Sort , Heap Sort
Sorting Algorithms
Bubble Sort
Data Structures - Searching & sorting
Quick Sort
Ad

Similar to Data Structure Sorting (20)

PPTX
MERGE and Quick Sort algorithm explain ppt
PPT
s4_quick_sort.ppt
PPT
quicksort (1).ppt
PPTX
Quick sort.pptx
PPT
Insert Sort & Merge Sort Using C Programming
PPTX
Divide-and-conquer
PPTX
Quick sort
PPT
quick_sort_with_explanationandImplmentation.ppt
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
PPTX
Introduction to Algorithms
PPTX
Sortings .pptx
PPT
search_sort Search sortSearch sortSearch sortSearch sort
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
PPT
Merge sort and Quick sort
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
PDF
Unit ii divide and conquer -1
PPT
quick_sort.ppt
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
MERGE and Quick Sort algorithm explain ppt
s4_quick_sort.ppt
quicksort (1).ppt
Quick sort.pptx
Insert Sort & Merge Sort Using C Programming
Divide-and-conquer
Quick sort
quick_sort_with_explanationandImplmentation.ppt
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Introduction to Algorithms
Sortings .pptx
search_sort Search sortSearch sortSearch sortSearch sort
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Merge sort and Quick sort
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
Unit ii divide and conquer -1
quick_sort.ppt
Analysis of Algorithm (Bubblesort and Quicksort)

More from Muhazzab Chouhadry (6)

PPTX
Install guid to SQL server 2008
PPTX
Stack in Sata Structure
PPT
Queue in Data Structure
PPTX
Linked lists in Data Structure
PPT
Tree and Binary Search tree
PPTX
Sentence and its types
Install guid to SQL server 2008
Stack in Sata Structure
Queue in Data Structure
Linked lists in Data Structure
Tree and Binary Search tree
Sentence and its types

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
master seminar digital applications in india
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PPTX
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
master seminar digital applications in india
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Anesthesia in Laparoscopic Surgery in India
human mycosis Human fungal infections are called human mycosis..pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
A systematic review of self-coping strategies used by university students to ...
2.FourierTransform-ShortQuestionswithAnswers.pdf
GDM (1) (1).pptx small presentation for students
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
Cell Types and Its function , kingdom of life

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 }
  • 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