Advertisement
nein_yards

Untitled

Sep 29th, 2020 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. PROCEDURE InsertionSort(ByReference Nums : ARRAY OF INTEGER)
  2. DECLARE Key : INTEGER
  3. DECLARE I, J : INTEGER
  4. FOR I <- 1 to LENGTH(Nums) - 1
  5. Key <- Nums(i)
  6. J <- I - 1
  7. WHILE J >= 0
  8. IF Key < Nums(J)
  9. THEN
  10. Nums(J + 1) = Nums(J)
  11. J = J - 1
  12. ELSE
  13. EXIT WHILE
  14. END IF
  15. END WHILE
  16. Nums(J + 1) = Key
  17. END FOR
  18. END PROCEDURE
  19.  
  20. DECLARE Nums : ARRAY [0 : 9] OF INTEGER
  21. DECLARE x, xIndex : INTEGER
  22.  
  23. Nums <- (89, 34, 90, 56, 76, 109, 67, 178, 23, 309)
  24. InsertionSort(Nums)
  25.  
  26. x <- 90
  27. xIndex <- BinarySearch(Nums, x)
  28.  
  29. IF xIndex = -1
  30. THEN
  31. OUTPUT "Element not found"
  32. ELSE
  33. OUTPUT "Element found"
  34. END IF
  35.  
  36. x <- 93
  37. xIndex <- BinarySearch(Nums, x)
  38.  
  39. IF xIndex = -1
  40. THEN
  41. OUTPUT "Element not found"
  42. ELSE
  43. OUTPUT "Element found"
  44. END IF
  45.  
  46.  
  47. FUNCTION BinarySearch(Nums : ARRAY OF INTEGER, x : INTEGER) : INTEGER
  48. DECLARE StartPointer, EndPointer, Mid : INTEGER
  49. StartPointer <- 0
  50. EndPointer <- LENGTH(Nums) - 1
  51.  
  52. WHILE EndPointer >= StartPointer
  53. Mid <- FLOOR((StartPointer + EndPointer) / 2)
  54. IF x > Nums(Mid)
  55. THEN
  56. StartPointer <- mid + 1
  57. ELSE IF x < Nums(mid)
  58. THEN
  59. EndPointer <- mid - 1
  60. ELSE
  61. RETURN Mid
  62. END IF
  63. END WHILE
  64. RETURN -1
  65. END FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement