0% found this document useful (0 votes)
31 views26 pages

Bucket Sort - UNIT I - Sorting

Bucket sort is a linear sorting algorithm that works by distributing elements into buckets based on their values. It then sorts each bucket individually and concatenates the buckets together. It is most suitable when input values are uniformly distributed. Bucket sort runs in linear time O(n) as it makes a single pass through the data to distribute elements into buckets and another pass to concatenate the sorted buckets.

Uploaded by

Anusha Somaraddi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views26 pages

Bucket Sort - UNIT I - Sorting

Bucket sort is a linear sorting algorithm that works by distributing elements into buckets based on their values. It then sorts each bucket individually and concatenates the buckets together. It is most suitable when input values are uniformly distributed. Bucket sort runs in linear time O(n) as it makes a single pass through the data to distribute elements into buckets and another pass to concatenate the sorted buckets.

Uploaded by

Anusha Somaraddi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Sorting

Syllabus

• Bucket Sort

• Radix Sort

• Counting Sort

• Analysis of algorithms.
Sorting

• Commonly encountered programming task in


computing.

• Examples of sorting:
– List containing exam scores sorted from Lowest to Highest or from
Highest to Lowest
– List containing words that were misspelled and be listed in
alphabetical order.
– List of student records and sorted by USN or alphabetically by first or
last name.
Sorting
Ascending order Descending order

st
alle
sm
t to
rg est g e s
t t o la Lar
a ll e s
Sm
Why do we sort?

• Searching for an element in an array will be more efficient.


(example: looking up for information like phone number).

– For searching on unsorted data by comparing keys, optimal solutions require (n) comparisons.
– For searching on sorted data by comparing keys, optimal solutions require (log n)
comparisons.

• It’s always nice to see data in a sorted display. (example:


spreadsheet or database application).

• Most people prefer to get reports sorted into some relevant


order before flipping through pages of data!
Performance of Sorting Algorithms
• We usually want to know…
– How many comparisons are required
– How many exchanges are required
• We are also interested in…
– Work required in best-case
– Work required in worst-case
– Work required in average-case
• Worst-case easiest to analyze, average-case is hardest
• Also want to know when best-case and worst-case
occur
– Often, something like “array already sorted” or “array is in
descending order”, etc.
Syllabus

• Bucket Sort – Sorting in linear time.

• Radix Sort

• Counting sort

• Analysis of above algorithms.


How Fast Can We Sort?

8
How Fast Can We Sort?

• Selection Sort, Bubble Sort, Insertion Sort: O(n2)


• Heap Sort, Merge sort:
O(nlgn)
• Quicksort: O(nlgn) - average
• What is common to all these algorithms?
– Make comparisons between input elements

ai < aj, ai ≤ aj, ai = aj, ai ≥ aj, or ai > aj


Lower-Bound for Sorting

• Theorem: To sort n elements, comparison sorts must make


(nlgn) comparisons in the best case.

We can beat the (nlgn) running time if we use other operations


than comparing elements with each other!
Can we do better?

• Linear sorting algorithms


– Bucket sort
– Radix Sort
– Counting Sort

• Make certain assumptions about the data

• Linear sorts are NOT “comparison sorts”


Bucket Sort
• Also known as Bin Sort.

• Input: A[1 . . n], where 0 ≤ A[i] < 1 for all i

• Output: elements A[i] sorted

• Auxiliary array: B[0 . . n - 1] of linked lists, each list initially empty

• Assumption:
– the input is generated by a random process that distributes elements
uniformly over [0, 1)
Bucket Sort

• The basic idea of bucket sort was proposed by E.J.Isaac and


R.C. Singleton.

• It has been in use since 1956.


Bucket Sort

• Idea:

– Divide [0, 1) into k equal-sized buckets (k=Θ(n))

– Distribute the n input values into the buckets

– Sort each bucket (e.g., using quick sort)

– Go through the buckets in order, listing elements in each


one
Bucket Sort - Example
A 1 .78 B 0 /

2 .17 1 .17 .12 /

3 .39 2 .26 .21 .23 /


4 .26 3 .39 /

5 .72 4 / Distribute
6 .94 5 / Into buckets
7 .21 6 .68 /

8 .12 7 .78 .72 /

9 .23 8 /
10 .68 9 .94 /
Bucket Sort - Example
0 /

1 .12 .17 /

2 .21 .23 .26 /

3 .39 /
Sort within each
4 /
bucket
5 /

6 .68 /

7 .72 .78 /

8 /

9 .94 / 16
Bucket Sort - Example
.12 .17 .21 .23 .26 .39 .68 .72 .78 .94 /

0 /

1 .12 .17 /

2 .21 .23 .26 /

3 .39 /

4 /

5 /

6 .68 /

7 .72 .78 /
Concatenate the lists from
8 / 0 to n – 1 together, in order
9 .94 /
Bucket Sort – Example 2

• Illustrate the operation of Bucket Sort on


the array
A = <.79, .13, .16, .64, .39, .20, .89, .53, .71, .42>.

• Prove that the Bucket Sort runs in linear time.


How should we implement the buckets?

• Linked Lists or Arrays?

• Linked list saves space as some buckets may


have few entries and some may have lots

• With linked list, fast algorithms like Quick Sort,


Heap Sort cannot be used.
When is bucket sort most suitable?

When the input is uniformly distributed


Bucket Sort - Variation

• Suppose the values in the list to be sorted can


repeat but the values have a limit (e.g., values
are digits from 0 to 9)
Bucket sort
• Idea: suppose the values are in the range
0..m-1; start with m empty buckets numbered
0 to m-1, scan the list and place element s[i] in
bucket s[i], and then output the buckets in
order
• Will need an array of buckets, and the values
in the list to be sorted will be the indexes to
the buckets
– No comparisons will be necessary
Bucket Sort - Example
4 2 1 2 0 3 2 1 4 0 2 3 0

2
0 1 2 3
0 1 2 3 4
0 2 4

0 0 0 1 1 2 2 2 2 3 3 4 4
Bucket Sort - Sorting integers

• Can we perform bucket sort on any array of (non-


negative) integers?

– Yes, but note that the number of buckets will depend on


the maximum integer value

• If you are sorting 1000 integers and the maximum


value is 999999, you will need 1 million buckets!
Correctness of Bucket Sort
• Consider two elements A[i], A[ j]
• Assume without loss of generality that A[i] ≤ A[j]
• Then nA[i] ≤ nA[j]
– A[i] belongs to the same bucket as A[j] or to a bucket with
a lower index than that of A[j]
• If A[i], A[j] belong to the same bucket:
– sorting puts them in the proper order
• If A[i], A[j] are put in different buckets:
– concatenation of the lists puts them in the proper order
Analysis of Bucket Sort
Alg.: BUCKET-SORT(A, n)

for i ← 1 to n O(n)

do insert A[i] into list B[nA[i]]

for i ← 0 to n - 1
k O(n/k log(n/k))
=O(nlog(n/k)
do sort list B[i] with quick sort

concatenate lists B[0], B[1], . . . , B[n -1]


O(n)
together in order

return the concatenated lists


(n)

You might also like