Professional Documents
Culture Documents
=
T(N) = 2 T(N/2) + cN
T(N) = cN log N + N = O(N log N)
CE, KMITL Data Structures and Algorithms 41
Runtime Quick Sort (cont) Runtime Quick Sort (cont)
Assume each of the sizes for S
1
are Assume each of the sizes for S
1
are
equally likely. 0 s |S
1
| s N-1.
1 N
0 i
cN 1)] - i - T(N T(i) [
N
1
T(N) + |
.
|
\
|
+ =
=
1 N
0 i
cN T(i)
N
2
+ |
.
|
\
|
=
=
2
1 N
0 i
cN T(i) 2 T(N) N + |
.
|
\
|
=
=
2
2 N
0 i
1) - c(N T(i) 2 1) T(N 1) (N + =
. \
=
CE, KMITL Data Structures and Algorithms 42
0 i
Runtime Quick Sort (cont) Runtime Quick Sort (cont)
2cN 1) T(N 1) (N T(N) N + + = ) ( ) ( ( )
1 N
2c
N
1) T(N
1 N
T(N)
+
+
=
+
N(N+1)
N
2c
1 - N
2) T(N
N
1) - T(N
+
=
( )
1 N
2c
2 - N
3) T(N
1 N
2) - T(N
3
2c
2
) 1 T(
3
T(2)
+ =
+
=
+ =
+
1 N
3 i
i
1
2c
2
T(1)
1 N
T(N)
N) l O(N T(N)
~log
e
(N+1) 3/2
CE, KMITL Data Structures and Algorithms 43
N) log O(N T(N) =
Pseudocode Merge Sort Pseudocode Merge Sort
mergesort(list first last) { mergesort(list, first, last) {
if( first < last )
mid = (first + last)/2; ( ) ;
// Sort the 1
st
half of the list
mergesort(list, first, mid);
// S t th 2
nd
h lf f th li t // Sort the 2
nd
half of the list
mergesort(list, mid+1, last);
// Merge the 2 sorted halves // Merge the 2 sorted halves
merge(list, first, mid, last);
end if
}
CE, KMITL Data Structures and Algorithms 44
Pseudocode Merge Sort (cont) Pseudocode Merge Sort (cont)
merge(list, first, mid, last) {
// Initialize the first and last indices of our subarrays y
indexA = first
lastA = mid
indexB = mid+1
lastB = last
indexC = indexA // Index into our temp array
CE, KMITL Data Structures and Algorithms 45
Pseudocode Merge Sort (cont) Pseudocode Merge Sort (cont)
// Start the merging
loop( indexA <= lastA AND indexB <= lastB )
if( list[indexA] < list[indexB] ) ( [ ] [ ] )
tempArray[indexC] = list[indexA]
indexA = indexA + 1
else else
tempArray[indexC] = list[indexB]
indexB = indexB + 1
d f end if
indexC = indexC + 1;
end loopp
CE, KMITL Data Structures and Algorithms 46
Merge Sort Pseudocode (cont) Merge Sort Pseudocode (cont)
// At this point one of our subarrays is empty // At this point, one of our subarrays is empty
// Now go through and copy any remaining items
// from the non-empty array into our temp array
loop (indexA <= lastA)
tempArray[indexC] = list[indexA]
indexA = indexA + 1 indexA indexA + 1
indexC = indexC + 1
end loop
loop (indexB <= lastB ) loop (indexB <= lastB )
tempArray[indexC] = list[indexB]
indexB = indexB + 1
indexC = indexC + 1
end loop
CE, KMITL Data Structures and Algorithms 47
Merge Sort Pseudocode (cont) Merge Sort Pseudocode (cont)
// Finally, we copy our temp array back into
// our original array
indexC = first
loop (indexC <= last)
li t[i d C] t A [i d C] list[indexC] = tempArray[indexC]
indexC = indexC + 1
end loop end loop
}
CE, KMITL Data Structures and Algorithms 48
Runtime Merge Sort Runtime Merge Sort
Let T(N) be the running time of Let T(N) be the running time of
mergesort on N items.
1 T(1) =
N T(N/2) 2 T(N)
1 T(1)
+ =
1
T(N/2) T(N)
+
1
/4
T(N/4)
/2
T(N/2)
1
N/2
( )
N
( )
+ =
+ =
1
N/8
T(N/8)
N/4
T(N/4)
N/4 N/2
+ =
1
T(1) T(2)
...
N/8 N/4
+ = 1
1 2
+ =
CE, KMITL 49 Data Structures and Algorithms
Runtime Merge Sort Runtime Merge Sort
N log
1
T(1)
N
T(N)
+ =
1 N
N log N N T(N) + =
Bruteforce: Brute force:
N T(N/2) 2 T(N) + =
N/2) T(N/4) (2 2 T(N/2) 2 + =
N T(N/4) 4
N/2) T(N/4) (2 2 T(N/2) 2
+ =
+ =
N 2 T(N/4) 4 T(N) + =
...
N 3 T(N/8) 8 T(N)
( ) ( )
+ =
kN ) T(N/2 2 T(N)
...
k k
+ =
Let k=logN
N log N T(1) N T(N) + =
Let k log N
CE, KMITL 50 Data Structures and Algorithms