Sorting Method
Sorting Method
Sorting Method
io/p/kak-podruzhit-python-i-bazy-dannyh-sql-
podrobnoe-rukovodstvo-2020-02-27
Let’s take a tour of the top 6 sorting algorithms and see how we can
implement them in Python!
Bubble Sort
Bubble sort is the one usually taught in introductory CS classes since
it clearly demonstrates how sort works while being simple and easy
to understand. Bubble sort steps through the list and compares
adjacent pairs of elements. The elements are swapped if they are in
the wrong order. The pass through the unsorted portion of the list is
repeated until the list is sorted. Because Bubble sort repeatedly
passes through the unsorted part of the list, it has a worst case
complexity of O(n²).
def bubble_sort(arr):
def swap(i, j):
arr[i], arr[j] = arr[j], arr[i]
n = len(arr)
swapped = True
x = -1
while swapped:
swapped = False
x = x + 1
for i in range(1, n-x):
if arr[i - 1] > arr[i]:
swap(i - 1, i)
swapped = True
return arr
Selection Sort
Selection sort is also quite simple but frequently outperforms bubble
sort. If you are choosing between the two, it’s best to just default
right to selection sort. With Selection sort, we divide our input list /
array into two parts: the sublist of items already sorted and the
sublist of items remaining to be sorted that make up the rest of the
list. We first find the smallest element in the unsorted sublist and
place it at the end of the sorted sublist. Thus, we are continuously
grabbing the smallest unsorted element and placing it in sorted
order in the sorted sublist. This process continues iteratively until
the list is fully sorted.
def selection_sort(arr):
for i in range(len(arr)):
minimum = i
return arr
Insertion Sort
Insertion sort is both faster and well-arguably more simplistic than
both bubble sort and selection sort. Funny enough, it’s how many
people sort their cards when playing a card game! On each loop
iteration, insertion sort removes one element from the array. It then
finds the location where that element belongs within
another sorted array and inserts it there. It repeats this process until
no input elements remain.
def insertion_sort(arr):
for i in range(len(arr)):
cursor = arr[i]
pos = i
return arr
Merge Sort
Merge sort is a perfectly elegant example of a Divide and Conquer
algorithm. It simple uses the 2 main steps of such an algorithm:
return merged
Quick Sort
Quick sort is also a divide and conquer algorithm like merge sort.
Although it’s a bit more complicated, in most standard
implementations it performs significantly faster than merge sort and
rarely reaches its worst case complexity of O(n²). It has 3 main
steps:
(2) Move all elements that are smaller than the pivot to the left of
the pivot; move all elements that are larger than the pivot to the
right of the pivot. This is called the partition operation.
(3) Recursively apply the above 2 steps separately to each of the sub-
arrays of elements with smaller and bigger values than the last pivot.