Uploaded by juniorbaidoo7

Linear Search Bubble Sort

advertisement
//Declaring all Variables, Constant and Arrays
DECLARE ArrayList : ARRAY [1:20] OF INTEGER
DECLARE SearchNum : INTEGER
DECLARE Found : BOOLEAN
DECLARE ListInput : INTEGER
DECLARE Start : INTEGER
CONSTANT ListLen <- 20
DECLARE Swapped : BOOLEAN
DECLARE Comparison : INTEGER
DECLARE Temp : INTEGER
//Initializing Variables to be used
Found <- FALSE
Start <- 1
Swapped <- TRUE
Comparison <- ListLen - 1
//A loop to accept user input and append to the list
FOR i <- 1 TO 20
OUTPUT "Enter a number to store in the list: "
INPUT ListInput
ArrayList[i] <- ListInput
NEXT i
//Asking user to enter the number they want to search for in the list
OUTPUT "What number do you want to search for? "
INPUT SearchNum
//Performing a linear search in the list
WHILE (Found = FALSE AND Start <= ListLen) DO
IF(SearchNum = ArrayList[Start]) THEN
Found <- TRUE
ELSE
Start <- Start + 1
ENDIF
ENDWHILE
//Checking if number was found during the search
IF(Found <- TRUE)THEN
OUTPUT SearchNum, "Found In the List At Index ", Start
ELSE
OUTPUT SearchNum, "Not Found In The List"
ENDIF
//Sorting the Items in the list in ascending order
WHILE (Swapped = TRUE AND Comparison > 0) DO
Swapped <- FALSE
FOR x <- 1 TO Comparison
IF(ArrayList[x] > ArrayList[x+1]) THEN
Temp <- ArrayList[x]
ArrayList[x] <- ArrayList[x+1]
ArrayList[x+1] <- Temp
Swapped <- TRUE
ENDIF
NEXT x
Comparison <- Comparison + 1
ENDWHILE
//Outputing the sorted list
OUTPUT ArrayList
Download