Kaucsenta

Rendező algoritmusok

Nov 2nd, 2013 (edited)
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. Polynomial runtime algorithms:
  2.  
  3. Insertionsort(A)
  4. for j=2 to length[A]
  5. do key=A[j]
  6. >insert A[j] into the sorted sequence A[1...j-1]
  7. i=j-1
  8. while i>0 and A[i]>key
  9. do A[i+1]=A[i]
  10. i<i-1
  11. A[i+1]=key
  12.  
  13. Best case: O(n^2) Avarage case: O(n^2) Worst case: O(n^2)
  14.  
  15.  
  16. Selectionsort(A)
  17. for i = 0 to length[A] - 1
  18. for j = i+1 to length[A]
  19. if A[i] > A[j]
  20. Temp = A[i]
  21. A[i] = A[j]
  22. A[j] = Temp
  23. End If
  24. j=j+1
  25. i=i+1
  26.  
  27. Best case: O(n^2) Avarage case: O(n^2) Worst case: O(n^2)
  28.  
  29.  
  30. BubbleSort(A)
  31. repeat
  32. swapped = false
  33. for i = 1 to length(A) - 1 inclusive do:
  34. /* if this pair is out of order */
  35. if A[i-1] > A[i] then
  36. swap( A[i-1], A[i] )
  37. swapped = true
  38. end if
  39. end for
  40. until not swapped
  41. end procedure
  42.  
  43. Best case: O(n^2) Avarage case: O(n^2) Worst case: O(n^2)
  44. ________________________________________________________________________________________________
  45.  
  46. Linear runtime algorithms:
  47.  
  48. Countingsort(A,b,k)
  49. for i=1 to k
  50. do C[i]=0
  51. for j=1 to length[A]
  52. do C[A[j]]=C[A[j]]+1
  53. for i=2 to k
  54. do C[i]=C[i]+C[i-1]
  55. for j=length(A) downto 1
  56. do B[C[A[j]]]=A[j]
  57. C[A[j]]=C[A[j]]-1
  58.  
  59. Best case: O(n+k) Avarage case: O(n+k) Worst case: O(n+k)
  60.  
  61.  
  62. Radixsort(A, d)
  63. for i=1 to d
  64. do use a stable sort to sort array A on digit i
  65.  
  66. Best case: O(n) Avarage case: O(n) Worst case: O(n)
  67.  
  68.  
  69. Bucketsort(A)
  70. n=length(A)
  71. for i=1 to n
  72. do insert A[i] into list B[⌊nA[i]⌋]
  73. for i=0 to n-1
  74. do sort list B[i] with insertion sort concatenate the list B[i]s together in order
  75. return the concatenated lists
  76.  
  77. Best case: O(n) Avarage case: O(n) Worst case: O(n)
  78. ________________________________________________________________________________________________
  79.  
  80. N logN runtime algorithms:
  81.  
  82. Heapsort(A) {
  83. BuildHeap(A)
  84. for i = length(A) downto 2 {
  85. exchange A[1] <-> A[i]
  86. heapsize = heapsize -1
  87. Heapify(A, 1)
  88. }
  89.  
  90. BuildHeap(A) {
  91. heapsize = length(A)
  92. for i = floor( length/2 ) downto 1
  93. Heapify(A, i)
  94. }
  95.  
  96. Heapify(A, i) {
  97. le = left(i)
  98. ri = right(i)
  99. if (le<=heapsize) and (A[le]>A[i])
  100. largest=le
  101. else
  102. largest=i
  103. if (ri<=heapsize) and (A[ri]>A[largest])
  104. largest=ri
  105. if (largest != i) {
  106. exchange A[i] <-> A[largest]
  107. Heapify(A, largest)
  108. }
  109. }
  110.  
  111. Best case: O(n logn) Avarage case: O(n logn) Worst case: O(n logn)
  112.  
  113.  
  114. Quicksort(A,p,r){
  115. if p<r
  116. then q=Partition(A,p,r)
  117. Quicksort(A,p,q-1)
  118. Quicksort(A,q+1,r)
  119. }
  120.  
  121. Partition(A,p,r){
  122. x=A[r]
  123. i=p-1
  124. for j=p to r-1
  125. do if A[j]<=x
  126. then i=i+1;
  127. exchange A[i)<->A[j]
  128. exchange A[i+1]<->A[r]
  129. return i+1
  130. }
  131.  
  132. Best case: O(n logn) Avarage case: O(n logn) Worst case: O(n^2)
  133.  
  134.  
  135. Mergesort(A,p,r){
  136. ifp p<r
  137. then q=⌊(p+r)/2⌋
  138. Mergesort(A,p,q)
  139. Mergesort(A,q+1,r);
  140. Merge(A,p,q,r)
  141. }
  142.  
  143. Merge(A,p,q,r){
  144. n1=q-p+1
  145. n2=r-q
  146. for i=1 to n1
  147. do L[i]=A[p+i-1]
  148. for j=1 to n2
  149. do R[j]=A[q+j]
  150. L[n1+1]=(infinity)
  151. L[n2+1]=(infinity)
  152. i=1;
  153. j=1;
  154. for k=p to r
  155. do if L[i] <= R[j]
  156. then A[k]=L[i]
  157. i=i+1
  158. else A[k]=R[j]
  159. j=j+1
  160. }
  161.  
  162. Best case: O(n logn) Avarage case: O(n logn) Worst case: O(n logn)
Add Comment
Please, Sign In to add comment