COMP 250 Lecture Notes - Lecture 6: Sorting Algorithm, Heapsort, Merge Sort
Document Summary
Lecture 6 - sept 19 sorting algorithms: bubble, selection, & insertion. We are talking about a list of elements. Any time you compare things, you can talk about a sorting algorithm. Another example -- you can sort through exams by last name. N * log(n) is waaaay less than n. As we walk through the list, if we see that an element and its neighbor are in the wrong order, then we swap them (put them in the right order). To do this, you need to swap elements. Remember that if you try to swap two things, you can"t do: As soon as you say x=y, you lost the previous value of x. Rather, you need to use a temporary variable : The first time you go through, if the smallest element is in the back, at some point it will be swapped with the 2nd to last element in the list.