Advanced Higher Comp coding Flashcards
EFFICIENT Bubble Sort
swapped = False ------------------------------------------- ------------------------------------------- ------------------------ --------------------------------- ---------------------------- ------------------- stopIndex-=1
EFFICIENT BUBBLE SORT
swapped = True
stopIndex = len(array)
while swapped == True and stopIndex >= 0:
swapped = False
for index in range(stopindex-1):
if array[index] > array[index+1]
temp = array[index]
array[index] = array[index+1]
array[index+1] = temp
swapped = True
stopIndex-=1
INSERTION SORT
value = array[index] -------------------------------------------------------- ------------------------------------- index -= 1 -----------------------------
INSERTION SORT
for index in range(1, len(array)):
value = array[index]
while index > 0 and value < array[index-1]:
array[index] = array[index-1]
index -= 1
array[index] = value
BINARY SEARCH
---------------------------- i--------------------------- return mid # changes depending on context --------------------------- ------------------- ------ ------------------- return -1 # changes depending on context
BINARY SEARCH
low = 0
high = len(array) - 1
while low <= high:
mid = (high+low) // 2
if array[mid] == target:
return mid # changes depending on context
elif array[mid] > target:
high = mid - 1
else:
low = mid + 1
return -1 # changes depending on context
What is a constructor? OOP
One of the default methods for a class which is used when an object is created. It may give values to none, some, or all of the instance variables for the object when it is created from the class.
What is Encapsulation? OOP
Encapsulation is the process of wrapping data and the methods that work on data within one unit.
This is related to setting private/public methods. Private can only be accessed from inside the class, public means it can be directly by any method within any other object in the program.
What is inheritance? OOP
The sharing of characteristics between a class of objects and a newly created subclass. This allows code to be re-used by extending an existing class.
What is Polymorphism? OOP
The ability of a subclass to inherit and change the properties and methods from the base class it inherits from.
How do you initialize a 2D Array?
array = [[0 for i in range(cols)] for count in range(rows)]