Advertisement
matt_mods

quick sort and insersion sort

Mar 18th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #########################################
  2. quick sort
  3. #########################################
  4. Quicksort(A as array, low as int, high as int){
  5. if (low < high){
  6. pivot_location = Partition(A,low,high)
  7. Quicksort(A,low, pivot_location)
  8. Quicksort(A, pivot_location + 1, high)
  9. }
  10. }
  11. Partition(A as array, low as int, high as int){
  12. pivot = A[low]
  13. leftwall = low
  14.  
  15. for i = low + 1 to high{
  16. if (A[i] < pivot) then{
  17. swap(A[i], A[leftwall + 1])
  18. leftwall = leftwall + 1
  19. }
  20. }
  21. swap(pivot,A[leftwall])
  22.  
  23. return (leftwall)}
  24.  
  25.  
  26.  
  27. ######################################
  28. insertion sort
  29. ######################################
  30.  
  31. INSERTION-SORT(A)
  32. for j ← 2 to length[A]
  33. do key ← A[j]
  34. Insert A[j] into the sorted sequence A[1 j - 1].
  35. i ← j - 1
  36. while i > 0 and A[i] > key
  37. do A[i + 1] ← A[i]
  38. i ← i - 1
  39. A[i + 1] ← key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement