Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix.

Similar presentations


Presentation on theme: "Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix."— Presentation transcript:

1 Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search
CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

2 Motivation Applications of basic data structures
Important algorithmic and data- representation concepts CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

3 Bin Sort (a.k.a. Bucket Sort)
Assumption: The range of possible key values is {0, ..., N-1}, where N is not too much bigger than the number of data elements, n. (It could be smaller.) Example: Sorting a collection of 67 two-digit numbers. N = 100 n = 67 CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

4 Bin Sort: The Algorithm
Create an array Head[0..N-1] of N headers of empty linked lists. Create another array (parallel to the first), Tail[0..N-1] of (initially null) links to the tails of the same lists. For each  key,data  record,  ki, di  insert it onto the end of the list at Head[ki], (which is Tail[ki]). Concatenate the N lists together and return this. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

5 Bin Sort: Example Sort  2, A, 3, X, 2, B, 1,C, 0,D, 1,E 
Head[0] Head[1] Head[2] Head[3] Head[4] Tail[0] Tail[1] Tail[2] Tail[3] Tail[4] CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

6 Bin Sort: Example after 1st record has been inserted
Sort  3, X, 2, B, 1,C, 0,D, 1,E  Head[0] Head[1] Head[2] 2, A < Head[3] Head[4] Tail[0] Tail[1] Tail[2] Tail[3] Tail[4] CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

7 Bin Sort: Example after all records have been inserted
Head[0] 0,D < Head[1] 1,C, 1,E <----- Head[2] 2,A, 2,B <----- Head[3] 3, X < Head[4] Tail[0] Tail[1] Tail[2] Tail[3] Tail[4] CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

8 Bin Sort: Example after all lists have been concatenated
Head[0] 0,D < Head[1] 1,C, 1,E <----- Head[2] 2,A, 2,B <----- Head[3] 3, X < Head[4] Tail[0] Tail[1] Tail[2] Tail[3] Tail[4]  0,D, 1,C, 1,E, 2, A , 2, B, 3, X CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

9 Bin Sort: Comments We really didn’t have to store the full records on the lists; the key information was implicit in which list the record was on. The sorting operations was stable; the relative ordering of records was preserved except when their keys were out of order. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

10 Bin Sort: Time and Space
Creating the arrays and performing the concatenation takes (N). Inserting the data records takes (n). Overall time complexity is (N+n). Space requirements: (N) for Head and Tail arrays, (n) for holding the records. Overall space complexity is (N+n). If we know that in some class of sorting problems N = n or N = c n, then Bin Sort gives us a linear-time, linear space sorting algorithm, which has a lower asymptotic growth rate than the well known n log n time algorithms such as Heapsort. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

11 Can Bin Sort be Adapted for Large N?
What if the range of possible keys is relatively large, but still bounded at some practical value like 10,000,000? Can we still use Bin Sort effectively? Yes, but in a new way. Break each key into a list of subkeys and perform one Bin Sort run for each subkey. This is called “radix sort”... CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

12 Radix Sort Suppose N, the bound on the range of possible key values, is large. For example, N = 10,000,000? We can use each digit of a key as a separate subkey. Consider a key such as 3,870,215 to be a list of 7 separate subkeys: 3,8,7,0,2,1,5 Sort all the records using the last (least signficant ) subkey. Then, using that partially sorted output as input to another sorting step, sort by the second-to-last subkey. Keep doing this until the most significant subkey has been used. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

13 Radix Sort: Example N = 100, n = 5 32, 51, 31, 52, 81 Phase 1: Sort by the rightmost digit (1’s position)  51, 31 , 81, 32, 52 Phase 2: Sort by the next digit (10’s position)  31, 32, 51, 52, 81 CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

14 Radix Sort: Why does it work?
Subkey sorting steps are applied in the order from least significant to most significant. Bin Sort is stable, so each phase preserves the useful work done by the previous phase. The subkeys are independent and cover the entire original key. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

15 Radix Sort: Time and Space
Space requirement: (m+n), where m is the maximum number of possible values of a subkey. Time requirement: (k ( m + n)), where k is the number of phases. If we assume m < n, and that k is ( log N), then the time requirement is ( n log N). However, depending upon the data to be sorted, we might be able to assume that N is constant, making log N also constant, and then have a time complexity of (n). CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

16 Radix Sort: Alternative radices
Our example used radix 10; each subkey was a digit. Writing each key in binary, we can easily use radix 2. A naturally efficient radix for strings of limited length would be 256; each 1-byte character serves as a subkey. (But, lexicographic ordering is not completely consistent with ASCII codes.) CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

17 Radix Sort: Irregular Subkeys
The subkeys for each phase do not have to have the same ranges. To sort playing cards, we can let the less significant subkey be suit, and the more significant subkey be rank. Clubs < Diamonds < Hearts < Spades 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K < A CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

18 Sparse Arrays An array can be viewed as a mapping from a set of index tuples to a set of values. A:  1   2  ...   m  V For example, a 2D array could represent a mapping such as this: A: {0, 1, ..., 63}  {0, 1, ..., 127}  {0, 1, ..., 255} CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

19 Sparse Arrays (Cont.) An m  n array normally requires storage locations. If m = 1000 and n = 1000 and each item requires 8 bytes, then about 8 MB are needed for this. What if 99% of these contain the number 0 ? We could save space by keeping not a full array, but a list of the non-zero entries.   321, 522, 0.1 ,  759, 194, 0.09   CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

20 Sparse Array Example   0, 6, 1 ,  2, 1, 2 ,  3, 3, 9 ,  4, 0, 5 ,  7, 1, 3 ,  7, 6, 1   CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

21 Sparse Arrays (Alternatives)
Alternative representations: Full array (wastes space) Single list (either array-based or linked) List-per-row or list-per-column (linked) The choice will depend upon... Overall size, sparseness, operations to be performed, time requirements. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

22 Stacks (Review) Operations in the Stack ADT:
Push: Stacks  Values  Stacks Pop: Stacks  Stacks  Values isEmpty: Stacks  { true, false } CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

23 Representing a Maze Assume Maze is represented by a 2D binary array.
0 = start 0 = goal CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

24 Stack-Based Maze Solving
Keep track of the path so far with a stack of array positions. At the current position, try to extend the path one step by trying to go right, up, left, or down. If none of the neighboring squares is available, backtrack by popping a position off the stack. (If the stack is empty, declare failure.) When extending the path, push the new position on the stack. Test to see if the new position is the goal. Store 1 in the new position to make it impassable in the future. If the goal has been reached, output the stack contents as the solution path. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -

25 Stack-Based Maze Solving (Comments)
Stack space needed in the worst case: Space for n2 positions, assuming the maze array of size n by n. Time: Each position is visited at most 4 times, once when first arriving, 3 times after backtracking from successor positions. This search technique is known as Depth-First Search (DFS). DFS will find a solution if one exists. DFS will not necessarily find a shortest path. CSE 373, Copyright S. Tanimoto, Bin Sort, Radix Sort, Sparse Arrays, DFS -


Download ppt "Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix."

Similar presentations


Ads by Google