KEMBAR78
Weak 11-12 Sorting update.pptxbhjiiuuuuu | PPTX
Sorting
1
Sorting Integers
20 8 5 10 7
5 7 8 10 20
How to sort the integers in this array?
Elementary Sorting Algorithms
Selection Sort
Insertion Sort
Bubble Sort
Selection Sort
 Main idea:
find the smallest element
put it in the first position
find the next smallest element
put it in the second position
…
 And so on, until you get to the end of the list
1 2 3
0
7 12 19
5
a: 19
1 2 3
0
7 19 12
5
a: 12
1 2 3
0
19 7 12
5
a: 7
Selection Sort -- Example
7 12 19
5
a:
1 2 3
0
5 7 12
19
a:
a:
1 2 3
0
5
Swap Action (SelectionSorting)
5 8 20 10 7
5 7 20 10 8
5 7 8 10 20
20 8 5 10 7
5 7 8 10 20
Selection Sort Analysis
 What is the time complexity of this algorithm?
 Worst case == Best case == Average case
 Each iteration performs a linear search on the rest of the
array
 first element N +
 second element N-1 +
 …
 penultimate element 2 +
 last element 1
 Total N(N+1)/2
= (N2
+N)/2
Insertion Sort
 Basic idea (sorting cards):
Starts by considering the first two elements
of the array data, if out of order, swap them
Consider the third element, insert it into the
proper position among the first three
elements.
Consider the forth element, insert it into the
proper position among the first four
elements.
… …
Insertion Sort -- Example
1 2 3
0
12 5 7
19
a:
1 2 3
0
19 5 7
12
a:
1 2 3
0
12 19 7
5
a:
1 2 3
0
7 12 19
5
a:
Insertion Sort -- animation
1 2 3
0
12 5 7
19
a:
1 2 3
0
19 5 7
19
a:
1 2 3
0
5 7
19
a: 12
19
1 2 3
0
19 5 7
12
a:
12
Insertion Sort -- animation (cont)
1 2 3
0
12 19 7
5
a:
1 2 3
0
19 5 7
12
a:
1 2 3
0
5 7
12
a: 19 19
1 2 3
0
19 7
12
a: 19
12
1 2 3
0
12 19 7
12
a: 5
Insertion Sort -- animation (cont)
1 2 3
0
12 19 7
5
a:
1 2 3
0
12 19 7
5
a: 19
1 2 3
0
12 19 19
5
a: 12
1 2 3
0
12 12 19
5
a: 7
1 2 3
0
7 12 19
5
a:
Insertion Sort Analysis
 What is the time complexity of this algorithm?
 Worst case > Average case > Best case
 Each iteration inserts an element at the start of the
array, shifting all sorted elements along
• second element 2 +
• …
• penultimate element N-1 +
• last element N
• Total (2+N)(N-1)/2 = O(N2
)
Bubble Sort
 Basic idea (lighter bubbles rise to the top):
Exchange neighbouring items until the largest
item reaches the end of the array
Repeat for the rest of the array
1 2 3
0
12 7 19
5
a:
Bubble Sort -- Example
12 7 19
5
a:
19 12 7
5
1 2 3
0
a:
12 19 7
5
a:
1 2 3
0
5 12 7
19
1 2 3
0
a: a:
1 2 3
0
12 7 19
5
a:
1 2 3
0
7 12 19
5
a:
1 2 3
0
7 12 19
5
a:
1 2 3
0
7 12 19
5
a:
1 2 3
0
7 12 19
5
a:
Bubble Sort Analysis
 What is the time complexity of this
algorithm?
 Worst case > Average case > Best case
 Each iteration compares all the adjacent
elements, swapping them if necessary
• first iteration N +
• second iteration N-1 +
• …
• last iteration 1
• Total N(1+N)/2 = O(N2
)
Summary
 Insertion, Selection and Bubble sort:
Worst case time complexity is proportional to
N2
.
In-Place Algorithms?
Best sorting routines are N log(N)
NLogN Algorithms
 Divide and Conquer
 Merge Sort
 Quick Sort
 Heap Sort
Divide & Conquer
A strategy to solve large number of problems.
It has following stages:
Divide: divide the problem into small
number of pieces
Conquer: Solve each piece by applying
divide and conquer recursively
Combine: Rearrange the pieces
B_Search(A,value,low,high)
{
if (high < low)
return low
mid = (low + high) / 2
if (A[mid] >= value)
return B_Search (A, value, low, mid-1)
else
return B_Search (A, value, mid+1, high)
}
displayArray(A[], p, r)
{
if(p<r)
show A[p]
displayArray(A,p+1,end);
}
Divide & Conquer
Analysis
 To sort the halves  (n/2)2
+(n/2)2
 To merge the two halves  n
 So, for n=100, divide and conquer takes:
= (100/2)2
+ (100/2)2
+ 100
= 2500 + 2500 + 100
= 5100 (n2
= 10,000)
Search
Divide and Conquer
Search
Search
Recall: Binary Search
Sort
Divide and Conquer
Sort Sort
Sort Sort Sort Sort
Divide and Conquer
Combin
e
Combin
e
Combin
e
Mergesort
 Mergesort is a divide and conquer algorithm
that does exactly that.
 It splits the list in half
 Mergesorts the two halves
 Then merges the two sorted halves together
 Mergesort can be implemented recursively
Merge Sort
 Divide : Split A down the middle into
subsequences, each of size n/2
 Conquer : sort each subsequence by calling
merge sort recursively
 Merge : the two sorted sequences into a single
sorted list
Mergesort
 The mergesort algorithm involves three
steps:
If the number of items to sort is 0 or 1, return
Recursively sort the first and second halves
separately
Merge the two sorted halves into a sorted group
7 5 2 4 1 8 3 0
7 5 2 4 1 8 3 0
7 5
2 4 1 8 3 0
7 5 2 4 1 8 3 0
7 5 2 4
0 1 2 3 4 5 7 8
2 4 5 7 0 1 3 8
7 5
2 4 1 8 0 3
7 5 2 4 1 8 3 0
5 7 2 4
Algorithm Merge-sort
 Merge-sort(array A, int p, int r)
 If (p<r)
 q ‹— (p+r)/2
 Merge-sort(A,p,q) //sort A[p…
q]
 Merge-sort(A,q+1,r)//sort
A[q+1..r]
 Merge(A,p,q,r)
Merge Algorithm
 merge(array A, int p, int q,int r)
1. int B[p…r];
2. int i =k =p;
3. int j =q+1
4. while(i<=q) and (j<=r)
5. do if(A[i] <= A[j])
6. then B[k++] =A[i++]
7. else B[k++] =A[j++]
8. while(i<=q)
9. do B[k++] =A[i++]
10.while(j<=r)
11. do B[k++] = A[j++]
Merging: animation
4 8 10 12 2 5 7 11
2
Merging: animation
4 8 10 12 2 5 7 11
2 4
Merging: animation
4 8 10 12 2 5 7 11
2 4 5
Merging
4 8 10 12 2 5 7 11
2 4 5 7
Mergesort
8 12 11 2 7 5
4
10
Split the list in half.
8 12
4
10
Mergesort the left half.
Split the list in half. Mergesort the left half.
4
10
Split the list in half. Mergesort the left half.
10
Mergesort the right.
4
Mergesort
8 12 11 2 7 5
4
10
8 12
4
10
4
10
Mergesort the right half.
Merge the two halves.
10
4 8 12
12
8
Merge the two halves.
8
8 12
Mergesort
8 12 11 2 7 5
4
10
8 12
4
10
Merge the two halves.
4
10
Mergesort the right half. Merge the two halves.
10
4 8 12
10 12
8
4
10
4 8 12
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 7 5
11 2
11 2
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 7 5
11 2
2 11
2 11
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 7 5
2 11
7
5
7 5
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 5
7
2 11
Mergesort
10 12 2 5 7 11
8
4
Mergesort the right half.
Mergesort
5 7 8 10 11 12
4
2
Merge the two halves.
Mergesort Analysis
Merging the two lists of size n/2:
O(n)
Merging the four lists of size n/4:
O(n)
.
.
.
Merging the n lists of size 1:
O(n)
O (lg n)
times
 Mergesort is O(n lg n)
 Space?
 The other sorts we have looked at (insertion, selection)
are in-place (only require a constant amount of extra
space)
 Mergesort requires O(n) extra space for merging
Quicksort
 Quicksort is another divide and conquer
algorithm
 Quicksort is based on the idea of
partitioning (splitting) the list around a pivot
or split value
Quicksort Algorithm
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
Quick Sort
 We follow three steps recursively.
