7 algorithms and data structures every programmer must know (2024)

7 algorithms and data structures every programmer must know (1)

In programmers life algorithms and data structures is most important subject if they want to go out in the programming world and make some bucks. Today, We will see what they do and where they are used with simplest examples. This list is prepared keeping in mind their use in competitive programming and current development practices.

1. Sort Algorithms

Sorting is the most heavily studied concept in Computer Science. Idea is to arrange the items of a list in a specific order. Though every major programming language has built-in sorting libraries, it comes in handy if you know how they work. Depending upon requirement you may want to use any of these.

  • Merge Sort
  • Quick Sort
  • Bucket Sort
  • Heap Sort
  • Counting Sort

More importantly one should know when and where to use them. Some examples where you can find direct application of sorting techniques include:

  • Sorting by price, popularity etc in e-commerce websites

2. Search Algorithms

Binary Search (in linear data structures)

Binary search is used to perform a very efficient search on sorted dataset. The time complexity is O(log2N). Idea is to repeatedly divide in half the portion of the list that could contain the item, until we narrow it down to one possible item. Some applications are:

  • When you search for a name of song in a sorted list of songs, it performs binary search and string-matching to quickly return the results.
  • Used to debug in git through git bisect

Depth/Breadth First Search (in Graph data structures)

DFS and BFS are tree/graph traversing and searching data structures. We wouldn’t go deep into how DFS/BFS work but will see how they are different through following animation.

7 algorithms and data structures every programmer must know (2)

Applications:

  • Used by search engines for web-crawling
  • Used in artificial intelligence to build bots, for instance a chess bot
  • Finding shortest path between two cities in a map and many other such applications

3. Hashing

Hash lookup is currently the most widely used technique to find appropriate data by key or ID. We access data by its index. Previously we relied on Sorting+Binary Search to look for index whereas now we use hashing.

The data structure is referred as Hash-Map or Hash-Table or Dictionary that maps keys to values, efficiently. We can perform value lookups using keys. Idea is to use an appropriate hash function which does the key -> value mapping. Choosing a good hash function depends upon the scenario.

Applications:

  • In routers, to store IP address -> Path pair for routing mechanisms
  • To perform the check if a value already exists in a list. Linear search would be expensive. We can also use Set data structure for this operation.

4. Dynamic Programming

Dynamic programming (DP) is a method for solving a complex problem by breaking it down into simpler subproblems. We solve the subproblems, remember their results and using them we make our way to solve the complex problem, quickly.

*writes down “1+1+1+1+1+1+1+1 =” on a sheet of paper* What’s that equal to?
*counting* Eight!
*writes down another “1+” on the left* What about that?
*quickly* Nine!
How’d you know it was nine so fast?
You just added one more
So you didn’t need to recount because you remembered there were eight! Dynamic Programming is just a fancy way to say ‘remembering stuff to save time later’

Applications:

  • There are many DP algorithms and applications but I’d name one and blow you away, Duckworth-Lewis method in cricket.

5. Exponentiation by squaring

Say you want to calculate 232. Normally we’d iterate 32 times and find the result. What if I told you it can be done in 5 iterations?

Exponentiation by squaring or Binary exponentiation is a general method for fast computation of large positive integer powers of a number in O(log2N). Not only this, the method is also used for computation of powers of polynomials and square matrices.

Application:

  • Calculation of large powers of a number is mostly required in RSA encryption. RSA also uses modular arithmetic along with binary exponentiation.

6. String Matching and Parsing

Pattern matching/searching is one of the most important problem in Computer Science. There have been a lot of research on the topic but we’ll enlist only two basic necessities for any programmer.

KMP Algorithm (String Matching)

Knuth-Morris-Pratt algorithm is used in cases where we have to match a short pattern in a long string. For instance, when we Ctrl+F a keyword in a document, we perform pattern matching in the whole document.

Regular Expression (String Parsing)

Many a times we have to validate a string by parsing over a predefined restriction. It is heavily used in web development for URL parsing and matching.

7. Primality Testing Algorithms

There are deterministic and probabilistic ways of determining whether a given number is prime or not. We’ll see both deterministic and probabilistic (nondeterministic) ways.

Sieve of Eratosthenes (deterministic)

If we have certain limit on the range of numbers, say determine all primes within range 100 to 1000 then Sieve is a way to go. The length of range is a crucial factor, because we have to allocate certain amount of memory according to range.

For any number n, incrementally testing upto sqrt(n) (deterministic)

In case you want to check for few numbers which are sparsely spread over a long range (say 1 to 1012), Sieve won’t be able to allocate enough memory. You can check for each number n by traversing only upto sqrt(n) and perform a divisibility check on n.

Fermat primality test and Miller–Rabin primality test (both are nondeterministic)

Both of these are compositeness tests. If a number is proved to be composite, then it sure isn’t a prime number. Miller-Rabin is a more sophisticated one than Fermat’s. Infact, Miller-Rabin also has a deterministic variant, but then its a game of trade between time complexity and accuracy of the algorithm.

Application:

  • The single most important use of prime numbers is in Cryptography. More precisely, they are used in encryption and decryption in RSA algorithm which was the very first implementation of Public Key Cryptosystems
  • Another use is in Hash functions used in Hash Tables

We’ll discuss some advanced algorithms every competitive programmer should know in the next post. Meanwhile master the above algorithms or share in the comments about what you think every beginner-intermediate programmer should know.

