3.3 Algorithms Flashcards
What is a recursive algorithm?
An algorithm that calls upon itself until a base case is met.
An elegant method as it is a** divide and conquer algorithm** that breaks down tth problem into sub problems and **combining them to form a final solution **
Simpler and more compact than iteration
What is a general and base case?
General case is a smaller instance of the same problem
Base case is what causes the algorithm to stop
Benefits and drawbacks to recursion
+ Natural way to process data
+ fewer lines of code, easier to read
- utilises stacks, which have limited size and stack overflows can occur
- takes longer to process due to stacking
Quicksort
divide and conquer sorting algorithm. very efficient and fast and often considered better than merge sort due to using less memory but this isn’t really the case as it depends on other factors
Quicksort Pseudocode
FUNCTION quick_sort(items, start, end)
IF start >= end THEN RETURN ELSE pivot_value = items[start] low_mark = start + 1 high_end = end finished = False WHILE finished = False WHILE low_mark <= high_mark AND items[low_mark] <= pivot_value low_mark = low_mark +1 ENDWHILE WHILE low_mark <= high_mark AND items[high_mark] >= pivot_value high_mark = high_mark - 1 ENDWHILE IF low_mark < high_mark THEN temp = items[low_mark] items[low_mark] = items[high_mark] items[high_mark] = temp ELSE finished = TRUE ENDIF ENDWHILE temp = items[start] items[start] = items[high_mark] items[high_mark] = temp quick_sort(items, start, high_mark - 1) quick_sort(items, high_mark + 1, end) ENDIF RETURN items
ENDFUNCTION
Compare sorting algorithms and their space and time complexity
Linear search, goes thorugh each item until item is found
Time: O(n)
Space O(1)
Binary search: divides list into two, compare and discards side if the item isnt in it, repeats
Time: O(log(n))
Space: O(1)
Bubble sort: contirunously compares items and does passes until item is found:
Time: O(n^2)
Space: O(1)
Insertion Sort: creates a seperate sorted sub list
Time: O(n^2)
Space: O(1)
Merge sort: seperate to individual elemetns, pair up in sorted order, repeat
Time: O(nlog(n))
Space: O(n)
Quick sort: divide and conquer algorithm, first element becomes pivot point, move item left or right dependign on size, do not sort sub list and repeat for all sub lists until sorted
Time: O(nlog(n))
SpaceL O(log(n))
Algorithm
An unambigous set of instructions to solve a problem. Example being flowchart or psuedocode
Iteration
Repeating a set of instructions until a condtion is met
+easier to look at and debug
+ generally uses less memory than recursive too.
Linear search pseudocode
declare myArray[0-999] is integer searchValue is integer found is boolean set found = False for i = 0 to len(myArray) if searchValue == myArray[i] set found = True output "searchValue found at position" i end if next i if found = False output "searchValue not found" end if
Binary search pseudocode
declare myArray[0 to 999] is integer start is integer end is integer found is boolean mid is integer set start = 0 set end = len(myArray) set found = False input searchValue repeat set mid =(start +end) DIV 2 if searchValue = myArray[mid] then set found = TRUE output "searchValue found at position" mid if searchValue > my array[mid] then set start = mid + 1 end if if searchValue < myArray[mid] then set end = mid - 1 end if until (found= True) OR (end < start)
Bubble Sort pseudocode
declare myArray[0-999] is integer temp is integer swapped is boolean repeat set swapped = False for i = 0 to (len(myArray)- 1) if myArray[i] <= myArray[i+1] temp = myArray[i] myArray[i+1] = myArray[i] myArray[i+1] = temp set swapped True end if end for until swapped = False
Purpose of data validation and verification
Validation aims to ensrue that data is sensible,reasoonable, complete and within acceptable boundaries
Only proves the data entred has a reasonable value, cant prove its what the user intended to enter.
An example of such would be a presence checks, range checks etc
Verfication is used to ensure data entered by user is consistent, like double entry of passwords upon setting
Both are used to minmises human error