r/compsci • • Aug 14 '13

Algorithims Everyone Should Know?

What are some of you're favourite algoritms or concepts that you think everyone should know, whether they solve problems that crop up frequently, or are just beautiful in their construction?

379 Upvotes

118 comments sorted by

View all comments

Show parent comment

2

u/asimian Aug 14 '13

Most languages either have a swap function built in, or it would be trivial to add one. In C, it's tougher because there is no way to write a generic function. Of course, your solution only works for numeric types, so it's not really generic either. If you only need ints:

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

The idea of "yet more code to keep track of in headers" is kind of ridiculous by the way.