Лекция №10. Сортировка. Часть №2. Предмет "Структуры и алгоритмы обработки данных"

  • View
    1.295

  • Download
    2

Embed Size (px)

Transcript

  • 1. . .. : . 2. , www.grebenshikov.ru

2. ( ) 1. [1 . . . m] [m + 1 . . . N ], i, j : 1 i m m < j N ai aj 2. . m . 1 3. QuickSort(A, p, r) 1 if p < r 2 then q P artition(A, p, r) 3Quicksort(A, p, q 1) 4Quicksort(A, q + 1, r)2 4. Partition(A, p, r) 1 x A[r] 2 ip1 3 for j p to r 1 4 do if A[i] x 5 then i i + 1 6 A[i] A[j] 7 A[i + 1] A[r] 8 return i + 1 3 5. 4 6. (1 2)5 7. (2 2)6 8. ( ), . : .T (n) = T (0) + T (n 1) + (n) = (1) + T (n 1) + (n) = T (n 1) + (n) = (n2)C(N ) = M (N ) 7 9. ( ) n T (n) = ( c k) + n (1) = (n2 + n) = (n2)k=18 10. ( ) : - .T (n) = 2T (n/2) + (n) = (n log(n))9 11. - . -. , . - . 10 12. ! RandomizedPartition(A, p, r) 1 i Random(p, r) 2 A[r] A[i] 3 return P artition(A, p, r)RandomizedQuickSort(A, p, r) 1 if p < r 2 then q RandomizedP artition(A, p, r) 3RandomizedQuicksort(A, p, q 1) 4RandomizedQuicksort(A, q + 1, r)11 13. T (n) = . .1, (k : n k 1) : Xk =0, 1 E[Xk ] = 0 P r {Xk = 0} + 1 P r {Xk = 1} = P r {Xk = 1} = n12 14. T (0) + T (n 1) + (n), (0 : n 1)T (1) + T (n 2) + (n),(1 : n 2) T (n) =. . .T (n 1) + T (0) + (n), (n 1 : 0) n1 = Xk (T (k) + T (n k 1) + (n)) k=013 15. n1 E[T (n)] = E[ Xk (T (k) + T (n k 1) + (n))] k=0n1= E[Xk ] E[ T (k) + T (n k 1) + (n))]k=0 n111 n11 n1= E[T (k)] + E[T (n k 1)] + (n)n k=0n k=0n k=02 n1= E[T (k)] + (n)n k=02 n1= E[T (k)] + (n)n k=2 14 16. , E[T (n)] a n log(n), a const, a > 0. : a, E[T (n)] a n log(n) n.n11 2 1 , k log(k) n log(n) n2 k=22 815 17. : 2 n1 E[T (n)] a k log(k) + (n)n k=22a 1 21 ( n log(n) n2) + (n) n 2 a8 n =a n log(n) ( (n))4 na a n log(n), > (n)4 16 18. . , - , N c N log2N c = 0 N .T (n) > c N log2N , c = 0 N 17 19. . N - N !. - - , , . 18 20. 3- 19 21. . N N! N22 h log2(N !) N log2 N N log2N 22 4 20 22. . n - , [0, k], k - . k = O(n), T (n) = (n) 21 23. CountingSort(A, B, k)1 for i 0 to k2 do C[i] 03 for j 1 to length[A]4 do C[A[j]] C[A[j]] + 15 C[i] i6 for i 1 to k7 do C[i] C[i] + C[i 1]8 C[i] i9 for j length[A] downto 1 10 do B[C[A[j]]] A[j] 11C[A[j]] C[A[j]] 122 24. ?23 25. : 24 26. .., .. : -, : . / -. . -. , 2006. .2. 58 . .42-47. ., ., . . - . : , 2000..235-257. ., ., ., . -: , 2- . - . : - , 2007. .198-239.25 27. , , 3. , 2- . - . : , 2000. .192-203.