0% found this document useful (0 votes)
20 views

Sorting and Searching Algorithms Notes

Uploaded by

nityajaradi02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Sorting and Searching Algorithms Notes

Uploaded by

nityajaradi02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SORTING AND SEARCHING

Introduction to

Sorting Algorithms
Bubble Sort:
• Definition: Repeatedly compares adjacent
elements and swaps them if they are in the
wrong order. This process is repeated until the
array is sorted.
• Time Complexity:
o Worst-case: O(n²)
o Average-case: O(n²)
o Best-case: O(n) (when the array is already
sorted)
• Space Complexity: O(1) (in-place)
Selection Sort:
• Definition: Divides the array into a sorted and
an unsorted region. Repeatedly selects the
smallest (or largest) element from the
unsorted region and moves it to the end of
the sorted region.
• Time Complexity:
o Worst-case: O(n²)
o Average-case: O(n²)
o Best-case: O(n²)
• Space Complexity: O(1) (in-place)
Insertion Sort:
• Definition: Builds the sorted array one item at
a time. Picks elements from the unsorted
region and inserts them into their correct
position in the sorted region.
• Time Complexity:
o Worst-case: O(n²)
o Average-case: O(n²)
o Best-case: O(n) (when the array is already
sorted)
• Space Complexity: O(1) (in-place)
Merge Sort:
• Definition: Divides the array into two halves,
recursively sorts each half, and then merges
the sorted halves to produce the sorted array.
• Time Complexity:
o Worst-case: O(n log n)
o Average-case: O(n log n)
o Best-case: O(n log n)
• Space Complexity: O(n) (requires additional
space for merging)
Quick Sort:
• Definition: Picks a 'pivot' element, partitions
the array into elements less than the pivot
and elements greater than the pivot, then
recursively sorts the sub-arrays.
• Time Complexity:
o Worst-case: O(n²) (when the smallest or
largest element is always picked as the
pivot)
o Average-case: O(n log n)
o Best-case: O(n log n)
• Space Complexity: O(log n) (for recursion
stack in average cases)
Heap Sort:
• Definition: Converts the array into a heap
structure (a binary heap), repeatedly extracts
the maximum (or minimum) element, and
reconstructs the heap until the array is sorted.
• Time Complexity:
o Worst-case: O(n log n)
o Average-case: O(n log n)
o Best-case: O(n log n)
• Space Complexity: O(1) (in-place)
2. Searching Algorithms
Linear Search:
• Definition: Sequentially checks each element
of the array until the target value is found or
the end of the array is reached.
• Time Complexity:
o Worst-case: O(n)
o Average-case: O(n)
o Best-case: O(1) (if the target is the first
element)
• Space Complexity: O(1) (in-place)
Binary Search:
• Definition: Efficiently searches for a target
value in a sorted array by repeatedly dividing
the search interval in half.
• Time Complexity:
o Worst-case: O(log n)
o Average-case: O(log n)
o Best-case: O(1) (if the target is in the
middle)
• Space Complexity: O(1) (iterative) or O(log n)
(recursive due to stack space)
3. Algorithm Analysis
Time Complexity:
• Measures the amount of time an algorithm
takes to complete as a function of the size of
the input. It is often expressed using Big-O
notation (e.g., O(n), O(log n)).
o O(1): Constant time
o O(log n): Logarithmic time
o O(n): Linear time
o O(n log n): Linearithmic time
o O(n²): Quadratic time
o O(2^n): Exponential time
Space Complexity:
• Measures the amount of memory an
algorithm uses as a function of the size of the
input. It is also expressed using Big-O
notation.
o O(1): Constant space
o O(n): Linear space
o O(n²): Quadratic space

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy