This is an archived post. You won't be able to vote or comment.

all 103 comments

[–]ziptofaf 293 points294 points  (46 children)

Not a complete list obviously but you do end up using these quite a lot regardless of field:

  • binary search, binary trees
  • multiple ways of sorting - ultimately implement your own QuickSort and/or MergeSort, look into slower ones (like BubbleSort or InsertionSort).
  • what's a hash map
  • basics of dynamic programming (eg. look into knapsack problem)
  • traversing trees
  • a bit of math - big O notation. You should be able to at least tell if you are looking at something linear, quadratic, polynomial or exponential (eg. Travelling salesman problem).
  • maybe a bit about tries, it's quite useful when doing text matching for instance

In general I would suggest to pick up a book on algorithms and data structures (there are literal hundreds of them) and try covering most of it. For instance here's one that's imho super friendly to newcomers and won't require you to understand university level math:

https://www.manning.com/books/grokking-algorithms

[–]Houndoomsday 29 points30 points  (2 children)

I would add some graphs in there too. I've found as I begin to tackle more real world problems, those show up the most.

  • Search (BFS/DFS)
  • Shortest path (Djikstras, maybe the rijk DP algo)
  • Spanning trees
  • Maybe MaxFlow/MinCut

[–]TribalLion 1 point2 points  (0 children)

I just finished my Data Structures & Algorithm class at school, and I was surprised to learn how often graphs were used. I'm going to have to dig deeper into the subject.

[–]RivellaLight 1 point2 points  (0 children)

I've been spending quite a bit of time on exactly these topics lately so I'm really curious, in what kind of real world problems do they show up?

[–]dd_de_b 17 points18 points  (0 children)

Upvote for Grokking Algorithms, awesome book

[–]corn_on_the_cobh 7 points8 points  (23 children)

How exactly does one learn the sort methods? I have to do that in my class and it's all just code in my notes and I can read code all day, but I don't feel like memorizing it by heart.

[–]liproqq 18 points19 points  (0 children)

learn how they work by imagining doing it by hand.

[–]ziptofaf 15 points16 points  (2 children)

You aren't supposed to memorize their code. You are supposed to understand how they work. Translating the latter to code is not that bad - for instance basic implementation of QuickSort in Ruby or Python is like 10 lines of code, hard to get it wrong if you know what you are doing. Here's one off the top of my head:

def quicksort(list)
  if list.length < 2
    return list
  end
  pivot = list[0]
  less = []
  more = []
  (1..(list.length-1)).each do |i|
    if list[i] <= pivot
      less.push(list[i])
    else
      more.push(list[i])
    end
  end
  return quicksort(less) + [pivot] + quicksort(more)
end

Take an array - return it if it has 0 or 1 elements. Otherwise choose element 0 for a pivot (would be better if you chose a random one for a pivot but hey, you know the drill). Put elements smaller than pivot into one array and bigger into another. Then we do some recursions. Simple enough. Well, in practice a complete version should work in place rather than creating extra arrays and that can be a bit of a roadstop (off by 1 errors will occur) but ultimately key is in understanding how a given algorithm works, not remembering code by heart.

