17-counting.pdf

# 17-counting.pdf - Felix Xiaozhu Lin Lecture 17 Linear Time...

• 27

This preview shows page 1 - 6 out of 27 pages.

Felix Xiaozhu Lin Lecture 17: Linear Time Sorting - Counting Sort Slides Courtesy: Prof. Vijay Raghunathan

Subscribe to view the full document.

Merge Sort: merge algorithm 2 // assume auxiliary space tmp[] Function merge (r[], lb, mid, ub): memcpy(&tmp[lb], &r[lb], mid-lb+1); memcpy(&tmp[mid+1], &r[mid+1], ub-mid); i ¬ lb; j ¬ mid+1; for k ¬ lb to ub if (i > mid) r[k] ¬ tmp[j++]; else if (j > ub) r[k] ¬ tmp[i++]; else if (tmp[j] < tmp[i]) r[k] ¬ tmp[j++]; else r[k] ¬ tmp[i++]; ECE368: Data Structures 1 3 5 5 2 3 4 6 tmp[0, 3] tmp[4, 7] r[0, 7] 1 2 3 3 4 5 5 6 i j
Merge Sort: take-away points A common choice for external sorting Memory complexity: O(n) , not in-place Time complexity (worst case): O(nlog n) 3 ECE368: Data Structures

Subscribe to view the full document.

Quick, Merge, or Heap? What is your personal pick? What factors should we consider? 4 ECE368: Data Structures
Sorting in linear time Counting sort: No comparisons between elements Input: A[1..n] , where A[j] {1, 2, …, k} Output: B[1..n] , in sorted order Auxiliary storage: C[1..k] Note that the above arrays start at index “1” and not “0” ECE368: Data Structures 5

Subscribe to view the full document.

Counting sort – the idea 1 2 3 4 5 ECE368: Data Structures 6 A : 4 1 3 4 3 B : 1 3 3 4
You've reached the end of this preview.
• Fall '08
• Staff

{[ 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