0% found this document useful (0 votes)
14 views

Sorting

The document discusses various sorting algorithms like bubble sort, selection sort, insertion sort, merge sort, and quick sort. It provides details on how each algorithm works and analyzes their time complexities, particularly in the worst case. Merge sort is explained in depth through examples of merging two sorted arrays in-place using indices to build the final sorted array. The process involves comparing elements and shifting/swapping elements to maintain sorted order as the arrays are merged.

Uploaded by

dhanushsaikode
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Sorting

The document discusses various sorting algorithms like bubble sort, selection sort, insertion sort, merge sort, and quick sort. It provides details on how each algorithm works and analyzes their time complexities, particularly in the worst case. Merge sort is explained in depth through examples of merging two sorted arrays in-place using indices to build the final sorted array. The process involves comparing elements and shifting/swapping elements to maintain sorted order as the arrays are merged.

Uploaded by

dhanushsaikode
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 96

#LifeKoKaroLift

Sorting

1
Sorting Algorithms

● Different sorting algorithms:


○ Bubble Sort
○ Selection Sort
○ Insertion Sort
○ Merge Sort
○ Quick Sort
○ Heap Sort

2
Bubble Sort

Bubble Sort
Successively bubbles up largest elements
one by one, to the end of the array

Sorts the array in “at most” n-1 passes

How to find complexity? (Worst case)


Selection Sort

Selection Sort
Selects minimum element from the
remaining array and places it in the
beginning of the array

Sorts the array in “at most” n-1 passes

How to find complexity? (Worst case)


Insertion Sort

Insertion Sort
Inserts every element at its right place in a
sorted array.

Sorts the array in n-1 passes

How to find complexity? (Worst case)


Merge Sort

Merge Sort

Sorts an array by dividing it into


two halves RECURSIVELY and
then merging the sorted array
back into a sorted array.

If T(N) = time to sort


array of size “N”
T(N) = 2xT(N/2) + cN Merging
Takes time
proportional to
“N”
Merge Sort

T(N) = 2.T(N/2) + cN

T(N) = 2.[2T(N/4)+cN/2] + cN
= 4.T(N/4) + 2.cN/2
+ cN
= 4.T(N/4) + 2.cN
= 22.T(N/22) + 2.cN
Now, 4.T(N/4) + 2.cN can be
further solved to obtain
= 8.T(N/8) + 3.cN
= 23.T(N/23) + 3.cN
Say T(N) = 2k.T(N/2k) + k.cN
(if mergeSort goes for k
recursions)
Merge Sort

Say T(N) = 2k.T(N/2k) + k.cN


(if mergeSort goes for k
recursions)

Now T(1) = constant, say d

So Recursions will stop


When N/2k = 1
Or N = 2k
Or k = log2 N
T(N) = N.T(1) + (log2 N)cN
T(N) = O(N) + O(Nlog2 N)
T(N) = O(Nlog2 N)
Merge Sort

0 1 2 3 4 5 6 7 8

-1 4 7 2 1 5 3 0 9
Merge Sort

5 6 7 8
0 1 2 3 4
5 3 0 9
-1 4 7 2 1
Merge Sort

5 6 7 8
2 3 4
0 1
5 3 0 9
7 2 1
-1 4
Merge Sort

5 6 7 8
2 3 4

0
1 5 3 0 9
7 2 1
4
-1
Merge Sort

5 6 7 8
2 3 4

0 1
5 3 0 9
7 2 1
-1 4
Merge Sort

5 6 7 8
3 4

0 1 2
5 3 0 9
2 1
-1 4 7
Merge Sort

5 6 7 8

0 1 2 3 4
5 3 0 9
2 1
-1 4 7
Merge Sort

5 6 7 8

0 1 2 4
3
5 3 0 9
1
-1 4 7 2
Merge Sort

5 6 7 8

0 1 2 3 4
5 3 0 9
-1 4 7 2 1
Merge Sort

5 6 7 8

0 1 2 3 4
5 3 0 9
-1 4 7 1 2
Merge Sort

5 6 7 8

0 1 2 3 4
5 3 0 9
-1 1 2 4 7
Merge Sort

5 6 7 8

0 1 2 3 4
5 3 0 9
-1 1 2 4 7

Now do the breaking and


merging on this side
Merge Sort

0 1 2 3 4 5 6 7 8

-1 1 2 4 7 0 3 5 9
Merge Sort

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 5 7 9
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 2 4 7 0 1 3 5 9

i j

-1 0 1 2 3 4 5 7 9

k
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 4 7 1 3 5 9

i mid j
IN-PLACE = Without using a third
array (and only working on indices at
different positions in the array)
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 4 7 1 3 5 9

First half Second half


i mid j
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 4 7 1 3 5 9

i mid j

-1 < 1 ⇒ i++
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 4 7 1 3 5 9

i mid j
Result array
0 < 1 ⇒ i++
Note how first sub-array shrinks when something from first sub-array
contributes to the building of resultant array
WHATEVER is BEHIND (to the left of) “i” becomes the result array
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 4 7 1 3 5 9

i mid j
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 4 7 1 3 5 9

i mid j

2,1 caused an inversion 2>1


- Temp = 1
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 4 7 1 3 5 9

i mid j 2>1
- Temp = 1

(2,1) caused an inversion


All elements to the right of 2 in first sub-array are greater than 2
So elements from i to mid are going to cause inversion with 1 as well…
So there will be total of mid-i (number of elements from i to mid)
inversions
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 2 2 4 7 3 5 9

i mid j

2>1
- Temp = 1
- Shift 7, 4, 2 to the
right
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 4 7 3 5 9

i mid j

2>1
- Temp = 1
- Shift 7, 4, 2 to the
right
- Arr[i] = temp
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 4 7 3 5 9

i mid j
2>1
- I++
- Mid++ (something from second half was added to
result. . so an element of second half is deleted and
moved and the boundary of first half is shifted right)
- J++
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 4 7 3 5 9

i mid j

Note how second sub-array shrinks and first sub-array moves to the
right when something from second sub array contributes towards
building the resulting sub-array
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 4 7 3 5 9

i mid j

2 < 3 ⇒ i++
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 4 7 3 5 9

i mid j
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 4 7 3 5 9

i mid j

4>3
- Temp = 3
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 4 4 7 5 9

i mid j

4>3
- Temp = 3
- Shift 7, 4 to the right
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 7 5 9

i mid j

4>3
- Temp = 3
- Shift 7, 4 to the right
- Arr[i] = temp
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 7 5 9

i mid j

4>3
- i++
- mid++
- j++
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 7 5 9

i mid j

4>3
- i++
- mid++
- j++
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 7 5 9

i mid j

4<5
- i++
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 7 5 9

mid j

i
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 7 5 9

mid j
7>5
- Temp = 5
i
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 7 7 9

mid j
7>5
- Temp = 5
- Shift 7 to right i
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 5 7 9

mid j
7>5
- Temp = 5
- Shift 7 to right i
- Arr[i] = temp
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 5 7 9

mid j
7>5
- I++
- Mid++ i
- J++
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 5 7 9

mid j
i<=mid

i
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 5 7 9

mid j
7 < 9 ⇒ i++
and
i becomes greater i
than mid
Merging the arrays (in sorted orders)

0 1 2 3 4 5 6 7 8

-1 0 1 2 3 4 5 7 9

Array Sorted !
Quick Sort

● Quick Sort
○ Select a pivot
■ First element
■ Last element
■ Any random element
■ Select middle element as the pivot
○ Place pivot at its proper place in the array
■ Elements less than pivot are on one side
■ Elements greater than pivot are on the other side
of pivot
○ Perform quickSort on the two halves
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 7 2 12 15 13 0 6

pointer pivot
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 7 2 12 15 13 0 6

pointer pivot
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 7 2 12 15 13 0 6

pointer pivot
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 7 2 12 15 13 0 6

pointer pivot
7 is greater than 6, swap the
elements as well as the pivot and
pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 6 2 12 15 13 0 7

pivot pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 6 2 12 15 13 0 7

pivot pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 6 2 12 15 13 0 7

pivot pointer
0 is less than 6
=> Swap the elements as well as
the pivot and pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 12 15 13 6 7

pointer pivot
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 12 15 13 6 7

pointer pivot
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 12 15 13 6 7

pointer pivot
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 6 15 13 12 7

pivot pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 6 15 13 12 7

pivot pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 6 15 13 12 7

pivot pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 6 15 13 12 7

pivot

pointer
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 6 15 13 12 7

Pivot is at its correct position All elements less than pivot


are to its left.
pivot
All elements more than pivot are
to its right.
Quick Sort

0 1 2 3 4 5 6 7 8

-1 4 0 2 6 15 13 12 7

Do the same thing here


Do the same thing here THEN
Quick Sort
Heap Sort
Heap Sort

1 9 7 0 4 5 3 2 -1

In-place sort = sorting the arrray without using extra space (like an
extra array) . . .or sorting that uses O(1) space apart from O(n) which
is used store the array which is to be sort

