1 / 25

Foundations of Data Structures

Foundations of Data Structures. Practical Session #12 Linear Sorting. Sorting algorithms criteria. Running time (worst/average/best cases). Additional memory required. “In place” algorithms require additional memory.

zagiri
Download Presentation

Foundations of Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Foundations of Data Structures Practical Session #12 Linear Sorting

  2. Sorting algorithms criteria • Running time (worst/average/best cases). • Additional memory required. • “In place” algorithms require additional memory. • Stability- stable sorting algorithms maintain the relative order of records with equal keys.

  3. Comparison sorting • A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (i.e., ) that determines which of two elements should occur first in the final sorted list. • Simple algorithms. For example: insertion sort, selection sort, bubble sort - . • Recursive algorithms. For example: merge sort, quicksort - . • A comparison sort must have a lower bound of comparison operations. This is a consequence of the limited information available through comparisons alone.

  4. Linear sorting • Linear sorting is the problem of sorting a collection of items with numerickeys. • The ability to perform arithmetic operations on the keys allows faster algorithms thancomparison-based algorithms in many cases. • Classical examples include: • Counting sort • Radix sort • Bucket sort

  5. Counting sort • A sort algorithm that is not based on comparisons and supports duplicate keys. • is an input array of length . • is the output array. • is an auxiliary array of size . • Assumption: consists of elements with integer keys in the range .

  6. Counting sort cont’d Counting-Sort (A, B, k) Example for i ← 1 to k C[i] ← 0 // Calc histogram for j ← 1 to n C[A[j]] ← C[A[j]] + 1 // Calc start index (backwards) in output for each key for i ← 2 to k C[i] ← C[i] + C[i-1] // Copy to output array for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 return B

  7. Counting sort analysis • Run time complexity: .This is an improvement over comparison-based sorts, which require time. • Counting sort is stable-two elements with the same key value will appear in the output in the same order as they appeared in the input. Stability is important when there is additional data besides the key. • Additional space required:

  8. Radix sort • A stable sort algorithm for sorting elements with digits, where the digits are in base , i.e., in range . • The algorithm uses a stable sort algorithm to sort the keys by each digit, starting with the least significant digit (the rightmost). • Radix-Sort(A[1..n], d) • for i ← 1 to d Use a stable sort to sort A • according to digit i

  9. Radix sort analysis • For example, sort 7 numbers with 3 digits in decimal base. Input array Sorted by 1st digit Sorted by 2nd (and 1st ) digit Sorted!

  10. Radix sort cont’d • Assuming the stable sort runs in (such as counting sort) the running time is . • If is constant and , the running time is O(n).

  11. Bucket sort • Assumption: the elements of the input array are uniformly distributed over the interval *. • * Can be extended to keys in any range • The idea • Divide the interval into equal-sized subintervals (buckets). • Distribute the input numbers between the buckets. • To produce the output, sort the elements in each bucket and then output the concatenation of the buckets.

  12. Bucket sort cont’d Bucket-sort (A) n ← length(A) for i ← 1 to n do for i ← 1 to n do sort B[i] using insertion sort return B[0]^B[1]^… B[n-1] Example

  13. Bucket sort analysis • Assuming the input is uniformly distributed over , the expected size of each bucket is • Thus, sorting each bucket requires expected time. • Distributing elements between buckets is and concatenating the buckets (lists) is Total expected run time is .

  14. Sort algorithms review

  15. Question 1 • Describe an algorithm, that given integers in the range , performs a preprocessing in time to answer in time “how many of the numbers are in the range ?” for some parameters?

  16. Question 1 solution • Counting-sort gets an array of integers in the range and performs the following lines: • for (j = 0; j < length[A]-1; j++) • C[A[j]] = C[A[j]]+1; • for(i = 1; i <= k; i++) • C[i] = C[i] + C[i-1]; • After which C[i] stores the number of items smaller or equal to (requires time). • Given a range to answer the query simply return C[b] – C[a] in time as required.

  17. Question 2 • Design an algorithm for sorting elements with keys in the range that runs in time if the items are uniformly distributed over , and in in the worst distribution case. • Hint: what’s the worst case scenario of bucket sort, and why it results in running time?

  18. Question 2 solution • Use bucket sort over the range with the following changes: • The elements in each bucket are stored in an AVL tree (instead of a linked list). • In the last stage, concatenate all the inorder visits of all the buckets one after another.

  19. Question 2 solution cont’d • Let be the number of elements in bucket (an AVL tree). • Inserting the elements into the buckets takes • When the keys are uniformly distributed, for every, resulting in running time. • In the worst distribution case there’s an s.t. , therefore • Inorder traversals of all the buckets takes time, and its concatenation takes as well. • In total: time for uniformly distributed keys and in the worst distribution case.

  20. Question 2 solution cont’d • Alternative solution • Execute in parallel the following two algorithms: • Original bucket sort. • Any sort algorithm that takes . • Stop when either of the algorithms has stopped (the quickest) and return its output.

  21. Question 3 • Given a set of integers in the range , suggest an efficient sorting algorithm. • Naïve attempts • Comparison-based algorithm take . • Counting-sort: . • Bucket sort: uniform distribution isn’t given. • Radix sort: • Can we do any better?

  22. Question 3 solution • Use radix-sort after preprocessing: • Convert all the numbers to base in total time: Each input number is converted to where • Call radix-sort on the transformed numbers . • All the numbers are in range 1 to , therefore, there are at most 4 digits for each number( in base is ). • The running time of the suggested algorithm is .

  23. Question 4 • There are sets . • Each set contains integers in the range and • is the number of elements in . • Suggest an algorithm for sorting all the sets in time complexity and space (memory) complexity. • Note: The output is sorted sets and nota single merged sorted set.

  24. Question 4 solution • Solution: • If we sort each set in separate using an algorithm (such as merge-sort) we get: • If we sort each set in separate using counting sort: Furthermore, the space complexity is .

  25. Question 4 solution cont’d • The following algorithm runs in time and space complexity: • Add a field set-numto each element – . • Build an array of all the elements in all of the sets –. • Sort using counting-sort –. • Split back into the original sets according to the set-numfield –

More Related