r/learnprogramming • u/Temporary-Warthog250 • Apr 05 '22
Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them
Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.
Someone else who reads yours and does understand it, try to explain it to them to help them learn
1.6k
Upvotes
5
u/MrSloppyPants Apr 05 '22 edited Apr 05 '22
A very simple way is to look at how many times you loop within the algo. If you take in an array and pick out the first element, that operation is O(1) because it doesn't matter how big the array is, you are always executing the same thing. If you take the array and loop over it once, your algo is O(n) where n is the number of elements in the array. if you loop over the array twice, your algo is O(2n). If you loop through the elements in the array and for each element you loop through the entire array again, your algo is O(n2) because you are looping through the array for each element in the array. If you are only looping through some of the array, say, as part of a binary search, then the algo is O(log n) which is faster than O(n) because you are not traversing the entire contents of the array.
This is way over-simplified but it's a good starting point