PSUDOCODE

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Sequential search:

Is a search that go through all the arrays items (one by one), until it
reaches the required element.
1. Have your array.
2. Have your flag/found.
3. Make a loop.
4. Make if condition to search for the required element.
5. End your If condition and then your loop.
Y = {1 , 2 , 3 , 4 , 5} // this is the array
INPUT X
Found = false // this is the flag/found
i=0
LOOP I FROM 0 TO 4 // the loop
IF (Y[i] == X) // to check each item in the array.
THEN 
Found = true  // if the element is found
PRINT X, “Found at :”, I // the element will be printed and its
index/position
ENDIF
ENDLOOP
IF (Found ==false) // if the element is still not found
THEN 
PRINT X, “Not Found” // it will print out that the element is Not
Found

Binary search: (you need flag )


Will be there haaaaaaa
A search can perform only on sorted elements. It works by repeatedly
dividing the elements in half, until you have narrowed down the possible
locations to just one.
1. Have your array
2. Create
 Min
 Max
 Mid
 Found \\ flag
 Ans
3. Make a loop
4. Take the middle
5. Until the item is located
6. Print the item

Multiples = {2,4,6,8,10,12,14,16,18,20} // the array


TARGET = 8 // wanted elements
MIN = 0 // the minimum value in the array
HIGH = 9  // maximum number of values in the array
FOUND = false // Our flag/found
ANS = 0 // Is the temporary variable for the target
MID =0 // the middle of the array
IOOP WHILE FOUND != true AND MIN <= HIGH // if still the target is
not found and minimum is less than or equal to the max
MID = ((MIN + HIGH) div 2) // to find the middle of the array
IF Multiples [MID] = TARGET
THEN
FOUND = true
ANS = MID
else if TARGET > Multiples [MID] then // if the target is greater than
the mid then (NOTE: the array is in ascending order) the search will
perform again in the right side.
MIN = MID + 1 // search in the right side
else 
HIGH = MID – 1 // search in the left side
end if
end WHILE
if FOUND = true then
output TARGET , "FOUND AT ARRAY INDEX" , ANS
else
output TARGET ," was not found"
end if

Bubble sort: it will come in the final 2nd


term
1. Search through entire list N times
2. At each position, compare DATA[X] to DATA[X+1]
3.  swap items X and X+1 if they are not in order
4. continue to end of list
ELEMENTS = [1, 663, 8, 2, 4, 1, 22, 66, 20,122] // the array

Loop I from 0 to 9 // Number of elements - 1


Loop J from 0 to (8 - I) // I subtracted from 8 (number of
elements - 2) because the last one is already done/sorted {N – 2 - i}
If ELEMENTS[J] > ELEMENTS[J + 1] // to check
element I with element j
THEN
TEMP = ELEMENTS [J] // element [j] will
be in the temporary variable
ELEMENTS [J] = ELEMENTS [J+1] //
element[j] will be swapped with the next
element
ELEMENTS [J+1] = TEMP // swap element j =
1 with the previous one
End if
End loop
End loop
Output "Sorted elements"
Loop E from 0 to 9
Output ELEMENTS [E]
End loop
Selection sort:
Compare bubble sort and selection sort
1. Find the BEST item in list and swap it to beginning of list
2. find BEST item starting in second position and swap into 2nd position
3. repeat at 3rd position, then 4th position, etc
4. Requires N passes, but each pass is 1 item shorter

ELEMENT = {1, 5, 6, 8, 5, 4, 94, 9, 974, 97, 84}


Min = 0
I=0
TEMP = 0
LOOP MIN from 0 to 9 // it reaches the element before the last
I = Min
LOOP J from MIN + 1 to 10
If ELEMENT[J] < ELEMENT[I]
I=J
End if
Endloop
TEMP = ELEMNT[I]
ELEMENT[I] = ELEMENTS [MIN]
ELEMENTS [MIN] = TEMP
end loop
output “SORTED ARRAY”
loop C from 0 to 10
output ELEMENTSIC]
end loop
MIN/MAX average:
Min = INTEGERS[0]
Max = INTEGERS[0]
Avg = 0
Sum = 0
Loop i from 0 to 19
If (INTEGERS[i] < Min)
Min = INTEGERS[i]
elseIF (INTEGERS[i] > Max )
THEN 
MAX = INTEGERS[i]
Sum =Sum + INTEGERS[i]
ENDIF
ENDIF
ENDLOOP
Avg = Sum / 20
PRINT Max, Min, Avg

OCCURRENCE:
NUMBERS = {76,89,22,42,13,100,99,14,30}
LOOP i from 0 to N-1
LOOP J FROM 0 to N-1
X=1
IF (NUMBERS[i] == NUMBERS[J+1]
X = X +1
Frequency[i] = X
ELSE 
Frequency[i] = X
ENDIF
ENDLOOP
ENDLOOP

Loop i from 0 to N-1


PRINT NUMBERS[i], Frequency[i]
ENDLOOP

Collection:
.addItem( data ) = add data item to the collection 
.resetNext() = start at the beginning 
.hasNext() → tells whether there is another item in the list
 .getNext() → retrieves a data item from the collection 
.isEmpty() → check whether collection is empty

Given a collection of data, write a pseudo code to copy the positive elements of the collection to
an array. Data.resetNext()

Data.resetNext()
I=0
Loop while Data.hasNext()   // to check if there are any more items
DataTemp = Data.getNext()   // to get the next item 
IF DataTemp > 0
THEN 
ARRAY1[i] = DataTemp
ENDIF
ENDLOOP
Loop i form 1 to Array1.length() 1 
PRINT Data[i]
ENDLOOP
- Sub procedure
o Advantages
o Disadvantages
o Use the worksheet from GCR
o GO TO COMPUTER HUB
- Sub program
o Same as sub procedure
o Use worksheet in GCR
- Given flow chart and determine the output
- Tracing table
- Drawing Gantt chart
o Drawing
o Use of Gantt chart
o You will write the scenario
o THINKIN AHEAD ON GCR
- Construct a pseudocode
o Loops
o Combinations
o Max and min and avg
o Occurrence

You might also like