Sorting Algorithms
What is Sorting?
• Sorting is the process of arranging data in a specific order, such as
ascending or descending.
Common Sorting Algorithms
1. Bubble Sort
◦ Repeatedly swaps adjacent elements if they are in the wrong
order.
◦ Time Complexity:
▪ Best: O(n)
▪ Worst: O(n²)
▪ Average: O(n²)
2. Selection Sort
◦ Finds the smallest element and places it at the beginning.
◦ Time Complexity:
▪ Best: O(n²)
▪ Worst: O(n²)
▪ Average: O(n²)
3. Insertion Sort
◦ Builds a sorted array one item at a time.
◦ Time Complexity:
▪ Best: O(n)
▪ Worst: O(n²)
▪ Average: O(n²)
Advanced Sorting Algorithms
1. Merge Sort
◦ Divides the array into halves, sorts them, and merges them.
◦ Time Complexity:
▪ Best: O(n log n)
▪ Worst: O(n log n)
▪ Average: O(n log n)
2. Quick Sort
◦ Uses a pivot to partition the array and sorts recursively.
◦ Time Complexity:
▪ Best: O(n log n)
▪ Worst: O(n²)
▪ Average: O(n log n)
Applications of Sorting
• Organizing data for binary searches.
• Arranging data for efficient merging and filtering.
• Enhancing visualization techniques.