Data Structures and Algorithms in C#
# CHAPTER 29
Data Structures and Algorithms in C#
1. Introduction
While C# provides excellent built-in collections likeList<T> and Dictionary, understanding how these structures work under the hood is critical. Knowledge of Data Structures and Algorithms (DSA) allows you to write highly optimized code and is the foundation of technical coding interviews.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand Big O Notation.
- Implement a custom Linked List.
- Understand how a Binary Search Algorithm works.
- Understand basic Sorting Algorithms (Bubble Sort).
3. Big O Notation (Time Complexity)
Big O notation describes how long an algorithm takes to run as the amount of data (N) increases.-
O(1) - Constant Time: Excellent. The time does not change regardless of data size. (e.g., Looking up a value in a
Dictionary).
-
O(N) - Linear Time: Fair. The time increases linearly with the data. (e.g., A
foreachloop iterating through aList).
- O(N^2) - Quadratic Time: Terrible. The time skyrockets as data grows. (e.g., A loop nested inside another loop).
4. Custom Data Structure: Linked List
AList<T> uses an array under the hood. When it runs out of space, it creates a new array and copies everything over. A Linked List, however, stores data in scattered "Nodes" across memory, with each Node pointing to the next one.
Advantage: Inserting an item at the beginning of a Linked List is O(1) (instant). Doing that in a List<T> requires shifting every other item down, which is O(N).
*(Note: C# provides a built-in LinkedList<T>, but building one is a classic interview question).*
5. Algorithm: Binary Search
If you are looking for a word in a dictionary, you don't read page 1, page 2, page 3. You open to the middle. If the word comes alphabetically *before* the middle page, you tear the book in half and throw away the back half. You repeat this until you find the word.This is Binary Search. It finds an item in a sorted array in O(log N) time (extremely fast).
6. Algorithm: Bubble Sort
Bubble Sort is a simple sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It is highly inefficient O(N^2) but excellent for learning.7. Built-in C# Sorting and Searching
While it's important to know how to write algorithms, in production C#, you almost always use the highly optimized built-in methods.8. Common Mistakes
- Running Binary Search on an Unsorted Array: Binary Search logic fundamentally relies on the array being sorted in ascending order. If you run it on an unsorted array, it will fail unpredictably.
- Using Nested Loops unnecessarily: If you write a loop inside a loop, you have created an O(N^2) algorithm. If N is 10,000, your code will execute 100,000,000 times! Always look for ways to optimize using Dictionaries.
9. Best Practices
-
Prefer
Dictionary<TKey, TValue>for lookups when you have a unique Key, as its O(1) time complexity is unbeatable.
10. Exercises
- 1. Hand-write the Binary Search algorithm on paper.
-
2.
Given an array
{ 10, 20, 30, 40, 50 }, trace the steps of Binary Search if looking for the number40.
11. MCQs with Answers
What does Big O Notation measure?
Which time complexity is the fastest/most efficient?
A loop nested inside another loop typically has a time complexity of:
What is the main advantage of a Linked List over an Array?
What is the strict requirement before performing a Binary Search?
How does Binary Search work?
What is the time complexity of Binary Search?
Which sorting algorithm compares adjacent elements and swaps them if they are in the wrong order?
Which class provides the optimized, built-in .Sort() and .BinarySearch() methods in C#?
12. Interview Questions
- Q: What is the difference in memory layout between an Array and a Linked List?
- Q: Explain O(log N) time complexity. Give an example.
- Q: Why is Bubble Sort considered highly inefficient for large datasets?