Sorting Algorithms Time Complexity in python .pptx
1.
Time Complexity ofBubble Sort,
Selection Sort, and Insertion Sort
An Overview of Sorting Algorithms
2.
Introduction
• Sorting algorithmsare fundamental in
computer science.
• We'll explore the time complexities of three
basic sorting algorithms:
• 1. Bubble Sort
• 2. Selection Sort
• 3. Insertion Sort
3.
Bubble Sort
• BubbleSort compares adjacent elements and
swaps them if they are in the wrong order.
• Best Case: O(n) - when the array is already
sorted
• Average Case: O(n^2)
• Worst Case: O(n^2)
4.
Selection Sort
• SelectionSort selects the smallest element
from the unsorted portion and moves it to the
sorted portion.
• Best Case: O(n^2)
• Average Case: O(n^2)
• Worst Case: O(n^2)
5.
Insertion Sort
• InsertionSort builds the sorted array one item
at a time.
• Best Case: O(n) - when the array is already
sorted
• Average Case: O(n^2)
• Worst Case: O(n^2)
6.
Comparison Table
• |Algorithm | Best Case | Average Case |
Worst Case |
• |----------------|-----------|--------------|------------|
• | Bubble Sort | O(n) | O(n^2) | O(n^2)
|
• | Selection Sort | O(n^2) | O(n^2) |
O(n^2) |
• | Insertion Sort | O(n) | O(n^2) | O(n^2)
|
7.
Conclusion
• All threesorting algorithms have O(n^2)
worst-case time complexity.
• Insertion Sort performs better for nearly
sorted arrays.
• For large datasets, more efficient algorithms
like Merge Sort or Quick Sort are preferred.