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

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
Image of page 1

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
Image of page 2
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
Image of page 3

Subscribe to view the full document.

Quick, Merge, or Heap? What is your personal pick? What factors should we consider? 4 ECE368: Data Structures
Image of page 4
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
Image of page 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
Image of page 6
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern