CC2203-Les06Sort-for-viewing

# CC2203-Les06Sort-for-viewing - 6-1/40Lesson 6:...

This preview shows pages 1–15. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 6-1/40Lesson 6: SortinglAfter completing this lesson, you should be able to use the following:–Insertion sort–Bubble sort–Selection sort–Merge sort–Quick sort6-2/40SortinglRearrange a[0], a[1], …, a[n-1] into ascending order. When done, a[0] <= a[1] <= … <= a[n-1]l8, 6, 9, 4, 3 => 3, 4, 6, 8, 96-3/40Sort MethodslInsertion SortlBubble SortlSelection SortlHeap Sort (Covered later in lesson 13)lMerge SortlQuick Sort6-4/40Insert An ElementlGiven a sorted list/sequence, insert a new elementlGiven 3, 6, 9, 14lInsert 5lResult 3, 5, 6, 9, 146-5/40Insert an Elementl3, 6, 9, 14 insert 5lCompare new element (5) and last one (14)lShift 14 right to get 3, 6, 9, , 14lShift 9 right to get 3, 6, , 9, 14lShift 6 right to get 3, , 6, 9, 14lInsert 5 to get 3, 5, 6, 9, 146-6/40Insert An Element// insert t into a[0:i-1]int j;for (j = i; j > 0 && t < a[j-1]; j--)a[j] = a[j-1];a[j] = t;demo: javac applications/Insert.javajava applications.Insert6-7/40Insertion SortlStart with a sequence of size 1lRepeatedly insert remaining elements6-8/40Insertion SortlSort 7, 3, 5, 6, 1lStart with 7 and insert 3 => 3, 7lInsert 5 => 3, 5, 7lInsert 6 => 3, 5, 6, 7lInsert 1 => 1, 3, 5, 6, 76-9/40Insertion Sortfor (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1]// code to insert comes here}6-10/40Insertion Sortfor (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1]int t = a[i];int j;for (j = i; j > 0 && t < a[j-1]; j--)a[j] = a[j-1];a[j] = t;}demo: javac applications/InsertionSort1.javajava applications.InsertionSort16-11/40Bubble Sort1.In an array aof size n, if a[i-1] > a[i], swap a[i-1] with a[i] for i=1, 2, 3, …n-1. This is called a Bubbling Pass2.After each bubbling pass, the largest element moves to the right-most position3.Then, perform bubbling pass for the remaining n-1 elements, a[0] …a[n-2], and obtain the largest at a[n-2]4.And so on until performing bubbling pass for the remaining 2 elements and obtaining the largest at a[1]6-12/40Bubble SortlSort 6, 5, 8, 4, 3, 1l6 > 5, swap 6 and 5 => 5,6, 8, 4, 3, 1l6 < 8, no swap => 5, 6, 8, 4, 3, 1l8 > 4, swap 8 and 4 => 5, 6, 4, 8, 3, 1l8 > 3, swap 8 and 3 => 5, 6, 4, 3, 8, 1l8 > 1, swap 8 and 1 => 5, 6, 4, 3, 1, 8lAfter the 1stpass, the largest element 8 moves to the right-most positionFirst Bubbling Pass6-13/40Bubble SortlSort the remaining 5, 6, 4, 3, 1l5 < 6, no swap => 5, 6, 4, 3, 1l6 > 4, swap 6 and 4 => 5, 4, 6, 3, 1l6 > 3, swap 6 and 3 => 5, 4, 3, 6, 1l6 > 1, swap 6 and 1 => 5, 4, 3, 1, 6lAfter the 2ndpass, the largest element 6 moves to the right-most positionSecond Bubbling Pass6-14/40Bubble SortlAfter the 3rdpass, the largest element 5 moves to the right-most position, and the result is: 4, 3, 1, 5lAfter the 4thpass, the result is: 3, 1, 4lAfter the 5thpass, the result is: 1, 3...
View Full Document

## This note was uploaded on 08/18/2011 for the course COMP 3868 taught by Professor Keithchan during the Summer '97 term at Hong Kong Polytechnic University.

### Page1 / 40

CC2203-Les06Sort-for-viewing - 6-1/40Lesson 6:...

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

View Full Document
Ask a homework question - tutors are online