Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm...

22
Sorting

Transcript of Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm...

Page 1: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

Sorting

Page 2: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

2

Simple Sorting Algorithm (Recap)

for in range(len(A)) : k = position of min. element between A [i] and A [N-1] Swap A [i] and A [k]

A[0] A[i] A[i+1] A[N-1]

Courtesy Prof P R Panda CSE, IIT Dellhi

Selection Sort

Page 3: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

3

Simple Sorting Algorithm (Recap)

for j in range(i+1, len(A)): if A[min_index] > A[j]: min_index = j

t = A [ i ] A [ i ] = A [ k ] A [ k ] = t

A[0] A[i] A[i+1] A[N-1]

Courtesy Prof P R Panda CSE, IIT Dellhi

for in range(len(A)) : k = position of min. element between A [i] and A [N-1] Swap A [i] and A [k]

Selection Sort

Page 4: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

4

Simple Sorting Algorithm (Recap)

A[0] A[i] A[i+1] A[N-1]

Courtesy Prof P R Panda CSE, IIT Dellhi

Find Min for first time n elements: n-1 coparisons Next time : n-2 . . up to 1 Total time = (n-1)+(n-2)+….+1=(n*(n-1))/2 O(n2)

Selection Sort

Page 5: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

5

Merge Sort Divide and conquer

Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/

Page 6: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

6

Merge Sort Divide and conquer

Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/

Page 7: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

7

Merge Sort Divide and conquer

Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/

Page 8: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

8

Merge Sort Divide and conquer

Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/

Page 9: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

9

Merge Sort Divide and conquer

Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/

Page 10: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

10

Merge Sort Divide and conquer

Page 11: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

11

Merge Sort Divide and conquer

Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/

Page 12: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

12

Merge Sort Running Time (Time Complexity as O) Recurrence Relation: T(1)=1 if n=1 T(n) = 2T(n/2) + cn Solution O(nlog2n)

Page 13: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

13

Quick Sort Based on partitioing in two parts such that first part is less than equal to x and right part is greater than x. If x is an element of the array then it gets located at the right place if the seuqence is sorted.

A[0] A[N-1]

x <=x >x pivot

Page 14: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

14

Quick Sort

Page 15: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

15

Quick Sort

Page 16: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

16

Quick Sort Choice of pivot decides the performance of the algorithm. If the partitioning happens in two almost equal parts, it is an ideal case. Time Complexity Best Case: T(1)=1 T(n)=2T(n/2)+cn where cn is the partining time Complexity O(nlog2n)

Page 17: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

17

Quick Sort Time Complexity Worst Case: T(1)=1 T(n)=T(n-1)+T(1)+ cn where cn is the partining time Complexity O(n2) Average Case: O(nlog2n)

Page 18: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

18

Insertion Sort Basic idea is to insert the current element at the right place. This may require shifting the elements

Page 19: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

19

Insertion Sort

https://www.geeksforgeeks.org/insertion-sort/

Page 20: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

20

Insertion Sort

Time Complexity Worst Case: O(n2) when elements are sorted in the reverse order Best Case: O(n) when elements are already sorted

Page 21: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

21

Bubble Sort

https://codingcompiler.com/bubble-sort-program-in-c-using-function/

Page 22: Sorting - ERNETpkalra/col100/slides/12-Sorting.pdf · 2019-09-24 · 4 Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] Courtesy Prof P R Panda CSE, IIT Dellhi Find Min for

22

Bubble Sort

Time Complexity: O(n2)