Lecture Notes 3: Introduction to Computer Science – Algorithms and Data Structures
Topic: Introduction to Algorithms and Data Structures
Objective: To understand the role of algorithms and data structures in problem-solving.
1. What is an Algorithm?
Definition: A step-by-step procedure or formula for solving a problem.
Characteristics of a Good Algorithm:
Correctness: It must solve the problem.
Efficiency: It must do so using minimal resources (time, space).
Clarity: It must be easy to understand and implement.
2. Sorting Algorithms
Definition: Algorithms that arrange elements in a specific order (ascending or descending).
Common Sorting Algorithms:
Bubble Sort: Repeatedly steps through the list, compares adjacent elements, and
swaps them if they are in the wrong order.
Time Complexity: O(n2)O(n2)
Quick Sort: Divides the list into smaller parts and sorts them independently.
Time Complexity: O(nlogn)O(nlogn) on average
Merge Sort: Divides the list in half, sorts each half, and merges the results.
Time Complexity: O(nlogn)O(nlogn)
3. Data Structures
Definition: A way to store and organize data to make it easier to perform operations.
Types of Data Structures:
Arrays: A collection of elements identified by index or key.
Use Case: Storing a list of numbers.
Linked Lists: A linear collection of data elements where each element points to the
next.
Use Case: Dynamic memory allocation.
Stacks: A collection where elements are added and removed from the top (LIFO –
Last In, First Out).
Use Case: Undo operations in software.
Queues: A collection where elements are added at the end and removed from the
front (FIFO – First In, First Out).
Use Case: Managing tasks in a print queue.
4. Summary
Algorithms and data structures are essential for solving problems efficiently.
They provide the foundation for creating software and applications.