2 1 Ordenation Algorithms
2 1 Ordenation Algorithms
1
Análisis y Diseño de Algoritmos
(Algorítmica III)
- Ordenation Algorithms -
Profesores:
Herminio Paucar.
Luis Guerra.
Sorting
Sorting. Given n elements, rearrange in ascending order.
● Obvious sorting applications. ● Non-obvious sorting
○ List files in a directory. applications.
Organize an MP3 library. List ○ Data compression. Computer
names in a phone book. graphics. Interval scheduling.
Display Google PageRank Computational biology.
results. Minimum spanning tree.
Supply chain management.
● Problems become easier once
○ Simulate a system of particles.
sorted. Book recommendations on
○ Find the median. Find the Amazon.
closest pair. ○ Load balancing on a parallel
○ Binary search in a database. computer.
○ Identify statistical outliers. ○ ...
Find duplicates in a mailing
list.
1. Basic Algorithms: Bubble Sort and
Selection Sort
Bubble sort
• Compare each element (except the last one) with its neighbor to the
right
– If they are out of order, swap them
– This puts the largest element at the very end
– The last element is now in the correct and final place
• Compare each element (except the last two) with its neighbor to the
right
– If they are out of order, swap them
– This puts the second largest element next to last
– The last two elements are now in their correct and final places
• Compare each element (except the last three) with its neighbor to
the right
– Continue as above until you have no unsorted elements on the left
4
Example of bubble sort
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
5
Analysis of bubble sort
Lower bound:
In a list sorted from the largest to the smallest element we
spend n+(n-1)+(n-2) + … + 1 = Ω(n2)
Obs: In the best case (sorted list) we just scan the list once:
O(n) time
Selection sort
9
2. Mergesort
Mergesort
Mergesort.
● Divide array into two halves.
● Recursively sort each half.
● Merge two halves to make sorted whole.
Jon von Neumann (1945)
A L G O R I T H M S
A L G O R I T H M S divide O(1)
A G L O R H I M S T sort 2T(n/2)
A G H I L M O R S T merge O(n)
11
Merging
A G L O R H I M S T
A G H I
Mergesort recurrence.
T(n) n
...
T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) n/2 (2)
n log2n
14
Proof by Telescoping
assumes n is a power of
2
15
Proof by Induction
assumes n is a power of 2
16
Analysis of Mergesort Recurrence
Claim. If T(n) satisfies the following recurrence, then T(n) ≤ n ⎡lg n⎤.
log2
n
17
Mergesort
Quicksort algorithm:
■ Choose a number pivot in the list; lets say we always choose pivot = A[1]
■ Partition the list A into the list L containing all numbers < , the list
R containing all numbers > , and list P containing all numbers =
■ Sort the list L and R recursively using Quicksort
■ Concatenate L, P, R (in this order)
Quicksort example
Quicksort
Quicksort algorithm:
■ Choose a number pivot in the list; lets say we always choose pivot = A[1]
■ Partition the list A into the list L containing all numbers < , the list
R containing all numbers > , and list P containing all numbers
=
■ Sort the list L and R recursively using Quicksort
■ Concatenate L, P, R (in this order)
Quicksort(A)
if (A.len > 1)
L, R, P = Partition(A);
Quicksort(L); Quicksort(R);
end if
Return the concatenation of L,P,R
Partition
Proof:
• The height of execution tree of QuickSort is at most n (lists L and R are
strictly smaller than A; that’s why we put pivot in P).
• At each level of the tree the algorithm spends O(n) due to the partition
procedure
T(0) = c
T(1) = c We do not know
the size of the
T(n) <= maxi=0…n-1 {T(i) + T(n - i - 1) + cn} subarrays
So partition generates splits (1:n-1, 2:n-2, 3:n-3, … , n-2:2, n-1:1) each with probability
1/n
So it is often balanced
The real liability of quicksort is that it runs in O(n2) on already- sorted input
■ ANÁLISE
– A ordenação da lista L requer O (n log n) utilizando o
MergeSort
– O loop Para requer tempo linear
Exercício
Saida: SIM se existem dois elementos distintos em S com soma x e NÃO caso
contrário
Exercício - Solução
L <- conjunto S em ordem crescente
Enquanto a lista L tiver mais que um elemento faça
Some o menor e o maior elemento de L
Se a soma é x
Devolva SIM
Se a soma é maior que x
Retire o maior elemento de L
Se a soma é menor que x
Retire o menor elemento de L
Fim Enquanto
Devolva NÃO
Exercício - Solução
L <- conjunto S em ordem crescente
Enquanto a lista L tiver mais que um elemento faça
Some o menor e o maior elemento de L
Se a soma é x
Devolva SIM
Se a soma é maior que x
Retire o maior elemento de L
Se a soma é menor que x
Retire o menor elemento de L
Fim Enquanto
Devolva NÃO
ANÁLISE
– A ordenação do conjunto S requer O (n log n) utilizando o MergeSort
– O loop enquanto requer tempo linear
3 How fast can we sort?
How Fast Can We Sort?
Easier question: How many comparisons are necessary for any comparison
sort algorithm?
■ Just counting comparisons, reordering of items is free
How Fast Can We Sort?
A= ???
Answer: No: Suppose just compare A[1] with A[2]
n=4
Decision Trees
Proof:
● Any decision tree has n! leaves
● The height of a decision tree with n! leaves is at least log(n!) because the
decision tree is a binary tree
● log(n!) ≥ (n/2) log n – (n/2):
Input: List of integers A[1], A[2], …, A[n] with values between 1 and k
<= 4
<= 2
output:
<= 1
<= 3
Counting Sort
1 CountingSort(A, B, k)
2 for i=1 to k Initialize
3 C[i]= 0;
C[i] stores the number
4 for j=1 to n
of elements equal
5 C[A[j]] += 1;
6 for i=2 to k to i,i =1,…,k
1 CountingSort(A, B, k) for
2 i=1 to k Takes time O(k)
3 C[i]= 0;
4 for j=1 to n
5 C[A[j]] += 1;
6 for i=2 to k
Takes time O(n)
7 C[i] = C[i] + C[i-1];
8 for j=n downto 1
9 B[C[A[j]]] = A[j];
10 C[A[j]] -= 1;
Counting Sort
■ That is the only reason why the last “For” is in reverse order
Counting Sort
Could we use counting sort to sort 32 bit integers? Why or why not?
Answer: no, k too large (232 = 4,294,967,296)
Radix Sort
One idea is to sort them based on most significant digit, then the second
m.s.d., etc.
Q: Does it work?
A: Not directly: consider input 91,19, 55, 54
■ Sorting by most significant digit: 19, 55, 54, 91
■ Then sorting by second most significant: 91, 54, 44, 91 wrong order!
Each pass over n numbers with d digits takes time O(n), so total
time O(dn)
Obs: Does not need to use digits (writing numbers in base 10),
can use any other base
Radix Sort
● Fast
● Good worst-case guarantee O(nd)
● Simple to code
● A good choice
Radix Sort
Hint: Consider Radix Sort (with Counting Sort) but don’t use
digits to represent the numbers, use a different base (with
more symbols).