Sort
Stable Sort: Stable sort is a sorting algorithm that preserves the relative order of equal elements in the sorted output.
Local Sort: Local sort is a sorting algorithm that sorts elements in a small region of the list.
Bubble Sort
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
Stable Sort: Yes
Local Sort: Yes
1 | Time complexity: O(n^2) |
Here is the implementation of bubble sort in Python:
1 | def bubble_sort(arr): |
Selection Sort
Selection sort is another simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted part of the list and putting it at the beginning. The pass through the list is repeated until the list is sorted.
Stable Sort: Yes
Local Sort: Yes
1 | Time complexity: O(n^2) |
Here is the implementation of selection sort in Python:
1 | def selection_sort(arr): |
Insertion Sort
Insertion sort is a simple sorting algorithm that builds the final sorted list one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
Stable Sort: Yes
Local Sort: Yes
1 | Time complexity: O(n^2) |
Here is the implementation of insertion sort in Python:
1 | def insertion_sort(arr): |
Merge Sort
Merge sort is a divide and conquer algorithm that divides the list into two halves, sorts each half recursively, and then merges the two sorted halves back together.
Stable Sort: Yes
Local Sort: No
1 | Time complexity: O(nlogn) |
Here is the implementation of merge sort in Python:
1 | def merge_sort(arr): |
Quick Sort
Quick sort is a divide and conquer algorithm that selects a pivot element and partitions the other elements into two sub-lists, according to whether they are less than or greater than the pivot. The sub-lists are then sorted recursively.
Stable Sort: No
Local Sort: Yes
1 | Time complexity: O(nlogn) |
Here is the implementation of quick sort in Python:
1 | def quick_sort(arr): |




