Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROCEDURE InsertionSort(ByReference Nums : ARRAY OF INTEGER)
- DECLARE Key : INTEGER
- DECLARE I, J : INTEGER
- FOR I <- 1 to LENGTH(Nums) - 1
- Key <- Nums(i)
- J <- I - 1
- WHILE J >= 0
- IF Key < Nums(J)
- THEN
- Nums(J + 1) = Nums(J)
- J = J - 1
- ELSE
- EXIT WHILE
- END IF
- END WHILE
- Nums(J + 1) = Key
- END FOR
- END PROCEDURE
- DECLARE Nums : ARRAY [0 : 9] OF INTEGER
- DECLARE x, xIndex : INTEGER
- Nums <- (89, 34, 90, 56, 76, 109, 67, 178, 23, 309)
- InsertionSort(Nums)
- x <- 90
- xIndex <- BinarySearch(Nums, x)
- IF xIndex = -1
- THEN
- OUTPUT "Element not found"
- ELSE
- OUTPUT "Element found"
- END IF
- x <- 93
- xIndex <- BinarySearch(Nums, x)
- IF xIndex = -1
- THEN
- OUTPUT "Element not found"
- ELSE
- OUTPUT "Element found"
- END IF
- FUNCTION BinarySearch(Nums : ARRAY OF INTEGER, x : INTEGER) : INTEGER
- DECLARE StartPointer, EndPointer, Mid : INTEGER
- StartPointer <- 0
- EndPointer <- LENGTH(Nums) - 1
- WHILE EndPointer >= StartPointer
- Mid <- FLOOR((StartPointer + EndPointer) / 2)
- IF x > Nums(Mid)
- THEN
- StartPointer <- mid + 1
- ELSE IF x < Nums(mid)
- THEN
- EndPointer <- mid - 1
- ELSE
- RETURN Mid
- END IF
- END WHILE
- RETURN -1
- END FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement