U1+2 Fundamentals of Algorithms

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/116

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

117 Terms

1
New cards

define algorithm

a sequence of steps or stages that can be followed to complete a task

2
New cards

decomposition

breaking a problem into a number of sub-problems,

so that each sub-problem accomplishes an identifiable task,

which might itself be further subdivided.

3
New cards

abstraction

the process of removing unnecessary detail from a problem

4
New cards

flowchart symbol input/output

parallelogram

5
New cards

flowchart symbol process

rectangle

6
New cards

flowchart symbol subroutine

box with lines

7
New cards

flowchart symbol start/stop

oval kinda thing

8
New cards

flowchart symbol decision

diamond decision!

9
New cards

dry running an algorithm

inputting things into the variables and doing any processing, to determine the purpose of the simple algorithm

10
New cards

methods of searching

linear and binary search

11
New cards

methods of sorting

bubble and merge sorts

12
New cards

how does a linear search algorithm work

  1. Identify a search term. eg 2

  2. Look at the first item in the list. 129348

  3. Compare the item with the search term. 2 compared to 1 - not found

  4. Is the current item the same as the search term? If so, the item has been found. If not, move to the next item. 2 compared to 2 - found

  5. Repeat from step two until the last item in the list has been reached. yes

  6. If the end of the list has been reached and the search term has not been found, then the search term is not in the list and the algorithm can stop. 0 compared to 8 - not found

13
New cards

look at this pseudocode for linear search

<p></p>
14
New cards

how does a binary search algorithm work

  1. Start by setting the counter to the middle position in the list.

  2. If the value held there is a match, the search ends.

  3. If the value at the midpoint is less than the value to be found, the list is divided in half, the lower half of the list is ignored and the search keeps to the upper half of the list.

  4. Otherwise, if the value at the midpoint is greater than the value to be found, the upper half of the list is ignored and the search keeps to the lower half of the list.

  5. The search moves to the midpoint of the remaining items. Steps 2-4 continue until a match is made or there are no more items to be found.

15
New cards

look at this pseudocode for binary search

knowt flashcard image
16
New cards

PP2019 Perform a binary search on 1 6 14 21 27 31 35 and write what comparisons you would make to find 30 [3]

compare 21 with 30 [1]

compare 31 with 30 [1]

compare 27 with 30 [1]

17
New cards

which search or sort has a condition that must be met, and what is it

binary search the list must be already ordered

18
New cards

benefits of linear search

  • for binary, items must already be in order

  • simplicity

  • quicker to add new items as items can be added at the end rather than in order

  • efficient for smaller lists

19
New cards

benefits of binary search AND WHY

  • efficient for large lists

  • necause GENERALLY FEWER COMPARISONS on avg

20
New cards

if you have 2n items in a list, what’s the maximum number of items you need to compare for a binary search

n+1

e.g find 9 in 123456789

comp 5 with 9

comp 7 with 9

comp 8 with 9

comp 9 with 9

9 items, 16 = 2 ^4, 4+1 =5 oh.

21
New cards

how does a merge sort algorithm work (ideal answer from a picture from Mrs A)

  1. first the algorithm splits the list in half

    9284 1376

  2. it continues to split each new list in half until every sublist has just one element

    9 2 8 4 1 3 7 6

  3. working in pairs of lists, the algorithm compares the first item of each sublist and the next, and creates a new list, with the lowest values first

    29 48 13 67

  4. it continues checking each item in the list and adding the lowest value into the new list until the list is empty

    2489 1367

  5. it continues this process from comparing pairs until all the items are in a newly ordered list

    12346789

22
New cards

how does a bubble sort algorithm work

  1. Look at the first number in the list. 9 1 4 2 3

  2. Compare the current number with the next number. 9>1

  3. Is the next number smaller than the current number? If so, swap the two numbers around. If not, do not swap. 1 9 4 2 3

  4. Move to the next number along in the list and make this the current number.

    1 9 4 2 3

  5. Repeat from step 2 until the last number in the list has been reached. 1 4 2 3 9

  6. If any numbers were swapped, repeat again from step 1. 1 2 3 4 9

  7. If the end of the list is reached without any swaps being made, then the list is ordered and the algorithm can stop. 12349

