Notes 7 - Week 7: Sorting This section includes 3 chapters....

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Week 7: Sorting This section includes 3 chapters. Chapter 11 reviews the simpler sorting methods excluding the bubble sort since it has already been covered by most students. These sorting methods (selection, insertion, recursive insertion, and Shell) are not especially efficient. Chapter 12 reviews efficient sorting methods: merge, quick, and radix and chapter 13 considers at sorted lists. All sorting methods can be either in ascending [default] order or descending order. The selection sort can search either for minimums or maximums and then swaps this value at its appropriate place on the list. (see page 256) For example: to sort 10 14 12 -11 13 using maximums first find the max of all x 0 to x n-1 , where n = 5 in this case. This is the value 14. Swap 14 at the location for the largest i=4, that has the value 13 to yield 10 13 12 -11 14 Now that 14 is in the correct location find the largest of x[0] to x[3] which is 13 and put this at location for the second largest i=3, that has the value -11 to yield 10 -11 12 13 14 Now find the maximum of x[0] to x[2] which is 12 already in the correct position so do nothing. The last iteration finds the max of x[0] and x[1] which is 10 and swaps to the correct position. to yield -11 10 12 13 14 the final result. The algorithm for this is for i = n-1 downto 1 find the maximum of x[0] to x[i] swap x[i] with the maximum end Finding the maximum requires a loop so this has a O(n 2 ) Task 1: Try sorting using minimums on these values: 10 14 13 -11 12 The insertion sort is similar to sorting a hand of playing cards one at a time. (see page 262) To use insertion sort on the values: 4 2 0 5 1 6 start by leaving the first value since a single valued list is already sorted. Then consider the second value 2 is not ordered with the 4. Beginning at the predecessor compare and shift until a predecessor is not larger, then store the 2. This yields: 2 4 followed by the rest of the list unchanged.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Now examine the third value 0 shifting its predecessors until it is less or we reach the beginning of the list. This will shift both the 2 and 4 so the 0 is stored at the beginning of the list. This yields: 0 2 4 followed by the rest of the list unchanged Now the fourth value 5 is compared but the very first predecessor is smaller so do nothing to yield: 0 2 4 5 followed by the rest of the list 1 6 Examining the next value 1 will shift predecessors down to the 2 and leaves the rest of the lower part of the list unchanged. This yields: 0 1 2 4 5 followed by the 6. No shifting required for this value so the algorithm ends. The insertion sort algorithm is:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 111C taught by Professor Metzler during the Spring '11 term at City College of San Francisco.

Page1 / 8

Notes 7 - Week 7: Sorting This section includes 3 chapters....

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online