sorting simple sorting algorithm recap
play

Sorting Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] - PowerPoint PPT Presentation

Sorting Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] 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 Courtesy Prof P R Panda CSE, IIT Dellhi 2 Simple Sorting


  1. Sorting

  2. Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] 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 Courtesy Prof P R Panda CSE, IIT Dellhi 2

  3. Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] for in range(len(A)) : k = position of min. element for j in range(i+1, len(A)): between A [i] and A [N-1] if A[min_index] > A[j]: Swap A [i] and A [k] min_index = j t = A [ i ] A [ i ] = A [ k ] Selection Sort A [ k ] = t Courtesy Prof P R Panda CSE, IIT Dellhi 3

  4. Simple Sorting Algorithm (Recap) A[0] A[i] A[i+1] A[N-1] 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(n 2 ) Selection Sort Courtesy Prof P R Panda CSE, IIT Dellhi 4

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

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

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

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

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

  10. Merge Sort Divide and conquer 10

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

  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(nlog 2 n) 12

  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 13

  14. Quick Sort 14

  15. Quick Sort 15

  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(nlog 2 n) 16

  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(n 2 ) Average Case: O(nlog 2 n) 17

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

  19. Insertion Sort https://www.geeksforgeeks.org/insertion-sort/ 19

  20. Insertion Sort Time Complexity Worst Case: O(n 2 ) when elements are sorted in the reverse order Best Case: O(n) when elements are already sorted 20

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

  22. Bubble Sort Time Complexity: O(n 2 ) 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend