0% found this document useful (0 votes)
13 views

Dmytronoks Algorithms-Cs50

An algorithm is a set of step-by-step instructions to complete a task. Big O notation analyzes an algorithm's efficiency by measuring how memory and time requirements scale with input size. Common algorithms include search algorithms like linear and binary search, and sorting algorithms like bubble, selection, and merge sort. Recursion is when a function calls itself, and has a base case to terminate and a recursive case to continue calling itself.

Uploaded by

Lord Rigo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Dmytronoks Algorithms-Cs50

An algorithm is a set of step-by-step instructions to complete a task. Big O notation analyzes an algorithm's efficiency by measuring how memory and time requirements scale with input size. Common algorithms include search algorithms like linear and binary search, and sorting algorithms like bubble, selection, and merge sort. Recursion is when a function calls itself, and has a base case to terminate and a recursive case to continue calling itself.

Uploaded by

Lord Rigo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Algorithms (CS50) Cheat Sheet

by dmytronoks via cheatography.com/158792/cs/33506/

Definition Big O notation

Algorithm is a step-b​y-step set of instru​ctions for completing a task. Big-O notation is a simplified analysis of an algori​thm’s effici​ency. It
attempts to answer two questions:
Search Algorithms Q1: how much memory is needed?
Q2: how much time does it take to complete?
Linear Search : Iterate across Worst case O(n): the target element
the array from left to right, is the last element of the array or it
Recursion
searching for a specified doesn’t exist at all. Best case Ω(1):
element. the target element is the first A recursive function is one that, as part of its execution, invokes
element of the array. itself.
Binary Search: Divide and Worst case O(log n) : the target Every recursive function has two cases that could apply, given any
conquer, reducing the search element will be found at the end of input:
area by half each time, trying the last division or it won’t be found - The base case → terminate the recursive process
to find a target number. First, at all. Best case Ω(1): the target - The recursive case → the recursion occurs

the array should be sorted. element is the mid-point of the full


array. Example of recursion: the factorial function

The factorial is found by multip​lying n by all the whole numbers less


Sorting Algorithms than it.
Bubble Sort: Worst case O(n^2): the array is in the Example:
Swapping pairs of two reversed order. Best case Ω(1): the array is 5! = 5 x 4 x 3 x 2 x 1 = 120
elements: higher already perfectly sorted and we don’t need In C:
valued elements to make any swaps on the first run. int fact(int n)
towards the right and {
the lower valued if (n == 1)
elements towards the return 1;
left. else
2)
Selection Sort: Find Worst case O(n : iterate over each of the n return n * fact(n-1);
the smallest unsorted elements of the array (to find the smallest }
element and add it to unsorted element) → it’s at the very end of Scenario A: The function calls itself in the 'else' clause → recursive
the end of the sorted the array and repeat this proces n times, case
list. since only one element gets sorted on each Scenario B: The function terminates in the 'if' clause → base case
pass. Best case Ω(n2): exactly the same!

Merge Sort: Sort The best and the worst cases are the same:
smaller arrays and n log n.
then merge them in
sorted order.

By dmytronoks Published 5th August, 2022. Sponsored by ApolloPad.com


Last updated 4th August, 2022. Everyone has a novel in them. Finish
Page 1 of 1. Yours!
https://apollopad.com
cheatography.com/dmytronoks/

You might also like