Sorting algorithms are used to rearrange a list of elements in a particular order (e.g. ascending or descending). There are many different sorting algorithms, each with its own strengths and weaknesses. Some common sorting algorithms include:
- Bubble sort: Bubble sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order. It has a time complexity of O(n^2) in the worst case, which makes it relatively slow for large lists.
- Selection sort: Selection sort is another simple sorting algorithm that works by repeatedly selecting the smallest element in the unsorted portion of the list and moving it to the sorted portion. It has a time complexity of O(n^2) in the worst case, which makes it relatively slow for large lists.
- Insertion sort: Insertion sort is a simple sorting algorithm that works by iterating through the list and inserting each element into its correct position in the sorted portion of the list. It has a time complexity of O(n^2) in the worst case, which makes it relatively slow for large lists.
- Merge sort: Merge sort is a divide-and-conquer sorting algorithm that works by recursively dividing the list in half, sorting each half, and then merging the sorted halves back together. It has a time complexity of O(n log n) in the worst case, which makes it more efficient than the previous algorithms for large lists.
- Quick sort: Quick sort is a divide-and-conquer sorting algorithm that works by selecting a pivot element and partitioning the list around it, such that all elements less than the pivot are placed before it and all elements greater than the pivot are placed after it. It has a time complexity of O(n log n) in the average case, which makes it one of the most efficient sorting algorithms for large lists.
- Heap sort: Heap sort is a comparison-based sorting algorithm that works by building a heap (a complete binary tree with a specific property) from the input list and then repeatedly extracting the maximum element from the heap and placing it at the end of the sorted list. It has a time complexity of O(n log n) in the worst case, which makes it more efficient than the previous algorithms for large lists.