CSE 310-Lecture5

CSE 310-Lecture5 - C M L Click to edit Master subtitle...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C M L Click to edit Master subtitle style 10/22/10 C M L Summer 2010 CSE 310 Algorithms and Data Ke Bai Research Assistant Compiler and Microarchitecture Lab Computer Science and Engineering Arizona State University Slides courtesy: Baoxin Li, Hairong Zhao and Aviral Shrivastava C M L 10/22/10 C M L Announcements • Assignment 3 – Due Today, June 28, 2010 • Midterm – Wednesday, June 30, 2010, 7:00 pm – 9:30 pm, BYAC 190 – Cover Induction, Recurrence, Asymptotic Notation, and Sorting Algorithm – Open book, open notes and no internet C M L 10/22/10 C M L CSE 310 Roadmap • Induction and Recursion • Asymptotic Notation • Recurrence Relations and Timing Complexity • Sorting Algorithms C M L 10/22/10 C M L Sorting • Input: – Array of n numbers, a[0] … a[n-1] • Output – Array of the same n numbers, such that • For each i=0 to n-2, a[i] <= a[i+1] C M L 10/22/10 C M L Some Attempts • Random sort – If array not sorted • Pick any two elements at random, and swap them to put them in the right order • Perm sort – If array not sorted • Permute the elements C M L 10/22/10 C M L Sorting Algorithms • http://www.cs.ubc.ca/~harrison/Java C M L 10/22/10 C M L Agenda • Selection Sort • Insertion Sort • Merge Sort • Quick Sort C M L 10/22/10 C M L Loop Invariant: a[0]..a[i] is sorted, a[i+1]..a[n] is not sorted Initial condition: i=-1 Final condition i=n-1 for (i=-1; i<n-1; i++) { let k = index of min element in (a[i+1], …., a[n]); swap (i+1, k); } Time Complexity: T(n) = O(n) + O(1) + T(n-1) T(n) = O( n2 ) Selection Sort C M L 10/22/10 C M L Selection Sort Example 35 65 30 60 20 scan 0-4, smallest 20 swap 35 and 20 20 65 30 60 35 scan 1-4, smallest 30 swap 65 and 30 20 30 65 60 35 scan 2-4, smallest 35 swap 65 and 35 20 30 35 60 65 scan 3-4, smallest 60 swap 60 and 60 20 30 35 60 65 done for (i=-1; i<n-1; i++){ let k = index of min element in (a[i+1], …., a[n]); swap (i+1, k); } C M L 10/22/10 C M L Insertion Sort • Based on the technique used by card players to arrange a hand of cards – Player keeps the cards that have been picked up so far in sorted order – When the player picks up a new card, he makes room for the new card and then inserts it in its proper place C M L 10/22/10 C M L /*Pseudocode of Insertion Sort*/ INSERTION-SORT(A) for j = 2 to length(A) do n key = A[j] /* insert A[j] into sorted sequence A[1 … j-1] */ i = j – 1 while i > 0 and A[i] > key do A[i+1] = A[i] i = i – 1 A[i+1] = key Insertion Sort C M L 10/22/10 C M L INSERTION-SORT(A) for j = 2 to length(A) do n key = A[j] /* insert A[j] into sorted sequence A[1 … j-1]*/ i = j – 1 while i > 0 and A[i] > key do A[i+1] = A[i] i = i – 1 A[i+1] = key times cost n c1 n-1 c2 c3=0 n-1 c4 Q nj=2 tj c5 ¡nj= 2 (tj-1) c6 Qnj=2(tj-1) c7 n-1 c8 tj= #of times the while loop test is executed for that value of j....
View Full Document

This note was uploaded on 10/21/2010 for the course CSE 310 taught by Professor Davulcu,h during the Spring '08 term at ASU.

Page1 / 90

CSE 310-Lecture5 - C M L Click to edit Master subtitle...

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

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