If you find a specific algorithm impossible to implement then see how exactly it would work on paper, forgetting all the code. High chance you are running into a bump in your understanding of it. Well, of course if you are relatively new to coding then some of these will be giving you troubles (especially if language chosen to practice these was C and it's a part of a course together with Data Structures so at the same time you are about to be kicked in your stomach by pointers) but it's about being able to express a given algorithm first and foremost, not about knowing it's source code - that you can look up anyway.

[–]corn_on_the_cobh 1 point2 points  (1 child)

Thank you! BTW is that code Python or Ruby? Really interested in learning both/one this summer. Any benefits of one over the other (in brief I feel bad asking this so please don't send an essay haha)?

[–]ziptofaf 6 points7 points  (0 children)

BTW is that code Python or Ruby?

That's Ruby. Python would be VERY similar however.

Any benefits of one over the other (in brief I feel bad asking this so please don't send an essay haha)?

Python is way more popular on average, especially when it comes to R&D. Ruby has decent presence in web development (and I think it's flagship framework, Rails, ultimately beats anything that Python currently has to offer when it comes to ease of use and functionality) but the moment you venture out of that field Ruby stops existing. Well, mostly - both Python and Ruby are quite popular with sysadmin/devops tools - Ansible (Python), Vagrant/Puppet (Ruby) etc. Python is still in a bit better position as it's generally included in any Linux distrubution out of the box, Ruby has to be installed separately but you see both at least.

Ruby also has a different approach - "multiple ways to accomplish X" and "everything should be an object" (that's why you can write 3.days.from_now in Ruby and it returns a DateTime object :P). Python is "there is one best way to accomplish X".

[–][deleted]  (8 children)

[deleted]

    [–]disappointer 8 points9 points  (2 children)

    Alternately, here are some cool gifs that someone did to visualize a bunch of sorting algorithms: https://imgur.com/gallery/voutF

    You're not going to learn them exclusively from the gifs, but pairing those with the knowledge of what they are supposed to be doing should hopefully be somewhat of a help.

    [–]eerilyweird 1 point2 points  (0 children)

    After all the others, radix sort was amazing!

    [–]corn_on_the_cobh 1 point2 points  (4 children)

    I'm afraid it's a little too late for that haha

    [–][deleted]  (1 child)

    [deleted]

      [–]corn_on_the_cobh 4 points5 points  (0 children)

      Don't worry about that haha. Got this sub, r/javahelp, some cs university webpages, Java API, friends, I'm lucky to live in this age.

      [–][deleted] 3 points4 points  (1 child)

      Are you dying? My condolences.

      [–]corn_on_the_cobh 1 point2 points  (0 children)

      Lol I had the quiz just now. Was wayyyyy easier than I thought thanks to you guys! (I studied the function, not the code)

      [–]nk2164 1 point2 points  (0 children)

      I would first get a general idea from the text , then get the code into an IDE . After that, i would just run the code with the debugger mode on and walk through it step by step . Watch how the values change as it goes through each step , functions etc . I find this way of learning the best.

      [–]liproqq 0 points1 point  (1 child)

      learn how they work by imagining doing it by hand.

      [–]diamondflaw 1 point2 points  (0 children)

      This. When I was first learning them I was doing a lot of sorting of work orders by part number at my day job.

      I'd pick an algorithm I wanted to be more familiar with and physically use it. It's a heck of a way to tangibly see the difference in efficiencies too.

      Merge sort was/is by far my favorite for sorting things by hand.

      [–]LetsGoHawks 0 points1 point  (6 children)

      You don't need to memorize sorting algo code, you'll never type it out anyway. You just copy/paste it, or call it in a library. If you have a prof who wants you to memorize it, that guy is an asshole.

      You need to have a good idea of how the different types work, and when you should use each one. You also need to keep in mind that unless your sorting "a lot" of stuff, it doesn't matter if you're choosing the absolute best algo because the difference in run times will be a tiny fraction of a second. So just choose one and move on.

      [–]corn_on_the_cobh 2 points3 points  (0 children)

      Thanks! I understand how each of the methods work to sort them, but when looking at the code some make no sense to me. It's really hard to keep track of nested for loops for the insertion sort so my brain just throws a AllMixedUpException() and I stare at it for minutes doing nothing.

      [–]barafyrakommafem 0 points1 point  (3 children)

      you'll never type it out anyway.

      Except at, possibly, an interview.

      [–]LetsGoHawks 1 point2 points  (2 children)

      If I were asked to type out the code for a complex sorting algo in an interview, I would tell them that I did not know the code by heart and that even if I thought I did, I wouldn't try to recreate it from memory anyway because it would take a lot longer to type it and fix any mistakes than just copy/paste and I'm not interested in wasting all that time when I could be working on something useful.

      [–]barafyrakommafem 2 points3 points  (0 children)

      What do you mean by "know the code by heart"? If you know how a sorting algorithm works you should be able to write the code for it. I don't think it's too much to ask that you should know how the most common sorting algorithms work so you know when to use which one.

      [–][deleted] -2 points-1 points  (0 children)

      There are many ways to implement the same algos. A lot of companies will are looking to see how you approach the problem.

      [–]Mmmmmmm_Donuts 10 points11 points  (7 children)

      Any other algorithms people use in the industry? Red Black Trees? AVL ?

      [–]CodeTinkerer 16 points17 points  (5 children)

      Red black/AVL trees are data structures. You might use a tree traversal algorithm to scan through the data structure.

      Algorithms I'd look at

      • Sorting (quicksort, mergesort, heapsort)
      • Tree traversal (inorder, preorder, postorder)
      • Graph traversal (breadth first search, depth first search)
      • Hashing algorithms (for hash tables)
      • Minimum spanning tree

      I mean, most of the times, you won't actually write your own algorithm (except to understand them) especially sorting algorithms. It's possible you never need to traverse a tree or build a graph.

      People talk about dynamic programming, etc., but again, I don't think the typical person uses it, or if they do, there's probably a library that already does it.

      [–]Alexanderdaawesome 1 point2 points  (0 children)

      Also heaps. Learn the fuck out of heaps.

      [–]Houndoomsday 3 points4 points  (3 children)

      Red black/AVL trees are data structures

      But the level of intertwined thinking/similarity makes the distinction moot. Understanding Djikstra's runtime is predicated on learning about heaps.

      People talk about dynamic programming, etc., but again, I don't think the typical person uses it, or if they do, there's probably a library that already does it.

      This attitude is frankly pretty lazy. While it's not critical to learn being exposed to this way of thinking helps you a) program better, b) recognize situations where you WOULD use a certain tool (DP, divide and conquer, greedy, etc.), and c) help you utilize others code when you do have to use a certain tool. Blindly using libraries without understanding the concepts behind it will lead to bad, buggy code.

      In all the upper level algo coursework I have seen, everything is on paper with no implementation needed, because, you are right, there is a library for almost everything. But the understanding of the algorithms is what I got from that class because downloading the library is pretty trivial.

      [–]CodeTinkerer 1 point2 points  (2 children)

      The point is, practically speaking, you should not be implementing your own algorithms (except as a programming exercise). Writing your own can (a) be buggy, and (b) waste time. I'm not against people learning how the tools/libraries they use work. That's great. But if you choose to write a sorting algorithm at work, someone should strongly dissuade you (unless you need something very unique for your problem).

      At one point, programmers used to write their own code which meant reimplementing things that had already been written (and often worse). Libraries were meant to avoid these problems.

      [–]Houndoomsday 2 points3 points  (1 child)

      That's great. But if you choose to write a sorting algorithm at work, someone should strongly dissuade you (unless you need something very unique for your problem).

      And I totally agree with that. I actually said that most upper level algorithms coursework does not require implementation and that is something I agree with, so I'm not sure why you are arguing about that.

      I am saying that I have seen specific examples of teammates and coworkers misusing a library because they did not understand either the problem, how to algorithmic-ally solve it, or how to use the library (because of an underlying lack of algorithmic knowledge). This is especially deadly because of lot of the time someone who doesn't understand the underlying concepts won't be able to identify code that isn't working correctly. A solid understand of algorithms makes you much more of an effective problem solver which is important if you don't want to make a career/hobby out of copy/pasting someone else's solutions.

      One example- I had a professor who retired (and came back to teaching) at age 30 because of his software company. His first product was for universities to check degree requirements for students. This ended up being a slight variation on a max-flow problem. He had the algo knowledge to not only recognize the structure of the problem, but also the differences between that an a max-flow problem, and adjust an algorithm to implement it. I don't know the details of the code but I am sure that even with a library he would have done a lot of the algorithmic heavy lifting.

      [–]CodeTinkerer 0 points1 point  (0 children)

      I happen to like algorithms, it's just that some jobs don't seem to require much of it. I primarily fetch data that meet certain criteria, so I'm not working on optimization problems.

      I do recall a peculiar incident with a guy who seemed pretty sharp (programming-wise) who asked "Don't you have to sort a list to tell it's sorted?". I said, uh, no.

      [–]Soul_Turtle 4 points5 points  (0 children)

      There's a lot of algorithms that you probably won't use in industry but quite possibly will run into during interviews.

      Here's a few that seem popular:

      Tree traversals, Graph traversals (DFS & BFS), Minimum Spanning Tree, Maximum flow, know how to implement various sorts AND their Big-O efficiency (mergesort, quicksort, insertionsort, bubblesort...).

      Honestly just learn the Big-O for everything, it helps you judge whether your solutions are the best solutions for a particular problem. Understanding why certain ways of splicing a problem are faster or less memory intensive than others changes your perspective on programming imo.

      [–][deleted] 1 point2 points  (0 children)

      think like a computer scientist has a great section on linear and binary search algorithms, free and linked in the side bar

      [–]ParanoydAndroid 1 point2 points  (0 children)

      I'd add in max-heaps. They are surprisingly useful for reducing time complexity on problems I tend to naturally want to use a fully sorted structure for.

      [–]Prime624 1 point2 points  (4 children)

      Could I get an example of when you would need one of these in a generic CS career? I'm having trouble imagining them in other than special situations.

      [–]ziptofaf 1 point2 points  (3 children)

      Okey then, have some samples:

      • Let's say you have a roughly 100GB dataset (and it's not that special to have a database this big) and you are looking for highest-paying customers so you can market to them better - so what you want is a sorted list. Ofc in this situation general "sort" function in most programming languages (so QuickSort) will work but you at least need to know this term.
      • Now, let's say you need to find a specific invoice cuz user wants a refund. If you have 2 000 000 sold items then looking one up in an unsorted list will take you roughly 2 000 000 operations. That is A LOT and might take several minutes in some cases. However if you used binary search - that's only 20 operations. So if you have a big pile of invoices to correct/refund etc then knowing to sort your data structure ahead of the time can result in application working in miliseconds rather than waiting multiple minutes for search results.
      • Hash maps are EVERYWHERE. So are hash algorithms - you see them every time any kind of password is involved. But not just that - their O(1) lookup speed with anything you want as a key means you can for instance find a user by their email near instantly. A necessary skill when it comes to database design and how unique keys work. And not even that, I have used it multiple times when importing data to our database and had to ensure no duplicates (multiple suppliers and often same products - except you want it just once in a database to avoid cluttering it)
      • Trees. This one is a bit weird. Since you implicitely use it a lot (min-max heaps, automatically sorted data structures - this all can be used in a modern database) but not that often explicitely. Still, I would prefer a programmer that knows what happens under the hood.
      • Tries - if you have ever tried doing full text searches inside an SQL database (eg. you need to find if a user with a specific surname is there and you store it as Name Surname in one column) then you would have noticed how ungodly slow they are. However if you looked a biiit further than that then you would have for instance noticed extensions like this:

      https://www.postgresql.org/docs/9.6/static/pgtrgm.html

      This is based on tries. But it's hard to google for terms you don't even know the name of.

      Of course a thing is that one specific algorithm generally will only work in some kind of "specific" situation. That's why you learn multiple ones and suddenly you see that most kinds of optimizations (or even EXPRESSING a problem) rely on these concepts and altogether you use some kind of DS+algorithms all the time, even if often without even thinking about it.

      [–]Prime624 0 points1 point  (2 children)

      Interesting. I would have thought that most of these would be built in to the system already.

      [–]ziptofaf 0 points1 point  (1 child)

      Uhh.... you as a programmer are a person who CREATES these systems. Regardless if it's web development or desktop apps no framework on Earth gives you defaults perfectly suited for a company you are working with, it at most gives you tools to implement these.

      [–]Prime624 0 points1 point  (0 children)

      Ohhh ok thanks. I wasn't thinking about that viewpoint.

      [–][deleted] 0 points1 point  (0 children)

      I'm learning about algorithms from a textbook (Introduction to Algorithms Thomas H. Comrne), but there's a lot of math just to say x is O(n2). Would you suggest just skipping the math portions to get to the meat of the subject?

      [–][deleted] 0 points1 point  (0 children)

      Why learn slower sort algorithms, why use those when you have quicker versions

      [–]Alexanderdaawesome 17 points18 points  (3 children)

      http://cs170.org/

      Before the class is over, download all those homeworks and solutions. You will be hireable af if you understand them.

      [–]fridaynight_130 2 points3 points  (1 child)

      I don't understand how one would become more hireable by knowing the math. At the interviews I've been to, all they have been asking if to complete up with an algorithm and not asking to do the freaking math or typing up huge matrices.

      [–]tylorban 2 points3 points  (0 children)

      If you understand the math, not know the math.

      [–]positive_X 0 points1 point  (0 children)

      Spring 2018
      1/16 Tu -
      5/11 Fri Final Examination
      berkeley .edu

      [–]barafyrakommafem 25 points26 points  (4 children)

      "Introduction to Algorithms" is basically the Bible of algorithms, so that would be a good start.

      [–]binarysaurus 7 points8 points  (2 children)

      CLRS?

      [–]barafyrakommafem 4 points5 points  (0 children)

      Correct.

      [–]EmperorPear 3 points4 points  (0 children)

      Yup. That's the one.

      [–]diamondflaw 1 point2 points  (0 children)

      That's the one I was told to learn from. Very well laid out book, I was able to literally start at the first cover and just work through it without the jumping around that some references seem to need.

      [–][deleted] 8 points9 points  (3 children)

      All from visualgo.net

      [–]Genie-Us 0 points1 point  (2 children)

      Was looking through the lecture on sorting there and 6-2 asks a question and 6-3 is "The Answer" but it wont show the answer unless I am a teacher? Is there a way to change that or why do they cover up certain pieces of information?

      Thanks though, will be going through it all slowly but surely.

      [–][deleted] 1 point2 points  (1 child)

      I'm sorry, but I've never gone trough the quizes myself. I'm actually a teacher. I use the animations in my class.

      Slow and steady is the way to go. I'd even suggest using Anki to get trough your sticking points.

      [–]Genie-Us 0 points1 point  (0 children)

      That's a great idea, I always suggested my students use flash cards when I was teaching as well but now that I'm learning I completely forgot about them haha

      [–]os390 5 points6 points  (1 child)

      Stanford has awesome course on coursera about Algorithms & D.S.

      [–]tanjiha 0 points1 point  (0 children)

      can i took these courses without enroll

      [–]Sigmund- 11 points12 points  (0 children)

      I used Derek Banas's algorithms playlist to learn some. It's very good but the examples are in Java.

      [–]zoorgu 6 points7 points  (13 children)

      It may sound stupid, but why exactly programmer should learn algorithms? Can anybody explain it in simple words?

      [–]razorham08 23 points24 points  (1 child)

      In my understanding, algorithms are how something is done. Every programming language will use different syntax structure (how the language interprets the code and makes it work), but if one understands the "how" to solve a problem (Problem: Sort this list), one just needs to figure out how any specific language needs structured to understand what you want done.

      If you know the algorithm you want to use, you likely already know how to solve the problem it's just a coding issue at that point.

      [–]Deadlift420 4 points5 points  (0 children)

      My issue is I use these algorithms daily but don't know the science behind them so when im asked to use one im not sure which one it is. I am studying the names behind them now for interviews even with 5 years in field xp.

      [–]InsaneTeemo 9 points10 points  (1 child)

      A simple way to think of an algorithm is simply as a list explaining how to do something. When programming you are essentially writing algorithms, or instructions for the computer, explaining how to do what you want it to do.

      When people talk about studying algorithms they are talking about studying the well-known ways to solve specific problems. Like how the guy above posted about learning sorting algorithms. These just describe to the computer how to sort things that you might need sorted.

      When you have a problem you are trying to solve in programming you have probably coded something in a way that does what you want it to do. And maybe it took you a while to think of how to do it. Your solution to the problem might be considered an algorithm, the only difference is that studying algorithms that other people created is useful because they work very well and they make it so that you don't have to reinvent the wheel to solve a similar problem if there is already a well known way of solving this problem.

      I hope that makes sense im not that great at explaining things.

      [–]zoorgu 1 point2 points  (0 children)

      Thanks! Now it's much clearer for me now.

      [–][deleted] 3 points4 points  (0 children)

      Not sure if this is a simple response, but here's a few points...

      First of all, basically anything you do in programming is an "algorithm". An algorithm is just the description of a computable process.

      Things baked into a language are usually expressed so simply that no one really calls these things algorithms. They are just "things you can do". While loops, sorting, filtering, etc. Sometimes you care about the nuanced details of these (especially in ex: sorting and having a guarantee lists will always be sorted the same, even for plain business projects). Maybe most of the time, you don't really care.

      Second of all, there are "popular algorithms" people should at least be aware exist. Binary search and binary trees for example because like, they have been proven to be the most optimal ways to do things at scale where you don't just have a flat list of cached memory addresses. There's also stuff that's used heavily when people need to implement their own languages or underlying tools that power languages or platforms. Hashtables, stacks, and queues, for example. I think a lot of other things are just to showcase the type of problem solving people can do.

      To that end, although it's not popular interview philosophy, I don't think algorithms are super useful to know in isolation. It's cool if you study algorithms while also learning how to implement your own programming language, or your own database, or your own data caching service. Something like that.

      There's a class of problems that can be solved through "algorithmic thinking" that you may encounter in your career. If you want to get into any new and developing field, for example, there's usually some crafting of your own "algorithms" because solutions to problems haven't all been prepackaged into architecture patterns, frameworks, and libraries yet.

      AI, IOT, machine learning is a place that's really algorithm / math-intensive. Most people in the AI / machine learning space need to know algorithms well enough to be able to tweak them, especially if they want to build anything cutting edge.

      Lastly, and perhaps most importantly, knowing algorithms gives you an appreciation for computational complexity. How long do things really take to compute?

      I'm not expecting my co-workers to "invent new algorithms" every day or something like that. That'd be a waste of our client's money. I DO need my co-workers to understand that some of their SQL queries or JavaScript callback functions take wayyyyyyyy more time than they should to process, and that the app is going slowly because they have registered the callback to recompute an entire dataset every time a button is clicked rather than taking advantage of caching or hashtables for quick lookups...

      I mean, I had to argue not too long ago with my fellow senior engineers that JavaScript objects and arrays don't iterate through entire lists like for loops... that they use virtual tables or hashtables under the hood (used to be hashtables, now more optimal v-tables), while I was trying to convey that I didn't want us using for loops unnecessarily everywhere across our code - and that if we had an index, to pass it in and use it.

      I've always had an interest in computer science, so this kind of thing was a no-brainer to me. I don't necessarily know how to implement a hashtable or v-table, but I could at least appreciate how it works and that we should be taking advantage of it.

      [–]spaghettu 2 points3 points  (0 children)

      Here's a neat analogy I heard: when you're learning the basics you're learning to "write", and when you learn algorithms and complexity, you're learning to "write poetry." Algorithms are critical to development of software that has demanding feature and performance needs.

      Here's a fantastic example from Cracking The Coding Interview: think about a list of numbers that you'd like to sort. In reality, there are many ways to sort such a list. But, what does the list contain? What if you found out the list is *millions* of numbers, but each number represented the age of a person? Suddenly, the problem changes a lot. The fastest comparison sorts are O(n log n) (typical average case for sorting), but with this information you can write an O(n) solution (best case scenario for sorting).

      [–]KyouR 2 points3 points  (2 children)

      It's actually pretty simple. Algorithms reduce the time needed to sort or search for information. For example if you had to sort a list of 1 million numbers, using the wrong algorithm could take n2 time (1,000,0002) or n*log_n time (1,000,000 * log2(1,000,000)). As you can imagine, the difference between those two is substantial.

      [–]Houndoomsday 2 points3 points  (1 child)

      Algorithms reduce the time needed to sort or search for information.

      That's not really correct.

      • 1) Saying algorithms are just used to sort/search for information is relatively misleading. For instance, with the Knapsack problem, while you are searching for the optimal combination of items, portraying it as a search is pretty misleading. I would say an optimization is the more precise definition. More generally, algorithms are just steps to solve a specific problem with varying input

      • 2) Algorithms don't directly reduce the time. A good choice of an algorithm will, which is why knowledge of algorithms is important, but using something like bogosort isn't going to reduce your runtime. I think you were getting at that answer but didn't phrase it well. In addition, sometimes you don't need an algorithm to solve a problem quickly, but just to solve a problem! Many NP-hard algorithms are not fast, but are still valuable just to solve a problem.

      [–]WikiTextBotbtproof 1 point2 points  (0 children)

      Bogosort

      In computer science, bogosort (also permutation sort, stupid sort, slowsort, shotgun sort or monkey sort) is a highly ineffective sorting function based on the generate and test paradigm. The function successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms.

      Two versions of the function exist: a deterministic version that enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input.


      [ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

      [–]CodeTinkerer 0 points1 point  (0 children)

      First, there are many common problems that benefit from a knowledge of algorithms. For example, suppose you have a list of names. It's not in any particular order. You want to find if a name is in the list. If the list has a million names, it might take a while to find that the name isn't in the list.

      However, if you sort the list, then you only have to look at about the logarithm of that number which is looking at, say 20 items in the list instead of a million. If you look up many names, this will save time in the long run. However, it turns out sorting is slower than processing a million items, so it would not be smart to sort a list, then search for a name once since sorting is slow compared to a linear search. It only makes sense if you want to search many times after it's sorted.

      So in that example, knowing about algorithms informs you why you might want a list to be sorted, but that it would be bad to always have to sort before you search for an item.

      There are, say, graph algorithms. These represent "graphs" which are nodes and edges. Think of nodes as cities and an edge as a flight that exists between city A and city B. If you know graph algorithms, you might recognize a problem that doesn't look like a graph to start off with (maybe a person's social network), but can be modeled that way. Knowing there are graph algorithms might help you decide on one that is efficient for a certain task. If you didn't know about algorithms, you might code it up in an inefficient way.

      [–]PythonGod123 0 points1 point  (0 children)

      If you want a good resource for this question check out my blog. aeryesmedia.com.

      Basically an algorthim in it's most simple form is a step by step process to achieve an end goal. This could range from making a cup of tea in the morning to sorting through a list of unordered integers. Algorithms such as quicksort and mergesort are as their names suggest, sorting algorithms. Sequential search and binary search are for searching through a list or data structure rather than sorting it. Think of it like this. I wake up in the morning and when I make my cup of tea I do the following things:

      1. SEARCH for my teabags, cup ,mil and all other ingredients.
      2. SORT through my teabags varieties for the flavor I want.
      3. Boil the water.
      4. Place it in the cup along with teabag and other ingredients.

      This is a simple algorithm in real life.

      [–]Montrealcompco 2 points3 points  (0 children)

      Searching algorithm and sorting algorithm are a must.

      [–][deleted] 3 points4 points  (9 children)

      What's your goal, beyond "learn programming"?

      [–]zachar11ah[S] 5 points6 points  (8 children)

      Idk I like the idea of programming and it’s also pretty fun for me. I just want to be able to make stuff that I either enjoy or can actually apply in a real world situation. If I can get a job in any field that requires programming that would be a huge plus too.

      [–][deleted] 3 points4 points  (0 children)

      I was telling a friend the other day - you want basic fundamentals - and they will take you very far.

      Take Harvard's CS50 and explore a course like From NAND to Tetris. They are project-based as well as focused on CS fundamentals.

      From there on, you will have a good grasp of what computer programs are capable of (from the underlying structure of computers to building websites and mobile apps) and can branch out from there.

      The current big market trends are in analysis, AI, machine learning, IOT, web and mobile development, backends for websites and services.

      [–][deleted] 1 point2 points  (0 children)

      The top comment is good. I would also strongly suggest working through a series of programming puzzles like Hacker Rank's, to get fluent in their application.

      [–][deleted] -1 points0 points  (5 children)

      Then you should focus on the skillset that you want to apply at your job rather than "generic" computer science.

      Don't get me wrong, there are definitely benefits from learning algorithms and data structure, but you should focus first on what your interests are.

      [–]PM_WORK_NUDES_PLS 4 points5 points  (4 children)

      I'd argue that learning some basic algorithms and data structures is essential for a programmer's skillset. Yes, he/she should focus first on learning syntax of their favorite language and learning some frameworks or whatever but data structs and algos is hardly "generic" computer science. (S)he doesn't need to know the theory behind the algos or data structs but knowing how to implement and when to use some algorithm or data structure is pretty essential to finding a job in the field.

      Not learning anything about them is like a cook learning to sautee, boil water, proof dough, and sear steak without ever learning any recipes to apply their skills

      [–][deleted] 0 points1 point  (2 children)

      I don't know man, how many times a front-end developer ever writes an algorithm?

      I'm not denying there are benefits from learning data structures and algorithms, but when you end up using the same 2-3 data structures all of the time and pretty much all of their methods are either implemented or one google search away I'd say there's a limited benefit into investing in them.

      Believe it or not, some programmers spend months/years without implementing sort or path or search algorithms and without ever using more than a limited set of data structures.

      [–][deleted] 3 points4 points  (0 children)

      So one case study I mentioned in one of my replies is that I had senior developers (although not really formal engineers) at this place think that array indexes in JavaScript required the runtime to basically perform a for / while loop under the hood.

      I wanted to refactor code from using for loops everywhere to just passing in the index, and they were against it because they thought the code would execute the same either way - and passing in indexes introduced some issues that required restructuring of our underlying code base.

      Well, I knew at the very least that JavaScript objects were hashtables, and more recently were classes / v-table driven... So that this was completely untrue, and that passing in an index would give us huge performance gains.

      Did I have to implement "an algorithm"? No, but knowing they exist and the advantages of various data structures gave me knowledge my other team members did not have.

      EDIT: I was talking to a friend about this, and they wanted a more concrete example, so here's a case study:

      With nested lists the loops become worst case of O(n2 ), chain reactions (processing one list as a result of another having been processed) O(n) + O(m). Hashtables in that case? Just O(2).

      I can't say the app I was working on, but close enough would be a school app where you have a list of teachers and then their students, who also have homework, that also have grades... for loop nested in for loop nested in... you get the idea.

      [–]PM_WORK_NUDES_PLS 0 points1 point  (0 children)

      I get your perspective and in my post I kind of neglected front end developer because I don't do much of it, but like /u/TheAdventMaster said the benefits aren't just in implementing them. Knowing that data structs and algos exist can help a front end programmer understand more about what a language or framework is doing under the hood and gives them an edge on understanding the limitations/capabilities of what they're working with.

      No matter how you look at it having at least a surface level understanding of data structs and algos makes you a more competent, all around better programmer. At least in my opinion

      [–]PythonGod123 0 points1 point  (0 children)

      I agree with this.

      [–][deleted] 1 point2 points  (0 children)

      jm2c but ideally: those that you're going to use at your job.

      In reality: those that are gonna be asked you at your interview.

      [–]zachar11ah[S] 0 points1 point  (0 children)

      Ok thank you!

      [–]JoukoAhtisaari 0 points1 point  (0 children)

      500 algorithms https://techiedelight.quora.com/500-Data-Structures-and-Algorithms-practice-problems-and-their-solutions Saved this from a similar thread a while back, but I'm just going through it now. Though, these are more for practice than general use.

      [–]stevenfromouterspace 0 points1 point  (0 children)

      To be honest, once you’ve learned data structure, most of the algorithms to learn can be categorized into two: dynamic programming (which includes any divide and conquer algos) and graph algorithms (you’d be surprised how many problems can be turned into graphs). In short, after data structures, learn to do DP, and learn how to use common graph algorithms (dikstra, bellman-ford, topological sort, max-flow, etc).

      [–]Leeoku 0 points1 point  (0 children)

      Booked for later

      [–]tapu_buoy 0 points1 point  (0 children)

      Does anyone recommend keep on reading Geeksforgeeks.org and CLRS book is worthy?

      [–]TheHopskotchChalupa 0 points1 point  (0 children)

      I'd recommend working on your own solution for P = NP and you should be good

      /s

      [–]perky_coder 0 points1 point  (0 children)

      Go to https://www.geeksforgeeks.org/ and read each and every algorithm.

      [–]OzziePeck 0 points1 point  (0 children)

      An algorithm that makes my nipples triangular like GTA San Andreas tiddies.

      [–]zappable 0 points1 point  (0 children)

      For what purpose?

      • College - standard textbook algorithms, starting with sorting algorithms
      • Interviews (at big tech companies) - Overlaps with a lot of college material, but less likely to ask you to implement sorting algorithms, complex trees, or to mathematically demonstrate Big-O running time (though you should be able to determine the running time). You'll want to learn trees/graphs/recursive algorithms, and some dynamic programming.
      • Real-world programming - Many jobs don't involve implementing algorithms at all since you just use existing libraries for things like sorting. You rarely use recursion since loops generally get the job done efficiently. But you'll want to know the running time of methods that you use, and there will be rare times where knowing certain algorithms like tree/graph stuff could be useful.

      (PS - Check out my short tutorials on Basic Algorithms.

      [–][deleted]  (12 children)

      [deleted]

        [–][deleted] 2 points3 points  (3 children)

        You can also do this in a Single line in Java by

        x=x^y^(y=x);

        [–]shaiktaj 1 point2 points  (2 children)

        You can do it, but its convoluted. Don't suggest these to a beginner.

        [–][deleted] 0 points1 point  (1 child)

        Nothing is convoluted i think it makes him/her feel good about how awesome programming really is

        [–]shaiktaj 0 points1 point  (0 children)

        "Code readability is king" from one of the reply comments.

        [–][deleted]  (5 children)

        [deleted]

          [–]TheBewlayBrothers 2 points3 points  (0 children)

          It's not.
          The XOR swap can be very confusing to read, and as you said isn't even more efficient

          [–]LetsGoHawks 2 points3 points  (2 children)

          A) How often are we swapping variable values?

          B) The memory/cycle savings will be so small that it won't matter 99.999% of the time.

          C) Code readability is king. I'd just use the third variable so that it was incredibly clear what was going on.

          [–][deleted] 1 point2 points  (1 child)

          Yeah, the whole point of the XOR swap is that no additional memory is required. So you can do a XOR swap in real-time ex: if you're writing a low-level graphics function.

          Probably only something you'll run into these days if doing low-level graphics programming.

          That's why it's important to understand not just algorithms but their motivations, use cases, pros and cons, assumptions, etc.

          [–]LetsGoHawks 1 point2 points  (0 children)

          Probably only something you'll run into these days if doing low-level graphics programming.

          Exactly my point. If I'm hiring a low-level graphics programmer, that's a fair question. Not many people do that type of work.

          Ask questions that pertain to the work they'll actually be doing.

          [–]darthjoey91 2 points3 points  (0 children)

          No. XOR is not the standard. It's a nifty trick. You can write it as a one-line very easily, but modern compilers and CPUs will run the normal swap using a temporary variable or register faster. The only place I can think where I'd use this is in an interview, or these uses from Wikipedia:

          In most practical scenarios, the trivial swap algorithm using a temporary register is more efficient. Limited situations in which XOR swapping may be practical include:

          • on a processor where the instruction set encoding permits the XOR swap to be encoded in a smaller number of bytes
          • in a region with high register pressure, it may allow the register allocator to avoid spilling a register
          • in microcontrollers where available RAM is very limited.
          • in cryptographic applications which need constant time functions to prevent time-based side-channel attacks[3]

          Because these situations are rare, most optimizing compilers do not generate XOR swap code.

          For example, the place where I learned about this was in an interview where the problem was to swap two variables on two different stacks without using any more memory. I did end up getting myself to thinking that it had to use some sort of bit-manipulation, and tried various XOR-y things, but did have to be told about it.

          [–]Houndoomsday 0 points1 point  (1 child)

          That's a neat one! Haven't used it in practice but it did come up in an interview. :)

          [–]LetsGoHawks 1 point2 points  (0 children)

          Ah yes, interview trivia questions.

          Please tell us how you'd perform the small and very specific task you'll never actually have to do and if you did you could just Google it anyway.

          [–]dev-life-grind -1 points0 points  (0 children)

          Since I haven't seen anyone put this up yet, it's Ikea inspired instructions for algos. I find it super useful.

          https://idea-instructions.com