0% found this document useful (0 votes)
5 views12 pages

Algorithms: Searching & Sorting Explained

The document explains algorithms as step-by-step procedures for problem-solving, emphasizing their importance in computer science and programming for efficiency and accuracy. It details searching algorithms like Linear and Binary Search, as well as sorting algorithms such as Bubble Sort, Selection Sort, Insertion Sort, and Interchange Sort, providing examples and Python code for each. Additionally, it highlights real-life applications of algorithms in various fields including search engines, e-commerce, banking, social media, AI, and gaming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Algorithms: Searching & Sorting Explained

The document explains algorithms as step-by-step procedures for problem-solving, emphasizing their importance in computer science and programming for efficiency and accuracy. It details searching algorithms like Linear and Binary Search, as well as sorting algorithms such as Bubble Sort, Selection Sort, Insertion Sort, and Interchange Sort, providing examples and Python code for each. Additionally, it highlights real-life applications of algorithms in various fields including search engines, e-commerce, banking, social media, AI, and gaming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Algorithms, Searching and

Sorting Algorithms
Discrete Structure
What is an Algorithm?

•An algorithm is a step-by-step


procedure to solve a problem.

•It takes input, performs steps,


and produces output.

•Used in computer science,


maths, and programming.
Why Do We Use
Algorithms?

•To solve problems in a


logical way.

•To make solutions faster


and efficient.

•To avoid mistakes and follow


clear steps.

•To compare different


solutions and select the best
Searching Algorithms:

Searching algorithms are


methods used to find a
value inside a list or
dataset.

Examples include:

•Linear Search

•Binary Search
Linear Search (With Example)
What it is:
•Checks each element one by one.
Example List:
[4, 7, 2, 9]
Searching for: 2
Process:
•4 ❌
•7 ❌
•2 ✔ (found)
Python Code:
def linear_search(arr, key):
for i in range(len(arr)):
if arr[i] == key:
return i
return -1
Binary Search (With Example)
Works only on sorted lists.
Example List:
[1, 3, 5, 7, 9]
Search for 5.
Steps:
•Mid = 5 → found ✔
Python Code (easy):
def binary_search(arr, key):
low, high = 0, len(arr)-1
while low <= high:
mid = (low + high) // 2
if arr[mid] == key:
return mid
elif key < arr[mid]:
high = mid - 1
else:
low = mid + 1
return -1
Sorting Algorithms:

Sorting algorithms arrange data


in ascending or descending
order.

Common types:
•Bubble Sort
•Selection Sort
•Insertion Sort
•Interchange Sort (Exchange
Sort)
Bubble Sort:
How it works:
Compare adjacent numbers and swap if needed.
Example:
[5, 3, 1] → [3, 5, 1] → [3, 1, 5] → [1, 3, 5]
Python Code:
def bubble_sort(arr):
for i in range(len(arr)-1):
for j in range(len(arr)-1-i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
Selection Sort
How it works:
•Find minimum element.
•Move it to the correct place.
•Repeat.
Example:
[7, 4, 2] → 2 selected → [2, 7, 4] → next round
Python Code:
def selection_sort(arr):
for i in range(len(arr)):
min_i = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_i]:
min_i = j
arr[i], arr[min_i] = arr[min_i], arr[i]
Insertion Sort
How it works:
•Take each element and insert into correct position.
Example:
[5, 3, 1]
3 inserted before 5 → 1 inserted before both.
Python Code:
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
Interchange Sort
How it works:
•Compare each element with every later element.
•Swap if needed.
Example:
[5, 3, 1] → [3, 5, 1] → [1, 5, 3] → [1, 3, 5]
Python Code:
def interchange_sort(arr):
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
Real-life Applications of Algorithms
Where algorithms are used:
•Search Engines: Google uses searching
algorithms to find information quickly.

•E-commerce: Sorting algorithms help


display products in ascending/descending
order.

•Banking & Finance: Searching algorithms


check transactions and detect fraud.

•Social Media: Algorithms decide which


posts appear first on your feed.

•Artificial Intelligence & Machine


Learning: Algorithms analyze data, make
predictions, and learn patterns.

•Gaming: Algorithms control logic, scores,


and player movements.

You might also like