As an enthusiast with a deep understanding of algorithms and data structures, I've extensively delved into the intricacies of these fundamental concepts that form the backbone of computer science and programming. My hands-on experience and exploration span competitive programming, real-world development practices, and theoretical foundations. Now, let's dive into the concepts mentioned in the article.

  1. Sort Algorithms:

    • Merge Sort: A divide-and-conquer algorithm that recursively divides the array into halves, sorts them, and then merges the sorted halves.
    • Quick Sort: Another divide-and-conquer algorithm that works by selecting a 'pivot' element and partitioning the other elements into two sub-arrays.
    • Bucket Sort: A sorting algorithm that distributes elements into buckets and then sorts each bucket individually.
    • Heap Sort: It uses a binary heap data structure to build a max-heap and repeatedly extracts the maximum element.

    Applications: Sorting algorithms find applications in various scenarios, such as sorting by price or popularity in e-commerce websites.

  2. Search Algorithms:

    • Binary Search: An efficient algorithm for finding a target value within a sorted array by repeatedly dividing the search interval in half.
    • Depth/Breadth First Search (DFS/BFS): Traversal algorithms for graphs and trees.

    Applications: Binary search is used in searching for songs in a sorted list, debugging in Git through git bisect, and DFS/BFS are utilized in search engines, AI for building bots, and finding shortest paths.

  3. Hashing:

    • Hash Map/Hash Table/Dictionary: Data structures that map keys to values efficiently using a hash function.

    Applications: Hashing is widely used in routers for IP address mapping, checking if a value exists in a list, and other scenarios where efficient key-based data retrieval is needed.

  4. Dynamic Programming:

    • A problem-solving technique that involves breaking down a complex problem into simpler subproblems, solving them, and using the results to solve the overall problem efficiently.

    Applications: Illustrated with the Duckworth-Lewis method in cricket, dynamic programming is employed in various algorithms to solve complex problems.

  5. Exponentiation by Squaring:

    • A method for fast computation of large positive integer powers of a number using binary exponentiation.

    Applications: Essential for RSA encryption, where large powers of numbers are calculated using modular arithmetic and binary exponentiation.

  6. String Matching and Parsing:

    • KMP Algorithm (String Matching): Used for efficiently finding a pattern in a longer string.
    • Regular Expression (String Parsing): Essential for validating strings against predefined restrictions, commonly used in web development.

    Applications: KMP is employed in searching for keywords in documents, while regular expressions are used for URL parsing and matching in web development.

  7. Primality Testing Algorithms:

    • Sieve of Eratosthenes: Deterministic algorithm for finding all prime numbers within a given range.
    • Fermat and Miller–Rabin Primality Tests: Nondeterministic tests for checking whether a number is likely prime.

    Applications: Crucial for cryptography, especially in the RSA algorithm for encryption and decryption, and in hash functions used in hash tables.

Understanding and mastering these algorithms and data structures are crucial for programmers, whether they are venturing into competitive programming or working on real-world development projects. These concepts form the foundation of efficient and optimized code, making them essential for any programmer's toolkit.

7 algorithms and data structures every programmer must know (2024)
Top Articles
Easy Homemade Input for your Flower Farm: Eggshell Extract or WCA
5 Skinceuticals Vitamin C Dupes Put to the Test! | Chemist Confessions
Ucsf Ilab
Ink Free News Kosciusko County
Nail Salons Open Now Near My Location
Darshelle Stevens Thothub
15 Cloud Tattoo Meaning Symbolism- Reflecting Change and Transience
Stella.red Leaked
Cognitive Function Test Potomac Falls
Getwush Com
What Was D-Day Weegy
7 Best Character Builds In Nioh 2
Parents & Students · Infinite Campus
Milwaukee Nickname Crossword Clue
Pear Shaped Rocsi
Craigslist Org Hattiesburg Ms
Tamilblasters Movie Download Isaimini
Elemental Showtimes Near Sedaliamovies
Christopher Goosley Obituary
Winnie The Pooh Sewing Meme
10425 Reisterstown Rd
Kneaders Franchise Cost
Milwaukee Zoo Ebt Discount
Orbison Roy: (1936 1988) American Singer. Signed 7 X 9
Walgreens On 37Th And Woodlawn
Kayak Parts Amazon
Kleen Krete Concrete Remover 1 Gal Liquid 32110
How To Get Coins In Path Of Titans
Police in Germany arrest 25 people allegedly planning to overthrow the government
Hubspot Community
Fgo Spirit Root
Philasd Zimbra
O2 eSIM guide | Download your eSIM | The Drop
Minor League Baseball Leaders
Tyrone Unblocked Games Bitlife
FedEx zoekt een Linehaul Supervisor in Duiven | LinkedIn
Victor Predictions Today
C Spire Express Pay
O'reilly's In Mathis Texas
When His Eyes Opened Chapter 3021
Csulb Atlas
Wbap Iheart
Kieaira.boo
Immortal Ink Waxahachie
Accident On 40 East Today
Lompoc Record Arrest Log
Subway Surfers Unblocked Games World
Pizza Mia Belvidere Nj Menu
EXTON: THE MOST BEAUTIFUL CHOCOLATE BOX VILLAGE IN RUTLAND
Henry Ford Connect Email
Only Partly Forgotten Wotlk
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6031

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.