25
Placement Preparation Sorting and Heaps Varun Raj B.Tech, CSE

Lecture 11.2 : sorting

Embed Size (px)

DESCRIPTION

This slide is part of course designed for placement preparation for students of IIT Guwahati.

Citation preview

  • 1. Placement PreparationSorting and HeapsVarun RajB.Tech, CSE

2. Prerequisites You must know array properly. You must be familiar with pointers and writing basicloops You must have some basic idea of time complexity 3. Aim of Lecture Selection Sort/Bubble Sort Insertion Sort Merge Sort Heaps and Heapsort Quick Sort (If possible) 4. MotivationSorting is any process of arranging items according to value.In computer science, sorting is one of the most extensivelyresearched subjects because of the need to speed up the operationon thousands or millions of records during a search operation 5. The real reason. The real reason we are studying sorting is because beit any coding question in any interview the candidateis expected to know the basic of the sorting andsorting questions are the most expected questions tobe asked 6. Selection SortThe most intuitive sorting algorithm.Works by selecting the smallest element of thearray and placing it at the head of the array.Then the process is repeated for the remainderof the array the next smallest element isselected and put into the next slot, and so ondown the line 7. Demo #1Code of Selection Sort 8. Exercise #1 Write a program that does selection sort on a set ofintegers Some pointers- You will need two loops You will need a variable that stores the currentminimum and then you will have to swap it with thehead. 9. Analysing Complexity In the 1st step the algorithm the for loop runs n-1times. In the second step the for loop runs n-2 times since the1st element is set Hence the time taken is n-1+n-2+n-3..+1=n*(n-1)/2That is O(n^2) The thing to note is that the complexity is O(n^2) forbest, average and worst case. The space complexity is O(1) that is no extra space isused since the algorithm works only on the input array 10. Bubble SortThe bubble sort works by iteratingdown an array to be sorted from thefirst element to the last, comparingeach pair of elements and switchingtheir positions if necessary. Thisprocess is repeated as many times asnecessary, until the array is sorted 11. Demo #2Code For Bubble Sort 12. Exercise #2 Write a program that does bubble sort on a set ofintegers Some pointers- You will need two loops At the end of 1st step the maximum reaches the lastposition .At the end of second step the 2nd maximumreaches the 2nd last position 13. Analysing Complexity for Bubble Sort In the 1st step the algorithm the for loop runs n times. In the second step the for loop runs n-1 times since the1st element is set Hence the time taken is n+n-1+n-2..+1=n*(n+1)/2That is O(n^2) The thing to note is that the complexity is O(n^2) forbest, average and worst case. The space complexity is O(1) that is no extra space isused since the algorithm works only on the input array 14. Some Improvements To Bubble SortOn Close Observation we observe that even in the bestcase Bubble Sort takes O(n^2) time this can be reduced toO(n) that is in a perfectly sorted array bubble sort willonly take O(n) time to terminate(which is already sorted)The idea behind this is that at each outer loop iterationkeep a counter so that if in its inner loop iteration noswapping was done this means that the array is alreadysorted and no further outer loop iterations need to bedone and the array is sorted.So terminate the code 15. Demo #3 This improves only the best case running time of bubble sort theworst case still remains O(n^2) The complexity is O(n^2) for average and worst case. For best case complexity is O(n) that is already sorted array 16. Insertion SortIt inserts each element of the arrayinto its proper position, leavingprogressively larger stretches of thearray sorted 17. DemoCode for insertion sort 18. Analysing Complexity for InsertionSort In the 1st step the algorithm the for loop runs 0 times. In the second step the for loop runs 1 times (worstcase) Hence the time taken is 1+2+3.n-1=n*(n-1)/2That is O(n^2) (worst case) The thing to note is that the complexity is O(n^2) foraverage and worst case. The best case time complexity is O(n). The space complexity is O(1) that is no extra space isused since the algorithm works only on the input array 19. Merge SortMergeSort is a Divide andConquer algorithm. It divides input array intwo halves, calls itself recursively for thetwo halves and then merges the two sortedhalves. 20. Merge Sort-IntiutionThe main concept here isrecursionThe Merge Sort takes anarray from 1 to n and callsitself on the array [1,n/2]and [n/2+1,n] recursivelyboth these subparts aresorted so too sort twocompletely sorted list weonly merge them 21. DemoCode for merge sortIts little complicated so I would like you to practiseproperlySome common errors Base case of recursion Problems with merging especially when one halfcrosses the midpoint (Explained) 22. Analysing Complexity for MergingSort There is some trick involved in finding the time complexity ofMerge Sort . The time complexity is O(nlogn) Here log refers tobase 2. Proof- T(n)=2*T(n/2) +cn where c is any constant The solution to this equation is k*nlogn the proof follows fromsubstitution and something you can look up online or I can do onthe board. The space complexity of this algorithm is O(n) since we are usingan extra array 23. Some remarks about Merge Sort Merge Sort is the fastest algorithm you have seen till now.Itscomplexity is O(n*logn) and for large n it is faster than anyquadratic algorithm for large input Sometimes for small input merge sort may become slower thaninsertion sort due to constant factors.Hence for small input preferinsertion sort The space complexity is O(n) that is an extra array is required thisis the price we had to pay for a faster algorithm. We will see now that Heaps offer us time complexity of O(nlogn)with no extra space required but it has a little sophisticated code. 24. Some Concluding Remarks - We learnt 3 quadratic time sorting algorithms Selection Sort,Insertion Sort, Bubble Sort We learnt to optimise bubble sort for best case The general advice is that whenever using a quadratic timealgorithm use insertion sort. (The reason for this is given as anexercise at the last slide) Merge Sort trumps all the algorithm given large enough input,however for input as small as 10 insertion sort will be faster 25. Extra QuestionsI am not going to discuss the answer to these questions in the class,however you could ask this question on facebook group or personallymessage me.These questions are for personal enhancement , maybe you couldencounter this question in the placement interview/test maybe not Insertion Sort-Prove that the time complexity of insertion sort isO(n+x) where x is the number of inversions in the input array.Hereinversion refers to the pair (ai, aj) with iaj. Merge Sort- The time complexity of Merge Sort is O(nlogn) .Provethat no sorting algorithm in the world that uses comparisionsorting can be asymptotically faster than this