1:- Find Pivot that divides the array into two
halves.
2:- Quick Sort the Left Half
3:- Quick Sort the right half
Quick Sort
For Example Consider the following array to Sort
using Quick Sort Algorithm.
0 1 2 3 4 5
Pivot Left right(last element of array)
(First Element in the array )
Remember that All elements to the right of pivot
must be greater. Pivot = 5
Is pivot < right? No right = 4
So swap pivot and right
right
pivot left
2 1 3 4
5 6
2 1 3 4
5 6
Quick Sort
Example Solved pivot = 5
New Array will be left = 4
0 1 2 3 4 5
pivot
Left right(last element of array)
(First Element in the array )
Is pivot > left? yes
So No need to swap pivot and right
Just increment the index.
pivot
left right
2 1 3 5
4 6
2 1 3 4
5 6
Quick Sort
Example Solved pivot = 5
New Array will be left = 2
0 1 2 3 4 5
pivot
Left right(last element of array)
Is pivot > left? yes
So No need to swap pivot and right pivot = 5
Just increment the index. Left = 6
pivot
left right
Again Check Is pivot > Left? No
So swap pivot and left.
2 1 3 5
4 6
2 1 3 5
4 6
Quick Sort
Example Solved pivot = 5
New Array will be right = 6
0 1 2 3 4 5
pivot Left right(last element of array)
Is pivot < right? yes
So No need to swap pivot and right pivot = 5
Just increment the index. right = 3
pivot left right
Again Check Is pivot < right? No
So swap pivot and left.
2 1 3 6
4 5
2 1 3 6
4 5
Quick Sort
Example Solved pivot = 5
New Array will be left = 3
0 1 2 3 4 5
pivot
Left right(last element of array)
Is pivot > left? yes
So No need to swap pivot and right pivot = 5
Just increment the index. left = 1
pivot
left right
Again Check Is pivot > left? yes
So no need to swap pivot and left.
2 1 5 6
4 3
2 1 5 6
4 3
Quick Sort
Example Solved pivot = 5
New Array will be left = 1
0 1 2 3 4 5
pivot
Left right(last element of array)
Is pivot > left? yes
So No need to swap pivot and right pivot = 5
Just increment the index. left = 1
pivot
Left sub array left right
Now both left and right at the same position so
5 is piovor at the sorted position
2 1 5 6
4 3
2 1 5 6
4 3
Quick Sort
Example Solved
pivot
Left sub array left right
So pivot has divided the array into two halves.
Now we will quick sort the left sub array.
pivot = 4
right = 1
piv Left Right
Is Pivot < right? No
So swap the pivot and right elements.
2 1 5 6
4 3
2 1 5 6
4 3
Quick Sort
Example Solved
pivot = 4
pivot left = 1
Left Right
Pivot = 4
Left Right Pivot Left = 2
Again Check Is Pivot > Left? Yes
No Need to Swap So just Increment.
2 4 5 6
1 3
2 4 5 6
1 3

