Algorithms itself

13 flashcardsby Sabs2023
Study these flashcardsFree · spaced repetition · iPhone, Android, Mac & web

All cards

1
Linear search?
Starts at first item and checks each item one by one:
-Doesnt need to be ordered/sorted
-Can be efficient in small data sets but NOT in large data sets
2
Example of python inplementation
3
Binary search?
Find an item in SORTED data set via divide and conquer:
Need sorted list and is efficient for large data sets
Discards HALF of the data set per time
4
Actual steps?(lets say find y)
1)Front and rear pointers point to first and last item
2)Calculate midpoint by n//2, if its y output it BUT:
2.5A) IF mid point is larger then y, rear pointer becomes n-1 and discard right side.
2.5B) IF midpoint is smaller then y, front pointer becomes n+1 discard left side.
3)REPEAT UNTIL MIDPOINT EQUALS AND IS Y
5
Implementation of binary search in python:
6
Bubble sort
Compares an item to next and swaps if not in order,repeat with next pairs until reach end which is called a pass.
-Repeat this whole process again until all items are ordered
-"Bubbles" the largest/smallest unsorted item to the top per pass
7
Implimentation of bubble sort in python:
8
Insertion sort?
Inserts an item into correct spot at one time each
Starts at the 2nd item(then 3 etc) and compares items to the left and sees if they should swap or not
9
Implimentation of insertion sort in python:
10
Merge sort ?
Divide and conquer: Splits items all into individual lists then compares,sorts and combines adjacent lists. Repeat for the other lists.
Then compares,sorts,combines adjacent 2 item lists. Repeat for others
Do this until you have one sorted list
11
Implimentation of merge sort
12
Quick sort?
1)Uses a pivot(1st item) in which items smaller are placed to the left side sublist and items larger on right sublist. Pivot is then sorted.
2)Repeat same steps for the left and right sublists.
3)Do this until all items become pivots in which then it becomes sorted
13
Implimentation of quick sort:
Study all 13 cards for free