Escolar Documentos
Profissional Documentos
Cultura Documentos
Summary
Divide the unsorted collection into two
Until the sub-arrays only contain one element
Then merge the sub-problem solutions together
Uses divide and conquer strategy
Radix Sort
Radix Sort
Radix Sort is a sorting algorithm that sorts
items by scanning individual digits.
It does not involve comparison between the
items being sorted.
This sort make use of key to shuffle the items.
For integer data items the key is the each
individual digit at each decimal place in that
item for a specific pass.
Radix Sort
We start with Least significant digit(LSD) to
Most significant digit (MSD)
Every integer can be represented by at most k
digits
d1d2dk where di are digits in base r
d1: most significant digit
dk: least significant digit
Radix Sort
Two classifications of radix sort:
For integer sorting: it sorts items from LSD to
MSD
For alphabets sorting: it sorts items from
MSD to LSD.
Radix Sort
Algorithm
Sort by the least significant digit first (counting sort)
=> Numbers with the same digit go to same bin
Reorder all the numbers: the numbers in bin 0 precede the numbers
in bin 1, which precede the numbers in bin 2, and so on
Radix Sort
Least-significant-digit-first
Example: 275, 087, 426, 061, 509, 170, 677, 503
1st Pass- List becomes
170, 061, 503, 275, 426, 087, 677, 509
Quick Sort
Quicksort Algorithm
Given an array of n elements (e.g., integers):
If array only contains one element, return
Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
Example
We are given array of n integers to sort:
40
20
10
80
60
50
30 100
40
20
10
80
60
50
30 100
Partitioning Array
Given a pivot, partition the elements of the array such that the
resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.
Partitioning loops through, swapping elements below/above pivot.
pivot_index = 0
40
20
10
80
60
50
30 100
pivot_index = 0
40
20
10
80
60
50
30 100
pivot_index = 0
40
20
10
80
60
50
30 100
pivot_index = 0
40
20
10
80
60
50
30 100
pivot_index = 0
40
20
10
80
60
50
30 100
pivot_index = 0
40
20
10
80
60
50
30 100
pivot_index = 0
40
20
10
80
60
50
30 100
pivot_index = 0
40
20
10
30
60
50
80 100
pivot_index = 0
40
20
10
30
60
50
80 100
pivot_index = 0
40
20
10
30
60
50
80 100
pivot_index = 0
40
20
10
30
60
50
80 100
pivot_index = 0
40
20
10
30
60
50
80 100
pivot_index = 0
40
20
10
30
60
50
80 100
pivot_index = 0
40
20
10
30
60
50
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
pivot_index = 0
40
20
10
30
50
60
80 100
40
20
10
30
50
60
80 100
20
10
30
40
50
60
80 100
Partition Result
7
20
10
30
40
50
60
80 100
> data[pivot]
20
10
30
40
50
60
80 100
> data[pivot]