Stable sort = If unsorted array has two equal entries A and B (such
that A occurs before B, then A will occur before B in the sorted array
also. . . such a sorting is called Stable Sort. . .
Heap Sort

1 9 7 0 4 5 3 2 -1

Calculate N = 9 (Length of the current heap) (Light yellow box)


Heap Sort

1 9 7 0 4 5 3 2 -1

maxHeapify Method: maxHeapify(arr, N, currentIndex)

Arr = the array containing elements [Root is at 0]


N = the length of current heap
currentIndex = where maxHeapification starts
Heap Sort

0 1 2 3 4 5 6 7 8

1 9 7 0 4 5 3 2 -1

N=9
Now start from parent of N/2-1 = 3
last node = (N/2-1) and Call maxHeapify() as
move up to the root follows
WHILE maxHeapifying
the tree at every node. . . maxHeapify(arr,N,3)
maxHeapify(arr,N,2)
maxHeapify(arr,N,1)
maxHeapify(arr,N,0)
Heap Sort

indexOfLargest

maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 0 4 5 3 2 -1

Assume the currentIndex to be the


index of Largest element
Heap Sort

indexOfLargest rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 0 4 5 3 2 -1

Find the INDICES OF left and right


children of currentIndex Note that root is at
leftChildIndex = 2*currentIndex +1 0
rightChildIndex = 2*currentIndex +2 And not 1
Heap Sort

indexOfLargest rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 0 4 5 3 2 -1

If the index of left child is not out of the current


heap (less than N)
And element at leftChildIndex is more than Note that root is at
element at indexOfLargest 0
Then leftChildIndex becomes indexOfLargest And not 1
Heap Sort
indexOfLargest
currentIndex rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 0 4 5 3 2 -1

If the index of left child is not out of the current


heap (less than N)
And element at leftChildIndex is more than Note that root is at
element at indexOfLargest 0
Then leftChildIndex becomes indexOfLargest And not 1
Heap Sort
indexOfLargest
currentIndex rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 0 4 5 3 2 -1

If the index of right child is not out of the current


heap (less than N)
And element at rightChildIndex is more than Note that root is at
element at indexOfLargest 0
Then rightChildIndex becomes indexOfLargest And not 1
Which is not the case
here
Heap Sort
indexOfLargest
currentIndex rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 0 4 5 3 2 -1

If indexOfLargest is not CurrentIndex


Then swap currentIndex element with the index
of largest element Note that root is at
0
And not 1
Heap Sort
indexOfLargest
currentIndex rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 2 4 5 3 0 -1

If indexOfLargest is not CurrentIndex


Then swap currentIndex element with the index
of largest element Note that root is at
0
And not 1
Heap Sort
indexOfLargest
currentIndex rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 2 4 5 3 0 -1

We have sent zero down the heap. . which may violate heap property with its own
children. .
[Note: IndexOfLargest will be at a position from where the largest element was
picked for swapping,i.e, Left child or right child. If the swapping happened at all]
Therefore, call maxHeapify(arr, N=9, currentIndex = indexOfLargest)
Heap Sort
indexOfLargest
currentIndex rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)

0 1 2 3 4 5 6 7 8

1 9 7 2 4 5 3 0 -1

We have sent zero down the heap. . which may violate heap property with its own
children. .
Also note that if swapping happened, then indexOfLargest will not be at
currentIndex, it will be either at left child or at right child of the current index
(or indexOfLargest will be down the tree)
Heap Sort

Array Has been heapified


0 1 2 3 4 5 6 7 8

9 4 7 2 1 5 3 0 -1
Heap Sort

Now sorting the array

0 1 2 3 4 5 6 7 8

9 4 7 2 1 5 3 0 -1

Swap the first node with the last node


Assume last node to be out of the heap
. . so size of heap is N-1 (or 9-1 = 8)
Heap Sort

Now sorting the array

0 1 2 3 4 5 6 7 8

-1 4 7 2 1 5 3 0 9

Swap the first node with the last node


Assume last node to be out of the heap
. . so size of heap is N-1 (or 9-1 = 8)
Heap Sort

Now sorting the array

0 1 2 3 4 5 6 7 8

-1 4 7 2 1 5 3 0 9

Do maxHeapify on array taking heap


size as 8 and starting from 0
Basically we are going to heapifyDown
from the root node
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

-1 4 7 2 1 5 3 0 9

Current largest index = index 0


Find largest. . .which will be index 2. . .
Swap 7 with -1
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

7 4 -1 2 1 5 3 0 9

Now call maxHeapify on Index 2 (or the index with which the swap
happened)
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

7 4 -1 2 1 5 3 0 9

Assume largest to be at index 2. . .


Children of index 2 are at index 5 and index 6
Largest will be at index 5 . so we swap -1 with 5.
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

7 4 -1 5 1 -1 3 0 9

Assume largest to be at index 2. . .


Children of index 2 are at index 5 and index 6
Largest will be at index 5 . so we swap -1 with 5.
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

7 4 -1 5 1 -1 3 0 9

Now do the mixheapify on index 5


Left child index of index 5 is 11 [outside the range of current heap]
Right child is also outside the range of current Heap (that is 8)
Therefore no more swapping is involved
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

7 4 -1 5 1 -1 3 0 9

After the remaining array has been max heapified. . .


Swap first node with the last node
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

7 4 -1 5 1 -1 3 0 9

After the remaining array has been max heapified. . .i value will again
decrease and the length of heap will decrease by 1)
Heap Sort

maxHeapify(arr, N=8, currentIndex = 0)

0 1 2 3 4 5 6 7 8

0 4 -1 5 1 -1 3 7 9

Swap last node with the first node

Again do the max heapify on the array of size 7


Thanks for
Listening!

You might also like