Docsity

Linear Time Sorting - Algorithm - Study Notes, Study notes of Algorithms and Programming

Partial preview of the text

Download Linear Time Sorting - Algorithm - Study Notes and more Study notes Algorithms and Programming in PDF only on Docsity!

Design and Analysis of Algorithms

Chapter 5

Linear Time Sorting

The lower bound implies that if we hope to sort numbers faster than O(n log n), we cannot do it by making comparisons alone. Is it possible to sort without making comparisons? The answer is yes, but only under very restrictive circumstances. Many applications involve sorting small integers (e.g. sorting characters, exam scores, etc.). We present three algorithms based on the theme of speeding up sorting in special cases, by not making comparisons.

5.1 Counting Sort

We will consider three algorithms that are faster and work by not making comparisons. Counting sort assumes that the numbers to be sorted are in the range 1 to k where k is small. The basic idea is to determine the rank of each number in final sorted array.

Recall that the rank of an item is the number of elements that are less than or equal to it. Once we know the ranks, we simply copy numbers to their final position in an output array.

The question is how to find the rank of an element without comparing it to the other elements of the array?. The algorithm uses three arrays. As usual, A[1..n] holds the initial input, B[1..n] holds the sorted output and C[1..k] is an array of integers. C[x] is the rank of x in A, where x ∈ [1..k]. The algorithm is remarkably simple, but deceptively clever. The algorithm operates by first constructing C. This is done in two steps. First we set C[x] to be the number of elements of A[j] that are equal to x. We can do this initializing C to zero, and then for each j, from 1 to n, we increment C[A[j]] by 1. Thus, if A[j] = 5 , then the 5th element of C is incremented, indicating that we have seen one more 5. To determine the number of elements that are less than or equal to x, we replace C[x] with the sum of elements in the sub array R[ 1 : x]. This is done by just keeping a running total of the elements of C.

C[x] now contains the rank of x. This means that if x = A[j] then the final position of A[j] should be at position C[x] in the final sorted array. Thus, we set B[C[x]] = A[j]. Notice We need to be careful if there are duplicates, since we do not want them to overwrite the same location of B. To do this, we decrement

5.1. COUNTING SORT 59

A [1 .. n ] 7 1 3 1 2 4 5 7 2 4 3

Input^1 2 3 4 5 6 7 8 9 10

C [1 ..k ]^0 0 0 0 0

1 2 3 4 5 6 7

k

Figure 5.2: A[ 1 ] = 7 processed

A [1 .. n ] 7 1 3 1 2 4 5 7 2 4 3

Input^1 2 3 4 5 6 7 8 9 10

C [1 ..k ]^1 0 0 0 0

1 2 3 4 5 6 7

k

Figure 5.3: A[ 2 ] = 1 processed

A [1 .. n ] 7 1 3 1 2 4 5 7 2 4 3

Input^1 2 3 4 5 6 7 8 9 10

C [1 ..k ]^1 0 1 0 0

1 2 3 4 5 6 7

k

Figure 5.4: A[ 3 ] = 3 processed

60 CHAPTER 5. LINEAR TIME SORTING

A [1 .. n ] 7 1 3 1 2 4 5 7 2 4 3

Input^1 2 3 4 5 6 7 8 9 10

C [1 ..k ]^2 0 1 0 0

1 2 3 4 5 6 7

k

Figure 5.5: A[ 4 ] = 1 processed

A [1 .. n ] 7 1 3 1 2 4 5 7 2 4 3

Input^1 2 3 4 5 6 7 8 9 10

C [1 ..k ]^2 1 1 0 0

1 2 3 4 5 6 7

k

Figure 5.6: A[ 5 ] = 2 processed

A [1 .. n ] 7 1 3 1 2 4 5 7 2 4 3

Input^1 2 3 4 5 6 7 8 9 10

C [1 ..k ]^2 2 2 2 1

1 2 3 4 5 6 7

finally

Figure 5.7: C now contains count of elements of A

62 CHAPTER 5. LINEAR TIME SORTING

C 2 4 5 8 9 9 11

1 2 3 4 5 6 7

C^2

C [ A [10]] = C [ A [10]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

A [1 .. n ]

Input

B [8] = B [ C [4]] = B [ C [ A [10]]] = A [10] = 4

Figure 5.10: A[ 10 ] placed in output array B

C 2 4 5 7 9 9 11

1 2 3 4 5 6 7

C^2

C [ A [9]] = C [ A [9]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

A [1 .. n ]

Input

B [4] = B [ C [2]] = B [ C [ A [9]]] = A [9] = 2

Figure 5.11: A[ 9 ] placed in output array B

5.1. COUNTING SORT 63

C 2 3 5 7 9 9 11

1 2 3 4 5 6 7

C^2

C [ A [8]] = C [ A [8]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output 3

A [1 .. n ]

Input

B [11] = B [ C [7]] = B [ C [ A [8]]] = A [8] = 7

Figure 5.12: A[ 8 ] placed in output array B

C 2 3 5 7 9 9 10

1 2 3 4 5 6 7

C^2

C [ A [5]] = C [ A [5]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

A [1 .. n ]

Input

B [9] = B [ C [5]] = B [ C [ A [7]]] = A [7] = 5

Figure 5.13: A[ 7 ] placed in output array B

5.1. COUNTING SORT 65

C 2 2 5 7 8 9 10

1 2 3 4 5 6 7

C^1

C [ A [4]] = C [ A [4]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

A [1 .. n ]

Input

B [2] = B [ C [1]] = B [ C [ A [4]]] = A [4] = 1

Figure 5.16: A[ 4 ] placed in output array B

C 1 2 5 7 8 9 10

1 2 3 4 5 6 7

C^1

C [ A [3]] = C [ A [3]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

A [1 .. n ]

Input

B [5] = B [ C [3]] = B [ C [ A [3]]] = A [3] = 3

Figure 5.17: A[ 3 ] placed in output array B

66 CHAPTER 5. LINEAR TIME SORTING

C 1 2 4 7 8 9 10

1 2 3 4 5 6 7

C^0

C [ A [3]] = C [ A [3]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

A [1 .. n ]

Input

B [1] = B [ C [1]] = B [ C [ A [2]]] = A [2] = 1

Figure 5.18: A[ 2 ] placed in output array B

C 0 2 4 7 8 9 10

1 2 3 4 5 6 7

C^0

C [ A [1]] = C [ A [1]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output 3

A [1 .. n ]

Input

B [10] = B [ C [7]] = B [ C [ A [1]]] = A [1] = 7

Figure 5.19: B now contains the final sorted data.

Counting sort is not an in-place sorting algorithm but it is stable. Stability is important because data are often carried with the keys being sorted. radix sort (which uses counting sort as a subroutine) relies on it to work correctly. Stability achieved by running the loop down from n to 1 and not the other way around:

68 CHAPTER 5. LINEAR TIME SORTING

C 2 4 5 8 9 9 11

1 2 3 4 5 6 7

C^2

C [ A [10]] = C [ A [10]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

7 1 3 1 2 4 *^5 7 2 4 **^3

A [1 .. n ]

Input

B [8] = B [ C [4]] = B [ C [ A [10]]] = A [10] = 4

C 2 3 5 7 8 9 10

1 2 3 4 5 6 7

C^2

C [ A [6]] = C [ A [6]] - 1

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

7 1 3 1 2 4 *^5 7 2 4 **^3

A [1 .. n ]

Input

B [7] = B [ C [4]] = B [ C [ A [6]]] = A [6] = 4

1 2 3 4 5 6 7 8 9 10 11

B [1 .. n ]

Output

7’ 1 ^^3 #^1 ^^^2 +^4 *^5 7” 2 ++^4 **^3

A [1 .. n ]

Input

1 ^^1 ^^^2 + 2 ++^3 # 4 * 4 ** 5 7’ 7 ”

Figure 5.20: Stability of counting sort

5.2. BUCKET OR BIN SORT 69

5.2 Bucket or Bin Sort

Assume that the keys of the items that we wish to sort lie in a small fixed range and that there is only one item with each value of the key. Then we can sort with the following procedure:

1. Set up an array of “bins” - one for each value of the key - in order, 2. Examine each item and use the value of the key to place it in the appropriate bin.

Now our collection is sorted and it only took n operations, so this is an O(n) operation. However, note that it will only work under very restricted conditions. To understand these restrictions, let’s be a little more precise about the specification of the problem and assume that there are m values of the key. To recover our sorted collection, we need to examine each bin. This adds a third step to the algorithm above,

3. Examine each bin to see whether there’s an item in it.

which requires m operations. So the algorithm’s time becomes:

T (n) = c 1 n + c 2 m

and it is strictly O(n + m). If m ≤ n, this is clearly O(n). However if m >> n, then it is O(m). An implementation of bin sort might look like:

BUCEKTSORT( array A, int n, int M) 1 // Pre-condition: for 1 ≤ i ≤ n, 0 ≤ a[i] < M 2 // Mark all the bins empty 3 for i ← 1 to M 4 do bin[i] ← Empty 5 for i ← 1 to n 6 do bin[A[i]] ← A[i]

If there are duplicates , then each bin can be replaced by a linked list. The third step then becomes:

3. Link all the lists into one list.

We can add an item to a linked list in O( 1 ) time. There are n items requiring O(n) time. Linking a list to another list simply involves making the tail of one list point to the other, so it is O( 1 ). Linking m such lists obviously takes O(m) time, so the algorithm is still O(n + m). Figures 5.21 through 5.23 show the algorithm in action using linked lists.

Figure 5.23: Bucket sort: the final sorted sequence 5.3 Radix Sort The main shortcoming of counting sort is that it is useful for small integers, i.e., 1..k where k is small. If k were a million or more, the size of the rank array would also be a million. Radix sort provides a nice Here is the algorithm that sorts A[1..n] where each number is d digits long. RADIX-SORT( array A, int n, int d) 1 for i ← 1 to d

-. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. - .78. - .17. - .26. - .94. - .12. - .39.39. - .68. - .72. - .21.21 .23. -. Figure 5.21: Bucket sort: step 1, placing keys in bins in sorted order -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. - .78. - .17. - .26. - .94. - .12. - .39.39. - .68. - .72. - .21.21 .23.

  • 5.3. RADIX SORT -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. - .78. - .17. - .26. -. -. - .39. -. -. - .21 .23.
    • 576 49[4] 9[5]4 [1]76 work around this limitation by sorting numbers one digit at a time.
    • 494 19[4] 5[7]6 [1]94
    • 194 95[4] 1[7]6 [2]78
    • 296 ⇒ 57[6] ⇒ 2[7]8 ⇒ [2]96 ⇒
    • 278 29[6] 4[9]4 [4]94
    • 176 17[6] 1[9]4 [5]76
    • 954 27[8] 2[9]6 [9]54