Weak 11-12 Sorting update.pptxbhjiiuuuuu

  • 1.
  • 2.
    Sorting Integers 20 85 10 7 5 7 8 10 20 How to sort the integers in this array?
  • 3.
    Elementary Sorting Algorithms SelectionSort Insertion Sort Bubble Sort
  • 4.
    Selection Sort  Mainidea: find the smallest element put it in the first position find the next smallest element put it in the second position …  And so on, until you get to the end of the list
  • 5.
    1 2 3 0 712 19 5 a: 19 1 2 3 0 7 19 12 5 a: 12 1 2 3 0 19 7 12 5 a: 7 Selection Sort -- Example 7 12 19 5 a: 1 2 3 0 5 7 12 19 a: a: 1 2 3 0 5
  • 6.
    Swap Action (SelectionSorting) 58 20 10 7 5 7 20 10 8 5 7 8 10 20 20 8 5 10 7 5 7 8 10 20
  • 7.
    Selection Sort Analysis What is the time complexity of this algorithm?  Worst case == Best case == Average case  Each iteration performs a linear search on the rest of the array  first element N +  second element N-1 +  …  penultimate element 2 +  last element 1  Total N(N+1)/2 = (N2 +N)/2
  • 8.
    Insertion Sort  Basicidea (sorting cards): Starts by considering the first two elements of the array data, if out of order, swap them Consider the third element, insert it into the proper position among the first three elements. Consider the forth element, insert it into the proper position among the first four elements. … …
  • 9.
    Insertion Sort --Example 1 2 3 0 12 5 7 19 a: 1 2 3 0 19 5 7 12 a: 1 2 3 0 12 19 7 5 a: 1 2 3 0 7 12 19 5 a:
  • 10.
    Insertion Sort --animation 1 2 3 0 12 5 7 19 a: 1 2 3 0 19 5 7 19 a: 1 2 3 0 5 7 19 a: 12 19 1 2 3 0 19 5 7 12 a: 12
  • 11.
    Insertion Sort --animation (cont) 1 2 3 0 12 19 7 5 a: 1 2 3 0 19 5 7 12 a: 1 2 3 0 5 7 12 a: 19 19 1 2 3 0 19 7 12 a: 19 12 1 2 3 0 12 19 7 12 a: 5
  • 12.
    Insertion Sort --animation (cont) 1 2 3 0 12 19 7 5 a: 1 2 3 0 12 19 7 5 a: 19 1 2 3 0 12 19 19 5 a: 12 1 2 3 0 12 12 19 5 a: 7 1 2 3 0 7 12 19 5 a:
  • 13.
    Insertion Sort Analysis What is the time complexity of this algorithm?  Worst case > Average case > Best case  Each iteration inserts an element at the start of the array, shifting all sorted elements along • second element 2 + • … • penultimate element N-1 + • last element N • Total (2+N)(N-1)/2 = O(N2 )
  • 14.
    Bubble Sort  Basicidea (lighter bubbles rise to the top): Exchange neighbouring items until the largest item reaches the end of the array Repeat for the rest of the array
  • 15.
    1 2 3 0 127 19 5 a: Bubble Sort -- Example 12 7 19 5 a: 19 12 7 5 1 2 3 0 a: 12 19 7 5 a: 1 2 3 0 5 12 7 19 1 2 3 0 a: a: 1 2 3 0 12 7 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a:
  • 16.
    Bubble Sort Analysis What is the time complexity of this algorithm?  Worst case > Average case > Best case  Each iteration compares all the adjacent elements, swapping them if necessary • first iteration N + • second iteration N-1 + • … • last iteration 1 • Total N(1+N)/2 = O(N2 )
  • 17.
    Summary  Insertion, Selectionand Bubble sort: Worst case time complexity is proportional to N2 . In-Place Algorithms? Best sorting routines are N log(N)
  • 18.
    NLogN Algorithms  Divideand Conquer  Merge Sort  Quick Sort  Heap Sort
  • 19.
    Divide & Conquer Astrategy to solve large number of problems. It has following stages: Divide: divide the problem into small number of pieces Conquer: Solve each piece by applying divide and conquer recursively Combine: Rearrange the pieces
  • 20.
    B_Search(A,value,low,high) { if (high <low) return low mid = (low + high) / 2 if (A[mid] >= value) return B_Search (A, value, low, mid-1) else return B_Search (A, value, mid+1, high) } displayArray(A[], p, r) { if(p<r) show A[p] displayArray(A,p+1,end); } Divide & Conquer
  • 21.
    Analysis  To sortthe halves  (n/2)2 +(n/2)2  To merge the two halves  n  So, for n=100, divide and conquer takes: = (100/2)2 + (100/2)2 + 100 = 2500 + 2500 + 100 = 5100 (n2 = 10,000)
  • 22.
  • 23.
    Sort Divide and Conquer SortSort Sort Sort Sort Sort
  • 24.
  • 25.
    Mergesort  Mergesort isa divide and conquer algorithm that does exactly that.  It splits the list in half  Mergesorts the two halves  Then merges the two sorted halves together  Mergesort can be implemented recursively
  • 26.
    Merge Sort  Divide: Split A down the middle into subsequences, each of size n/2  Conquer : sort each subsequence by calling merge sort recursively  Merge : the two sorted sequences into a single sorted list
  • 27.
    Mergesort  The mergesortalgorithm involves three steps: If the number of items to sort is 0 or 1, return Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group
  • 28.
    7 5 24 1 8 3 0 7 5 2 4 1 8 3 0 7 5 2 4 1 8 3 0 7 5 2 4 1 8 3 0 7 5 2 4
  • 29.
    0 1 23 4 5 7 8 2 4 5 7 0 1 3 8 7 5 2 4 1 8 0 3 7 5 2 4 1 8 3 0 5 7 2 4
  • 30.
    Algorithm Merge-sort  Merge-sort(arrayA, int p, int r)  If (p<r)  q ‹— (p+r)/2  Merge-sort(A,p,q) //sort A[p… q]  Merge-sort(A,q+1,r)//sort A[q+1..r]  Merge(A,p,q,r)
  • 31.
    Merge Algorithm  merge(arrayA, int p, int q,int r) 1. int B[p…r]; 2. int i =k =p; 3. int j =q+1 4. while(i<=q) and (j<=r) 5. do if(A[i] <= A[j]) 6. then B[k++] =A[i++] 7. else B[k++] =A[j++] 8. while(i<=q) 9. do B[k++] =A[i++] 10.while(j<=r) 11. do B[k++] = A[j++]
  • 32.
    Merging: animation 4 810 12 2 5 7 11 2
  • 33.
    Merging: animation 4 810 12 2 5 7 11 2 4
  • 34.
    Merging: animation 4 810 12 2 5 7 11 2 4 5
  • 35.
    Merging 4 8 1012 2 5 7 11 2 4 5 7
  • 36.
    Mergesort 8 12 112 7 5 4 10 Split the list in half. 8 12 4 10 Mergesort the left half. Split the list in half. Mergesort the left half. 4 10 Split the list in half. Mergesort the left half. 10 Mergesort the right. 4
  • 37.
    Mergesort 8 12 112 7 5 4 10 8 12 4 10 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 12 8 Merge the two halves. 8 8 12
  • 38.
    Mergesort 8 12 112 7 5 4 10 8 12 4 10 Merge the two halves. 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 10 12 8 4 10 4 8 12
  • 39.
    Mergesort 10 12 112 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 11 2
  • 40.
    Mergesort 10 12 112 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 2 11
  • 41.
    Mergesort 10 12 112 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5
  • 42.
    Mergesort 10 12 112 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11
  • 43.
    Mergesort 10 12 25 7 11 8 4 Mergesort the right half.
  • 44.
    Mergesort 5 7 810 11 12 4 2 Merge the two halves.
  • 45.
    Mergesort Analysis Merging thetwo lists of size n/2: O(n) Merging the four lists of size n/4: O(n) . . . Merging the n lists of size 1: O(n) O (lg n) times  Mergesort is O(n lg n)  Space?  The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space)  Mergesort requires O(n) extra space for merging
  • 46.
    Quicksort  Quicksort isanother divide and conquer algorithm  Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value
  • 47.
    Quicksort Algorithm Given anarray 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
  • 48.
    Quick Sort  Wefollow three steps recursively. 1:- Find Pivot that divides the array into two halves. 2:- Quick Sort the Left Half 3:- Quick Sort the right half
  • 49.
    Quick Sort For ExampleConsider the following array to Sort using Quick Sort Algorithm. 0 1 2 3 4 5 Pivot Left right(last element of array) (First Element in the array ) Remember that All elements to the right of pivot must be greater. Pivot = 5 Is pivot < right? No right = 4 So swap pivot and right right pivot left 2 1 3 4 5 6 2 1 3 4 5 6
  • 50.
    Quick Sort Example Solvedpivot = 5 New Array will be left = 4 0 1 2 3 4 5 pivot Left right(last element of array) (First Element in the array ) Is pivot > left? yes So No need to swap pivot and right Just increment the index. pivot left right 2 1 3 5 4 6 2 1 3 4 5 6
  • 51.
    Quick Sort Example Solvedpivot = 5 New Array will be left = 2 0 1 2 3 4 5 pivot Left right(last element of array) Is pivot > left? yes So No need to swap pivot and right pivot = 5 Just increment the index. Left = 6 pivot left right Again Check Is pivot > Left? No So swap pivot and left. 2 1 3 5 4 6 2 1 3 5 4 6
  • 52.
    Quick Sort Example Solvedpivot = 5 New Array will be right = 6 0 1 2 3 4 5 pivot Left right(last element of array) Is pivot < right? yes So No need to swap pivot and right pivot = 5 Just increment the index. right = 3 pivot left right Again Check Is pivot < right? No So swap pivot and left. 2 1 3 6 4 5 2 1 3 6 4 5
  • 53.
    Quick Sort Example Solvedpivot = 5 New Array will be left = 3 0 1 2 3 4 5 pivot Left right(last element of array) Is pivot > left? yes So No need to swap pivot and right pivot = 5 Just increment the index. left = 1 pivot left right Again Check Is pivot > left? yes So no need to swap pivot and left. 2 1 5 6 4 3 2 1 5 6 4 3
  • 54.
    Quick Sort Example Solvedpivot = 5 New Array will be left = 1 0 1 2 3 4 5 pivot Left right(last element of array) Is pivot > left? yes So No need to swap pivot and right pivot = 5 Just increment the index. left = 1 pivot Left sub array left right Now both left and right at the same position so 5 is piovor at the sorted position 2 1 5 6 4 3 2 1 5 6 4 3
  • 55.
    Quick Sort Example Solved pivot Leftsub array left right So pivot has divided the array into two halves. Now we will quick sort the left sub array. pivot = 4 right = 1 piv Left Right Is Pivot < right? No So swap the pivot and right elements. 2 1 5 6 4 3 2 1 5 6 4 3
  • 56.
    Quick Sort Example Solved pivot= 4 pivot left = 1 Left Right Pivot = 4 Left Right Pivot Left = 2 Again Check Is Pivot > Left? Yes No Need to Swap So just Increment. 2 4 5 6 1 3 2 4 5 6 1 3

Editor's Notes

  • #4 End of lecture 43. Start of 44
  • #21 Start of lecture 45