Documente Academic
Documente Profesional
Documente Cultură
Internal sorting
Sorting
Sorting
Bubblesort
void bubble(ITEM[] a, int l, int r)
{
for (int i = l; i < r; i++)
for (int j = r; j > i; j--)
compExch(a, j-1, j);
}
For each i from l to r-1, the inner (j) loop puts the minimum element
among the elements in a[i], ..., a[r] into a[i] by passing from right to left
through the elements, compareexchanging successive elements. The
smallest one moves on all such comparisons, so it "bubbles" to the
beginning.
Complexity of Bubblesort
Complexity of Bubblesort
c ((N 1) + (N2) + ... + 1) +k
(N 1) + (N 2) + ... + 1
+
1 + 2 + + (N 1) =
= N + N + + N = N (N 1)
Selection Sort
void selection(ITEM[] a, int l, int r)
{
for (int i = l; i < r; i++)
{
int min = i;
for (int j = i+1; j <= r; j++)
if (less(a[j], a[min]))
min = j;
exch(a, i, min);
}
}
For each i from l to r-1, exchange a[i] with the minimum element in a[i],
..., a[r]. As the index i travels from left to right, the elements to its left are
in their final position in the array (and will not be touched again), so the
array is fully sorted when i reaches the right end.
DSA - lecture 11 - T.U. Cluj-Napoca - M. Joldos
c (N2 N) + k
also O(N2)
Insertion Sort
void insertion(ITEM[] a, int l, int r)
{
int i;
for (i = r; i > l; i--)
compExch(a, i-1, i);
for (i = l+2; i <= r; i++)
{
int j = i; ITEM v = a[i];
while (less(v, a[j-1]))
{
a[j] = a[j-1];
j--;
}
a[j] = v;
For each i, it sorts the elements a[l], ..., a[i] by moving one
position to the right elements in the sorted list a[l], ..., a[i-1]
}
that are larger than a[i], then putting a[i] into its proper
}
position.
10
Shellsort
Insertion sort
adjacent items
items can move through the array only one place
at a time
Shellsort
11
Shellsort Example
insertion sorting the subfile at
positions 0, 4, 8, 12
insertion sorting the subfile at
positions 1, 5, 9, 13
12
Shellsort
void shell(ITEM[] a, int l, int r)
{
int h;
for (h = 1; h <= (r-l)/9; h = 3*h+1);
for ( ; h > 0; h /= 3)
for (int i = l+h; i <= r; i++)
{
int j = i;
ITEM v = a[i];
while (j >= l+h && less(v, a[j-h]))
{
a[j] = a[j-h];
j -= h;
}
a[j] = v;
}
}
13
Mergesort
Divide-and-conquer.
To sort A[p .. r]:
1. Divide Step
Mergesort
2. Conquer Step
Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r].
3. Combine Step
Combine the elements back in A[p .. r] by merging
the two sorted subarrays A[p .. q] and A[q + 1 .. r]
into a sorted sequence. To accomplish this step,
we will define a procedure MERGE (A, p, q, r).
16
Heapsort
17
Heapsort
time
Can we build a heap for n given elements faster?
(O(n))
18
ITEM[] pq;
int N;
void makePQ(int maxN) {
pq = new ITEM[maxN+1];
N = 0;
}
boolean isEmpty() {
return N == 0;
}
void insert(ITEM v) {
pq[++N] = v; swim(N);
}
ITEM getmax() {
exch(1, N);
sink(1, N-1);
return pq[N--];
}
19
Heapsort
void sort(ITEM[] a, int l, int r) {
PQsort(a, l, r);
}
void PQsort(ITEM[] a, int l, int r) {
int k;
PQ pq = makePQ(r-l+1);
for (k = l; k <= r; k++)
pq.insert(a[k]);
for (k = r; k >= l; k--)
a[k] = pq.getmax();
}
DSA - lecture 11 - T.U. Cluj-Napoca - M. Joldos
20
Heapsort Example
Data set: 150 70 30 10 20 60 40 50 140 100 80 90 130 110
21
Heapsort Example
22
Heapsort Example
23
Heapsort Example
24
Heapsort Example
25
Heapsort Example
26
Heapsort Example
27
Heapsort Example
28
Quicksort
29
Quicksort Algorithm
30
Quicksort partitioning
Choose pivot here is E
scan from the left and stop at the S,
scan from the right and stop at the A
exchange the S and the A
Quicksort Operation
Pivot selection
Partition, recursive
call, pivot selection
DSA - lecture 11 - T.U. Cluj-Napoca - M. Joldos
32
Quicksort operation
Partition, recursive
call, base case
Recursive call, ,
base case, join
DSA - lecture 11 - T.U. Cluj-Napoca - M. Joldos
33
Quicksort operation
Partition, , recursive
call, base case
DSA - lecture 11 - T.U. Cluj-Napoca - M. Joldos
34
Quicksort operation
Join, join
35
The worst case for quick-sort occurs when the pivot is the
unique minimum or maximum element
One of L and G has size n 1 and the other has size 0
The running time is proportional to the sum n + (n 1) + +
2+1
Thus, the worst-case running time of quick-sort is O(n2)
36
Good call: the sizes of L and G are each less than 3s/4
Bad call: one of L and G has size greater than 3s/4
37
Therefore, we have
38
39
40
41
42
h lg (n / e)n
= n lg n n lg e
= (n lg n)
DSA - lecture 11 - T.U. Cluj-Napoca - M. Joldos
43
44
Counting Sort
A= 3
C= 3 0 1 2
Then we determine the number of elements which are
less than or equal to each of the keys by calculating
the prefix sums:
C= 3 0 4 6
DSA - lecture 11 - T.U. Cluj-Napoca - M. Joldos
45
Counting Sort
B=
C=
1
4
B=
C=
1
1
1
3
1
4
3
6
B=
C=
1
1
1
3
1
4
3
5
46
(k)
(1)
(n)
(1) ((1) (n)= (n))
(k)
(1) ((1) (n)= (n))
(n)
(1) ((1) (n)= (n))
(1) ((1) (n)= (n))
47
48
Radix Sort
21
14
14
17
75
75
21
14
17
75
49
Radix Sort
50
Bucket Sort
51
52
(1)
O(n)
(1) (i.e. total O(n))
O(n)
O(ni2) (i=0n-1 O(ni2))
O(n)
53
54
55
not.
56
57
58
Reading
AHU, chapter 8
Preiss, chapter: Sorting Algorithms and
Sorters
CLR, chapters 7 section 7.4, 8 sections 8.1,
8.2, 9, CLRS chapter 2, sect. 1, chapters 6,
7, 8
Notes
59