23
New cards

look at this bubble sort pseudocode

knowt flashcard image
24
New cards

if there are n items in a bubble sort, how many maximum passes will we make

n-1

25
New cards

advantages of bubble sort

  • simple algorithm so easy to implement on a computer

  • efficient way to check if the list is in order already

  • does not use much memory as sort is done using original list

26
New cards

advantages of merge sort

  • faster for very large lists

27
New cards

VB NET for a decimal

decimal, single, double

28
New cards

VB NET for whole number

integer. short, long

29
New cards

convert string to decimal VB

CDec

30
New cards

convert string to decimal PSEUDOCODE

string to decimal

31
New cards

convert integer to string PSEU

number_to_string()

32
New cards

define data type

what kind of value the variable holds

33
New cards

make gravity a constant holding 9.8 VB

Const Gravity as single = 9.8

34
New cards

make gravity a constant as 9.8 PSEU

Constant GRAVITY = 9.8

35
New cards

Output hello and a string called name PSEU

OUTPUT “hello”, name

36
New cards

output hello and a string called name VB

Console.WriteLine(“Hello” & name)

37
New cards

assign input to name PSEU

name ← USERINPUT

38
New cards

assign input to name VB

Name = Console.ReadLine()

39
New cards

concatenation of string1 and string2 to make string3 PSEU

string3 ← string1 & string2

40
New cards

concatenation of string1 and string2 to make string3 VB

string3 ← string1 7 string2 (same)

41
New cards

check if input number is a number P

if number.isnumeric = true

42
New cards

check if input number is a number VB

If isnumeric(number) then

43
New cards

definite iteration a counter to do something 5 times P

for counter = 1 to 5 do

end for

44
New cards

definite iteration a counter to do something 5 times VB

for counter = 1 to 5

next

45
New cards

indefinite iteration condition check first, general, P

While …

EndWhile

46
New cards

indefinite iteration condition check first, general, VB

do while …

loop

47
New cards

indefinite iteration condition check last, general, P

repeat

until …

48
New cards

indefinite iteration condition check last, general, VB

do

loop until …

49
New cards

simple selection

is the same in al of them so dwww

50
New cards

is there a gap between “else if” for VB, P, both or neither

neitherr

51
New cards

bubble sort algorithm though you dont need to know it !

knowt flashcard image
52
New cards

difference between the two types of subroutine

  • procedures do not send any data back to the main program

  • functions must return a value to the program

53
New cards

declaring a new procedure PSU

Procedure nameofprocedure(variable)

54
New cards

declaring a new procedure VB

Sub nameofprocedure(variable)

end sub

55
New cards

declaring a function PSU

Function nameoffunction(variable)

Return

End function

56
New cards

declaring functions VB

Sub nameoffunction(variable)

return …

end sub

57
New cards

what is div

integer division, how many full times x goes into y

58
New cards

what is mod

remainder (MODremainDer)

59
New cards

not equal to in PSU

<> it is the same in VB

60
New cards

less that or equal to in PSU (or VB its the same)

<=

61
New cards

more that or equal To in VB or PSU its the same

>=

62
New cards

how to remember where the equal sign is

it is alwaus on the right rifht right

63
New cards

generating a random number PSU

Random.randint(start, stop)

64
New cards

generating a random number VB

Imports System.math

randomize()

dim randomnum as integer

randomnum = CINT(*6rnd()+1)

65
New cards

length of string PSU

Len(string)

66
New cards

Length of string VB

Len(string)

OR

string.length()

67
New cards

position of first character in string for VB (or PSU is the same)

String(0)

68
New cards

substring PSU

String(start,end)

69
New cards

substirng VB

String.substring(start, length)

70
New cards

convert character into ASCII code PSU

Char_to_code(variable)

