{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CSE 310-Lecture5

# CSE 310-Lecture5 - Summer 2010 CSE 310 Algorithms and Data...

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

C M L Click to edit Master subtitle style 10/22/10 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

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

View Full Document
C M L 10/22/10 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 CSE 310 Roadmap Induction and Recursion Asymptotic Notation Recurrence Relations and Timing Complexity Sorting Algorithms

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

View Full Document
C M L 10/22/10 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] IsSorted(A, n)
C M L 10/22/10 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

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

View Full Document
C M L 10/22/10 Sorting Algorithms http://www.cs.ubc.ca/~harrison/Java
C M L 10/22/10 Agenda Selection Sort Insertion Sort Merge Sort Quick Sort

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

View Full Document
C M L 10/22/10 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 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); }

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

View Full Document
C M L 10/22/10 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 /*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

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

View Full Document
C M L 10/22/10 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 0 c3=0 n-1 c4 q nj=2 tj c5 Qnj= 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. tj -1= # of elements in A[1…j-1] that are > A[j] Insertion Sort
C M L 10/22/10 Running time of an algorithm = sum of running times for each statement executed. T(n) = c1*n + c2*(n-1) +c4*(n-1) +c5 * ° nj=2 tj + c6 * °nj=2 (tj-1) + c7* ° nj=2(tj-1) +c8*(n-1) T(n) depends not just on n, but also the kinds of input arrays Timing Complexity

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

View Full Document
C M L 10/22/10 Array is sorted: In this case, tj = 1 for all j T(n) = c1*n +c2*(n-1) +c4*(n-1) +c5*(n-1) +c8*(n-1) = (c1+c2+c4+c5+c8)*n -(c2+c4+c5+c8) = O(n) a linear function of n.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern