# Stable vs. Non-Stable Sorts

### Pages to are hidden for

"Stable vs. Non-Stable Sorts"

```					    MERGESORT –Radix
and Bin Sort -

Csc 2053 SORTING

1                         CSC 2053   1
Stable vs. Non-Stable Sorts
   We frequently use sorting methods for items with multiple keys

   Sometimes we need to apply the sorting with different keys

–   For instance we want to sort a list of people based on last
name and then on age

So Black age 30 should appear before Jones age 30

CSC 2053                          2
Stable vs. Non-Stable Sorts
   If we sort a list based on the first key (name) and then apply a
sort based on the second key (age) how can we guarantee that
the list is still ordered based on the first key?

   Definition:

        A sorting method is said the be stable if it preserves the
relative order of duplicated keys on the list

CSC 2053                           3
An Example of a Stable Sort
R. Sedgewick

Washington (23)            Black (23)        Jackson (23)

Wilson (50)                Brown (40)        Washington (23)

Black (23)                 Jackson (23)      Adams (30)

Brown (40)                 Jones (50)        Smith (30)

Smith (30)                 Smith (30)        Brown (40)

Thompson (40)              Thompson (40)     Thompson (40)

Jackson (23)               Washington (23)   Jones (50)

White (50)                 White (50)        White (50)

Jones (50)                 Wilson (50)       Wilson (50)

CSC 2053                  4
Stable vs. Non-Stable Sorts
 Mergesort is relatively easy to be made stable
– Just make sure the merge function is stable

 Heapsort sorts in O(n log n) but it is not stable

 Quicksort is also not stable

 Exercise: You should experiment with all the main sorting
algorithms to understand which ones are stable and which ones
are not.

CSC 2053                     5
Mergesort

   We saw that Quicksort is based on the idea of selecting an
element and dividing the list in two halves and then sorting the
halves separately

   The complementary process which is called merging.
– Given two lists which are ordered, combine them into a
larger ordered list

CSC 2053                          6
MERGESORT
   Selection and merging are complementary because

–   Selection divides a list into two independent lists

–   Merging joins two independent lists into one larger list

   Mergesort consists of two recursive calls and one merging
procedure

CSC 2053                           7
Mergesort
   The desirable features of Mergesort

–   It performs in O (n log n) in the worst case
–   It is stable

–   It is quite independent of the way the initial list is organized

–   Good for linked lists. Can me implemented in such a way that
data is accessed sequentially

CSC 2053                              8
Mergesort
   Drawbacks

–   It may require an auxiliary array of up to the size of the
original list

This can be avoided but the algorithm becomes
significantly more complicated making it not
worth it

Instead we can use heapsort which is also O(n
log n)

CSC 2053                           9
Understanding the Algorithm

1. Calculate the index of the middle of the list,
called it m

2. Use recursion to sort the two partitions
[first,m] and [m+1,last]

3. Merge the two ordered lists into one large list

CSC 2053                          10
Mergesort
void mergesort(int list[], int first, int last) {
// PRE: list is an array &&
// the portion to be sorted runs from first to last inclusive
if (first >= last) // Nothing to sort
return;

int m = (first+last)/2; // calculate the middle of the list

// Recursively call the two partitions
mergesort(list,first,m);
mergesort(list,m+1,last);

merge(list,first,m,last); // merges two sorted lists

// POST: list is sorted in ascending order between the first
//   and the last
}
CSC 2053                            11
Understanding the merge function
   We know that we can easily merge two arrays into a third one
   Let us try to improve this by using only two arrays.

   Given an array where we know that this list is organized in such
a way that
     from first to m we have half of the array sorted and
     from m+1 to last we have another half to be sorted, we can
have a merge in-place

–   merge(list, first, m, last)

   We'll see that an extra array is still required but we save time by
not having to create an extra array to hold the two halves.

CSC 2053                          12
Understanding the merge
     When the merge function is called we have the following
scenario – a list divided into two sections each in ascending
order. Store the two lists in second array as below

list   list in ascending order               list in ascending order

first                                      m                                last

     To make the algorithm simpler, reverse the second half of the
list storing it in an auxiliary list in descending order
aux       list in ascending order              list in descending order

first                                      m                                last

CSC 2053                              13
Tracing the merge from the second
array to the first (last step)
first(0)                                                  last(7)

list   2   4   9      10          3   4   11   12

m(3)

CSC 2053                           14
Tracing the merge
Create and auxiallary array
first(0)                                                      last(7)

list       2   4   9      10          3   4   11   12

m(3)

Aux

CSC 2053                           15
Tracing the merge
Create a variable i to refer what m is referring to (the middle)

first(0)                                                           last(7)

list   2          4   9      10          3   4   11   12

i(3)                   m(3)
Auxilliary
array

CSC 2053                           16
Tracing the merge
Copy the middle of the first array to the middle of the auxiallary
array and decrement i.

first(0)                                                         last(7)

list   2          4   9      10          3   4   11   12

i(3)                   m(3)

aux                                 10

CSC 2053                           17
Tracing the merge
(last step)
first(0)                                                             last(7)

list       2          4   9      10          3   4   11   12

i(2)                   m(3)

aux                         10

CSC 2053                           18
Tracing the merge
Continue to copy the first part of the first array to the
second array

first(0)                                                             last(7)

list       2          4   9      10          3   4   11   12

i(1)                   m(3)

aux                  9      10

CSC 2053                           19
Tracing the merge
(last step)
first(0)                                                             last(7)

list       2          4   9      10          3   4   11   12

i(2)                   m(3)

aux                  9      10

CSC 2053                           20
Tracing the merge
(last step)
first(0)                                                             last(7)

list       2          4   9      10          3   4   11   12

i(1)                   m(3)

aux              4   9      10

CSC 2053                           21
Tracing the merge
(last step)
first(0)                                                         last(7)

list   2          4   9      10          3   4   11   12

i(1)                   m(3)

aux    2          4   9      10

CSC 2053                           22
Tracing the merge
(last step)
first(0)                                                         last(7)

list   2          4   9      10          3   4   11   12

i(0)                   m(3)

aux    2          4   9      10

CSC 2053                           23
Tracing the merge
Assign j to last element in the first array
first(0)                                                            last(7)

list   2          4   9      10          3   4   11      12

i(0)                   m(3)            j(7)

aux    2          4   9      10

CSC 2053                              24
Tracing the merge
Copy elements from j(12) backwards to m(10) to
second array in descending order
first(0)                                                             last(7)

list   2          4   9      10          3    4   11      12

i(0)                   m(3)             j(7)

aux    2          4   9      10          12

CSC 2053                               25
Tracing the merge
Copy elements from j(12) backwards to m(10) to
second array in descending order
first(0)                                                             last(7)

list   2          4   9      10          3    4   11      12

i(0)                   m(3)             j(6)

aux    2          4   9      10          12

CSC 2053                               26
Tracing the merge
(last step)
first(0)                                                              last(7)

list   2          4   9      10          3    4    11      12

i(0)                   m(3)              j(6)

aux    2          4   9      10          12   11

CSC 2053                                27
Tracing the merge
(last step)
first(0)                                                              last(7)

list   2          4   9      10          3    4    11      12

i(0)                   m(3)              j(5)

aux    2          4   9      10          12   11

CSC 2053                                28
Tracing the merge
(last step)
first(0)                                                                 last(7)

list   2          4   9      10          3    4    11         12

i(0)                   m(3)                 j(5)

aux    2          4   9      10          12   11   4

CSC 2053                                   29
Tracing the merge
(last step)
first(0)                                                                 last(7)

list   2          4   9      10          3    4    11         12

i(0)                   m(3)                 j(4)

aux    2          4   9      10          12   11   4

CSC 2053                                   30
Tracing the merge
(Stop when j reaches m(at 10)
first(0)                                                                 last(7)

list   2          4   9      10          3    4    11         12

i(0)                   m(3)                 j(4)

aux    2          4   9      10          12   11   4          3

CSC 2053                                   31
Tracing the merge
Now merge two parts of the array
first(0)                                                                 last(7)

list   2          4   9      10          3    4    11         12

i(0)                   m(3)                 j(3)

aux    2          4   9      10          12   11   4          3

CSC 2053                                   32
Tracing the merge
(Assign i to index 0 and j to lastindex
first(0)                                k(0)                             last(7)

list   2          4   9      10          3    4    11         12

i(0)                   m(3)                 j(7)

aux    2          4   9      10          12   11   4          3

CSC 2053                                   33
Tracing the merge
Starting with i, compare i to j(2 to 3)

first(0)                                k(0)                             last(7)

list   2          4   9      10          3    4    11         12

i(0)                   m(3)                 j(7)

aux    2          4   9      10          12   11   4          3

CSC 2053                                   34
Tracing the merge
Store the smaller of the two in the main list and increment i

first(0)                               k(1)                             last(7)

list 2          4   9      10          3    4    11         12

i(1)                   m(3)                 j(7)

aux   2          4   9      10          12   11   4          3

CSC 2053                                   35
Tracing the merge
Compare i and j and choose smallest(3) and decrement j

first(0)                            k(1)                         last(7)

list
3   9      10          3    4    11      12
2

i(1)               m(3)              j(7)

aux
4   9      10          12   11   4       3
2

CSC 2053                                36
Tracing the merge
Compare i and j and pick smallest. The are same so choose the
first one - this is a stable sort – and increment i

first(0)                               k(2)                             last(7)

list 2          3   9      10          3    4    11         12

i(1)                   m(3)                 j(6)

aux   2          4   9      10          12   11   4          3

CSC 2053                                   37
Tracing the merge
(last step)
first(0)                                 k(2)                             last(7)

list   2          3   4      10          3    4    11         12

i(1)                   m(3)                 j(6)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   38
Tracing the merge
Compare i and j and pick smallest - 4

first(0)                                 k(3)                             last(7)

list   2          3   4      10          3    4    11         12

i(2)                   m(3)                 j(6)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   39
Tracing the merge
(last step)
first(0)                                 k(3)                             last(7)

list   2          3   4      4           3    4    11         12

i(2)                   m(3)                 j(6)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   40
Tracing the merge
decrement j and Compare i and j and pick
smallest
first(0)                                 k(4)                             last(7)

list   2          3   4      4           3    4    11         12

i(2)                   m(3)                 j(5)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   41
Tracing the merge
(last step)
first(0)                                 k(4)                             last(7)

list   2          3   4      4           9    4    11         12

i(2)                   m(3)                 j(5)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   42
Tracing the merge
(last step)
first(0)                                 k(5)                             last(7)

list   2          3   4      4           9    4    11         12

i(3)                   m(3)                 j(5)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   43
Tracing the merge
(last step)
first(0)                                 k(5)                             last(7)

list   2          3   4      4           9    10   11         12

i(3)                   m(3)                 j(5)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   44
Tracing the merge
(last step)
first(0)                                 k(6)                             last(7)

list   2          3   4      4           9    10   11         12

i(4)                   m(3)                 j(5)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   45
Tracing the merge
(last step)
first(0)                                 k(6)                             last(7)

list   2          3   4      4           9    10   11         12

i(4)                   m(3)                 j(5)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   46
Tracing the merge
(last step)
first(0)                                 k(7)                             last(7)

list   2          3   4      4           9    10   11         12

i(4)                   m(3)                 j(4)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   47
Tracing the merge
(last step)
first(0)                                 k(7)                             last(7)

list   2          3   4      4           9    10   11         12

i(4)                   m(3)                 j(4)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   48
Tracing the merge
i and j crossover – so method ends and main list is sorted in
ascending order

first(0)                                 k(8)                             last(7)

list   2          3   4      4           9    10   11         12

i(4)                   m(3)                 j(3)

aux     2          4   9      10          12   11   4          3

CSC 2053                                   49
merge
void merge(int list[], int first, int m, int last) {
int i, j;
int aux[MAXSIZE]; // This copies the mainarray
for (i = m+1; i > first;i--) // to the second array
aux[i-1] = list[i-1];
for (j = m; j < r; j++)
aux[last+m-j] = list[j+1];

// ASSERT: aux list has been prepared with the right half in
descending order and left half in ascending order
for (int k = 0; k <= last; k++)
if (aux[j] < aux[i]) // this re-assembles the list into
list[k] = aux[j--]; // sorted order to the first array
else
list[k] = aux[i++];
}
CSC 2053                           50
On the performance of Mergesort
   Unlike quicksort, mergesort guarantees O(n log n) in the worst
case
– The reason for this is that quicksort depends on the value of
the pivot whereas mergesort divides the list based on the
index

   Why is it O (n log n)?

– Each merge will require N comparisons
– Each time the list is halved
– So the standard divide-and-conquer recurrence applies to
mergesort

CSC 2053                       51
Lecture Sort - Key Points
   Quicksort

–   Use for good overall performance where time is not a
constraint

   Heap Sort
– Slower than quick sort, but guaranteed O(n log n)
– Use for real-time systems where time is critical

CSC 2053                         52
   In many applications the key used in the ordering is not as
simple as we would expect

–   Keys in phone books

–   The key in a library catalog

–   ZIP codes

   So far we have not considered the subtleties of dealing with
complex keys

CSC 2053                       53
   Also in several applications the consideration of the whole key
is not necessary

–   How many letters of a person name do we compare to find
this person in the phone book?

   Radix sort algorithms try to gain the same sort of efficiency by
decomposing keys into pieces and comparing the pieces

CSC 2053                          54
   So the main idea is to treat numbers as being represented in a
base and work with individual digits of the numbers

–   We could represent a number in binary and work with the
individual bits

–   We can consider the number in decimal and work with the
individual digits

–   We can consider strings as sequence of characters and work
with the individual characters
–   etc.

CSC 2053                         55
   Radix sort is used by several applications that deal with
– Telephone numbers
– Social security
– ZIP codes

   Consider the example with ZIP codes
– Letter can be divided into 10 boxes: ZIP codes starting with
0 go to box 0, ZIP codes starting with 1 go to box 1 and so on
– After the zip codes are separated in boxes, each of the boxes
can be sorted further considering the second digit

CSC 2053                             56
An example (MSD)

0   2    1     0   4
3   2    8     3   2
2   2    5     1   3
5   5    3     1   6
0   0    0     1   7
1   8    2     2   8
6   6    5     8   8
3   6    6     9   4
3   8    3     0   6
1   3    4     4   5
5   4    4     4   8
2   6    4     7   5
0   2    9     4   1

CSC 2053           57
An example (MSD)

0   2    1     0   4
0   0    0     1   7
Sorted on
0   2    9     4   1
first digit        1   8    2     2   8
1   3    4     4   5
2   2    5     1   3
2   6    4     7   5
3   2    8     3   2
3   6    6     9   4
3   8    3     0   6
5   5    3     1   6
5   4    4     4   8
6   6    5     8   8

CSC 2053           58
An example (MSD)

0   0    0     1   7
0   2    1     0   4
Sorted on
0   2    9     4   1
first digit        1   3    4     4   5
and second         1   8    2     2   8
digit              2   2    5     1   3
2   6    4     7   5
3   2    8     3   2
3   6    6     9   4
3   8    3     0   6
5   4    4     4   8
5   5    3     1   6
6   6    5     8   8

CSC 2053           59
   Because radixsort deals with individual digits of a number we
have choices on how to go about comparing the digits
– From left to right
– From right to left

   The methods that use the first type are called MSD (Most-
   The methods that use the second type are called LSD (Less-

   MSD sorts are normally more frequently used because the
examine the minimum number of data to get the job done

CSC 2053                         60
   This depend on the size of the keys.

   Normally for smaller keys a simple extraction of the digits of the
keys can do the job

   For large keys it may be a better idea to use the binary
representation of the key.

–   Computers represent data with binary numbers

–   Most modern languages allow us to deal with the binary
representation of variables

CSC 2053                            61
Sorting - Bin Sort
   Assume
– All the keys lie in a small, fixed range
 eg
– integers 0-99
– characters ‘A’-’z’, ‘0’-’9’
– There is at most one item with each value of the key
   Bin sort
 Allocate a bin for each value of the key
 Usually an entry in an array
 For each item,
 Extract the key
 Compute it’s bin number
 Place it in the bin
 Finished!

CSC 2053                             62
Sorting - Bin Sort: Analysis
–   All the keys lie in a small, fixed range
 There are m possible key values
–   There is at most one item with each value of the key
   Bin sort
   Allocate a bin for each value of the key   O(m)
 Usually an entry in an array

 For each item,               n times
Key
 Extract the key           O(1)                        condition
 Compute it’s bin number   O(1)
 Place it in the bin       O(1) x n  O(n)
 Result: O(n) + O(m) = O(n+m) = O(n)  if n >> m

CSC 2053                         63
Sorting - Bin Sort: Caveat
   Key Range
– All the keys lie in a small, fixed range
 There are m possible key values
– If this condition is not met, eg m >> n,
then bin sort is O(m)

   Example
– Key is a 32-bit integer, m = 232
– Clearly, this isn’t a good way to sort a few thousand
integers
– Also, we may not have enough space for bins!
   Bin sort trades space for speed!
– There’s no free lunch!

CSC 2053                             64
Sorting - Bin Sort with duplicates
Relax?

–  There is at most one item with each value of the key
   Bin sort
 Allocate a bin for each value of the key    O(m)
 Usually an entry in an array
 For each item,                   n times
 Extract the key               O(1)
 Compute it’s bin number       O(1)
 Add it to a list             O(1) x n  O(n)
 Join the lists                            O(m)
 Finished!     O(n) + O(m) = O(n+m) = O(n)      if n >>
m

CSC 2053                         65
Sorting - Generalised Bin Sort
• Bin sort in phases
• Example
36 9 0 25 1 49 64 16 81 4
•   Phase 1 - Sort by least significant digit

0    1 2         3     4 5 6 7             8    9
0    1                64 25 36                  9
81                  4   16                 49

CSC 2053                      66
Sorting - Generalised Bin Sort
    Radix sort - Bin sort in phases
• Phase 1 - Sort by least significant digit

0      1 2        3    4 5 6 7             8    9
0      1              64 25 36                  9
81                4   16                 49
•   Phase 2 - Sort by most significant digit

0        1   2   3    4   5    6      7   8   9
0

CSC 2053                 67
Sorting - Generalised Bin Sort
   Radix sort - Bin sort in phases
• Phase 1 - Sort by least significant digit
0      1 2        3    4 5 6 7            8    9
0      1              64 25 36                 9
81                4   16                49
•   Phase 2 - Sort by most significant digit
0        1   2    3   4   5    6      7   8   9     Be careful to
1

The O bin
holds
values 0-9
CSC 2053                           68
Sorting - Generalised Bin Sort
   Radix sort - Bin sort in phases
• Phase 1 - Sort by least significant digit

0        1    2   3   4   5    6      7   8    9
1           64                       9
0                        25 36
81           4     16                49
•   Phase 2 - Sort by most significant digit

0        1    2   3   4   5    6      7    8 9
0                                         81
1

CSC 2053                 69
Sorting - Generalised Bin Sort
    Radix sort - Bin sort in phases
• Phase 1 - Sort by least significant digit

0     1 2          3    4 5 6 7                8     9
0     1                64 25 36                      9
81                  4   16                     49

•   Phase 2 - Sort by most significant digit

0        1   2     3     4    5     6 7 8 9
0                                  64   81
1

CSC 2053                  70
Sorting - Generalised Bin Sort
   Radix sort - Bin sort in phases
• Phase 1 - Sort by least significant digit

0      1 2         3    4 5 6 7                8     9
0      1               64 25 36                      9
81                 4   16                     49

•   Phase 2 - Sort by most significant digit

0        1   2     3    4     5    6 7 8 9
0                                 64   81
1
4

CSC 2053                  71
Sorting - Generalised Bin Sort
   Radix sort - Bin sort in phases
• Phase 1 - Sort by least significant digit

0      1 2         3    4 5 6 7                8     9
0      1               64 25 36                      9
81                 4   16                     49

•   Phase 2 - Sort by most significant digit

0 1 2 3 4 5 6 7 8 9                                      Note that the 0
0 16 25 36 49 64 81                                       quite large!
1
4
9

CSC 2053                           72
Sorting - Generalised Bin Sort
   Radix sort - Bin sort in phases
• Phase 1 - Sort by least significant digit

0      1 2        3     4 5 6 7               8     9
0      1               64 25 36                     9
How much
81                 4   16                    49   space is needed
in each phase?
• Phase 2 - Sort by most significant digit
n items
0 1 2 3 4 5 6 7 8 9                                         m bins
0 16 25 36 49 64 81
1
4
9

CSC 2053                           73
Sorting - Generalised Bin Sort
• Phase 1 - Sort by least significant digit
• Create m bins       O(m)
• Allocate n items    O(n)
• Phase 2
• Create m bins         O(m)
• Allocate n items    O(n)
• Final
• All steps in sequence, so add
• Total       O(3m+2n) O(m+n)  O(n) for m<<n

CSC 2053                   74
Sorting - Radix Sort - Analysis
• Base (or radix) in each phase can be anything suitable
• Integers
• Base 10, 16, 100, …
• Bases don’t have to be the same
class date {                              Phase 1 - s1=31 bins
int day;   /* 1 .. 31 */
int month; /* 1 .. 12 */              Phase 2 - s2=12 bins
int year; /* 0 .. 99 */               Phase 3 - s3=100 bins
}

•   Still O(n)   if n >> si for all i

CSC 2053                         75
   For sorting n records with k number of digits the running time
of Radixsort is equivalent to nk = O(n)

–   This is because the the algorithms makes k (constant) pass
over all n keys

   Clearly this performance depend on the sorting used to sort
the element based on digit k.

CSC 2053                         76
for(i=0;i<k;i++) {                            O( si )
for(j=0;j<s[i];j++) bin[j] = EMPTY;
for(j=0;j<n;j++) {
move A[i]                          O( n )
to the end of bin[A[i]->fi]
}
for(j=0;j<s[i];j++)
concat bin[j] onto the end of A;
O( si )
}
}

CSC 2053                 77
for(i=0;i<k;i++) {                                  O( si )
for(j=0;j<s[i];j++) bin[j] = EMPTY;
for(j=0;j<n;j++) {
Clear the si bins for the ith radix
move A[i]                                 O( n )
to the end of bin[A[i]->fi]
}
for(j=0;j<s[i];j++)
concat bin[j] onto the end of A;
O( si )
}
}

CSC 2053                          78
for(i=0;i<k;i++) {                                O( si )
for(j=0;j<s[i];j++) bin[j] = EMPTY;
for(j=0;j<n;j++) {
move A[i]                              O( n )
to the end of bin[A[i]->fi]
}
for(j=0;j<s[i];j++)
Move element A[i]
concat bin[j] onto the end of A;
}        to the end of the bin addressed O( si )
}                by the ith field of A[i]

CSC 2053                         79
for(i=0;i<k;i++) {                                   O( si )
for(j=0;j<s[i];j++) bin[j] = EMPTY;
for(j=0;j<n;j++) { Concatenate si bins into
move A[i]        one list again            O( n )
to the end of bin[A[i]->fi]
}
for(j=0;j<s[i];j++)
concat bin[j] onto the end of A;
O( si )
}
}

CSC 2053                            80
Sorting - Better than O(n log n) ?
   If all we know about the keys is an ordering rule
– No!
   However,

–   If we can compute an address from the key
(in constant time) then

–   bin sort algorithms can provide better performance

CSC 2053                           81
   For large values of n the running time of radixsort is
comparable to O(n log n)

–   If we use binary representation of the keys and we have 1
million 32-bit keys, then k and log n are both about 32. So
kn would be comparable to n log n

CSC 2053                          82
   Total
–   k iterations, 2si + n for each one

–   As long as k is constant

– In general, if the keys are in (0, bk-1)
 Keys are k-digit base-b numbers
si = b for all k
 Complexity       O(n+kb) = O(n)

CSC 2053             83
?   Any set of keys can be mapped to (0, bk-1 )

!   So we can always obtain O(n) sorting?

   If k is constant, yes

CSC 2053            84
–   But, if k is allowed to increase with n

eg it takes logbn base-b digits to represent n

–   Radix sort is no better than quicksort

= O(n log n + 2 log n )
= O(n log n )

CSC 2053                           85
•   Radix sort is no better than quicksort

•   Another way of looking at this:
• We can keep k constant as n increases
if we allow duplicate keys
• keys are in (0, bk ), bk < n
•   but if the keys must be unique,
then k must increase with n

•   For O(n) performance,
the keys must lie in a restricted range

CSC 2053               86
   Radix sort uses a lot of memory
–   n si locations for each phase

–   In practice, this makes it difficult to achieve O(n)
performance

–   Cost of memory management outweighs benefits

CSC 2053                           87
Lecture 9 - Key Points
   Bin Sorts

–   If a function exists which can convert the key to an address
(ie a small integer)

and the number of addresses (= number of bins) is not too
large
then we can obtain O(n) sorting

… but remember it’s actually O(n + m)

–   Number of bins, m, must be constant and small

CSC 2053                         88
Bin or Bucket Sort Analysis

   Bucket sorts work well for data sets where the possible key values
are known and relatively small and there are on average just a
few elements per bucket.

   This means the cost of sorting the contents of each bucket can be
reduced toward zero.

   The ideal result is if the order in each bucket is uninteresting or
trivial, for instance, when each bucket holds a single key.

   The buckets may be arranged so the concatenation phase is not
needed, for instance, the buckets are contiguous parts of an
array.

CSC 2053                            89
Sorting
   We now know several sorting algorithms

–   Insertion O(n2)
–   Heap      O(n log n)   Guaranteed
–   Quick     O(n log n)   Most of the time!

   Can we do any better?

CSC 2053                   90

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 6 posted: 7/27/2012 language: English pages: 90