71
New cards

convert character into ASCII code VB

ASC(variable)

72
New cards

convert ASCII code into character PSU

code_to_char(variable)

73
New cards

convert ASCII code into character VB

CHR(variable)

74
New cards

convert to uppercase PSU

Convert_to_upper(variable)

75
New cards

convert to uppercase VB

Variable.ToUpper

76
New cards

convert to lowercase PSU

Convert_to_lower(variable)

77
New cards

convert to lowercase VB

Variable.ToLower

78
New cards

get last character PSU

Variable[last]

i rllly dont believe this

79
New cards

get last character VB

Variable(variable.length-1)

80
New cards

1D array with 3 strings PSU

New 1D array Words[1] of string

OR

names ← [“abc”, “def”]

81
New cards

1D array with 3 strings VB

Dim words(2) as string

OR

dim words as new string() {“abc”, “def”}

82
New cards

2D array 3 people with their surname and first name PSU

dim words(3,2) as string

OR

words ← [ [“Red”,”Cap”], [“Yellow”,”Hoodie”], [“Blue”,”Mask”] ]

83
New cards

2D array for 3 people with their surname and first name VB

Dim words(3,2) as string

OR

Dim words = new string(,) { {“Red”,”Cap”}, {“Yellow”,”Hoodie”}, {“Blue”,”Mask”} }

84
New cards

what is a decimal in other words

real, float

85
New cards

three combining principles

selection, sequence, iteration

86
New cards

what type of loop is definite iteration (CC), and what loop can be used for it

counter controlled

for loop

87
New cards

what type of loop is indefinite iteration (CC), and what loop can be used for it

condition-controlled

PSU: while-endwhile, repeat-until

VB: while-end while, do-loop, do-loop until

88
New cards

input and output in PSU

USERINPUT and OUTPUT

89
New cards

input and output in VB

Console.ReadLine()

Console.WriteLine

90
New cards

generate a random number between 1 and 10 in PSU

RANDOM_INT(1,10)

91
New cards

generating a random number between 1 and 10 in VB is much easier…

Dim random As New Random()

Dim randomNumber As Integer = random.Next(1, 11)

92
New cards

define subroutine

a named ‘out of line’ block of code that may be executed (called) by simply writing its name in a program statement.

93
New cards

advantages of subroutines

  • When a problem is decomposed, each problem to be solved can be written within its own subroutine, 

    • which makes it easier to solve

  • Each subroutine can be tested separately, 

    • which can save time when debugging

  • They can be used repeatedly within the same program without needing to be written more than once

    • This saves on memory space.

    • It saves on maintenance time. If there is a change to be made, it only needs to be changed within one subroutine.

  • A team of programmers can work on different subroutines at the same time

    • which speeds up the finish time

    • Subroutines can be stored in a separate file called a “library” and then be used by other programs.

94
New cards

what is a local variable and what can it do

Know that subroutines may declare their own variables, called local variables, and that local variables usually:

  • only exist while the subroutine is executing

  • are only accessible within the subroutine.

95
New cards

what is a global variable

a variable that can be directly used by any subroutine in the program. is used anywhere in the program and is declared outside the subroutine

96
New cards

difference between local and global variables

global- If we change the value inside the variable from within a subroutine, the original value in the main program also changes.

local - use a copy of the value inside the variable so that we don't affect the original value. (like screenshotting a pic before editing the screenshot so you always have the original)

97
New cards

advantages of local variables

any variables created inside the subroutine will be deleted once the subroutine ends

98
New cards

disadvantages of local variables

we use one more memory location each time we pass a variable into a parameter,

99
New cards

what is a structured approach to programming

a way of writing a program that includes moduralised the code, created interfaces, decomposition and subroutines (including parameters, return values and local variables if a function)

100
New cards

advantages of a structured approach to programming

  • Application programs are easier to read and understand.

  • Application programs are less likely to contain logic errors.

  • Errors are more easily found.

  • Higher productivity during application program development.

  • Improved application program design.