2 methods of searching
Binary search
Linear search
3 types of sorting
Bubble
Merge
Insertion
How does binary search work
What does the binary search code look like
Searchitem = 11
list = [“2”, “3”, “4”, “6”, “8”]
Last = length(list)-1
First = 0
While first <= last and found = false:
Midpoint = (first + last)/2
If list[midpoint] == searchitem:
Found = true
Else:
If searchitem < list[midpoint]:
Last = midpoint - 1
Else:
First = midpoint + 1How does linear search work
Code for linear search
Searchitem = 2
Counter = 0
Found = false
While counter < list and found = false:
If counter == searchitem:
Found = true
Else:
Counter = counter + 1
Disadvantages of linear search
Can take a while as it goes through every value
Advantage of linear search
Works on any data set wether in order or not
How does a bubble sort work
Code for bubble sort
counter = 0
swapped = True
swaps = 0
length = list.length
while swapped == True:
while counter < length-1:
if list[counter] > list[counter+1]:
temp = list[counter]
list[counter] = list[counter+1]
list[counter+1] = temp
swaps = swaps + 1
counter = counter + 1
if swaps == 0 then
swapped = False
else:
swaps = 0
counter = 0How does insertion sort work
An insertion sort compares values in turn, starting with the second value in the list. If this value is greater than the value to the left of it, no changes are made. Otherwise this value is repeatedly moved left until it meets a value that is less than it. The sort process then starts again with the next value. This continues until the end of the list is reached.
Code for insertion sort
For i from 0 to length(list)-1:
Key = list[i]
j = i - 1
while j > -1 and key < list[j]
list[j+1] = list[j]
j = j + 1
list[j + 1]= key
Next i
What is a flow diagram?
A flow diagram is a diagram that shows an overview of a program. Flow diagrams normally use standard symbols to represent the different types of instruction. These symbols are used to construct the flowchart and show the step-by-step solution to the problem. Flow diagrams are sometimes known as flowcharts.
Advantages of flow diagrams
Disadvantages of using flow diagrams
Pros of merge sort
Quickest
Most efficient
Cons of merge sort
Difficult to understand
Pros of bubble sort
Simple to understand
Cons of bubble sort
Slow, inefficient, lots of steps
Pros of insertion sort
Fast, good for small data sets
Cons of insertion sort
Remembering pivot point/ key