The quicksort algorithm was discovered by Tony Hoare in 1959.
This algorithm is considered one of the fastest sorting algorithms and shows the best results in most cases.
Here is the algorithm visualization. Below I will describe how it works.
The algorithm works on the “divide and conquer” principle.
Worst case
asymptotic complexity O(n^2)
The answer depends on strategy for choosing pivot. In early versions of Quick Sort where leftmost (or rightmost) element is chosen as pivot, the worst occurs in following cases.
1. Array is already sorted in same order. 2. Array is already sorted in reverse order. 3. All elements are same (special case of case 1 and 2)
It can be fixed by choosing random pivot position
Task
Let’s see how the algorithm works on the example of a simple array of 12 numbers.
[10, 5, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6]
Below I will describe the steps that will show how this algorithm works.
Step 1.
Select “Pivot”. This is the element that will split the array into two parts in the future. On the left side of the pivot, there will be elements that are less than the pivot, and on the right, more or equal. There are many methods for choosing a pivot, and the speed at which the array will be sorted depends on the choice of the pivot. One of the methods for selecting a pivot is to select the last element of the array. Another method is selecting random element, in this case the time complexity always will be O(n log n).
We also need to create a variable “i”, which will point to the first element in the array, which is less than the pivot and at the same time is closest to it. We also need the index of the current element “j”
In our case,
pivot = 6 (value of the last element in the array),
i = 0,
j = 0
Step 2.
Compare the array element with index j with the pivot. 10 > 6. Array elements do not need to be moved. Change the index “j”. j = j + 1 = 1
Step 3.
Compare the array element with index j with the pivot. 5 < 6. Swap the elements with indices i and j. Now the array looks like this [10, 5, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6] –> [5, 10, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6] Change the indexes i = i + 1 = 1 j = j + 1 = 2
Step 4.
Compare the array element with index j with the pivot. 7 > 6. Array elements do not need to be moved. Change the index “j”. j = j + 1 = 3
Step 5.
Compare the array element with index j with the pivot. 4 < 6. Swap the elements with indices i and j. Now the array looks like this [5, 10, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6] –> [5, 4, 7, 10, 1, 2, 5, 7, 9, 8, 3, 6] Change the indexes i = i + 1 = 2 j = j + 1 = 4
Step 6.
Compare the array element with index j with the pivot. 1 < 6. Swap the elements with indices i and j. Now the array looks like this [5, 4, 7, 10, 1, 2, 5, 7, 9, 8, 3, 6] –> [5, 4, 1, 10, 7, 2, 5, 7, 9, 8, 3, 6] Change the indexes i = i + 1 = 3 j = j + 1 = 5
Step 7.
Compare the array element with index j with the pivot. 2 < 6. Swap the elements with indices i and j. Now the array looks like this [5, 4, 1, 10, 7, 2, 5, 7, 9, 8, 3, 6] –> [5, 4, 1, 2, 7, 10, 5, 7, 9, 8, 3, 6] Change the indexes i = i + 1 = 4 j = j + 1 = 6
Step 8.
Compare the array element with index j with the pivot. 5 < 6. Swap the elements with indices i and j. Now the array looks like this [5, 4, 1, 2, 7, 10, 5, 7, 9, 8, 3, 6] –> [5, 4, 1, 2, 5, 10, 7, 7, 9, 8, 3, 6] Change the indexes i = i + 1 = 5 j = j + 1 = 7
Step 9.
Compare the array element with index j with the pivot. 7 > 6. Array elements do not need to be moved. Change the index “j”. j = j + 1 = 8
Step 10.
Compare the array element with index j with the pivot. 9 > 6. Array elements do not need to be moved. Change the index “j”. j = j + 1 = 9
Step 11.
Compare the array element with index j with the pivot. 8 > 6. Array elements do not need to be moved. Change the index “j”. j = j + 1 = 10
Step 12.
Compare the array element with index j with the pivot. 3 < 6. Swap the elements with indices i and j. Now the array looks like this [5, 4, 1, 2, 5, 10, 7, 7, 9, 8, 3, 6] –> [5, 4, 1, 2, 5, 3, 7, 7, 9, 8, 10, 6] Change the indexes i = i + 1 = 6 j = j + 1 = 11
Step 13.
The “j” index is 11, which is the pivot index. We finish rearranging the elements in this iteration. Now we can split this array into 2 arrays. The left one will contain elements less than the pivot, and the right one is greater than or equal to the pivot. We leave the pivot in the middle between the arrays. It has already taken its place in the sorted array. The first element of the array in which all elements are greater than or equal to the pivot will be the element with index i. We transfer the pivot in the middle between the arrays. [5, 4, 1, 2, 5, 3] [6] [7, 7, 9, 8, 10]
Then we sort the resulting 2 subarrays ([5, 4, 1, 2, 5, 3] and [7, 7, 9, 8, 10]) according to the above algorithm. We no longer use pivot “6”. Each subarray will have its own pivot
To make it easier to write, I will describe the steps in the form of a table. You will be able to follow the steps and check the instructions above.
Step 14-19.
Sort the sub-array [5, 4, 1, 2, 5, 3]. pivot = 3; i = 0; j = 0
Compare
Current array
Procedure
Result array
i
j
5 > 3
[5, 4, 1, 2, 5, 3]
Do not swap elements.
Increment “j”
[5, 4, 1, 2, 5, 3]
0
1
4 > 3
[5, 4, 1, 2, 5, 3]
Do not swap elements.
Increment “j”
[5, 4, 1, 2, 5, 3]
0
2
1 < 3
[5, 4, 1, 2, 5, 3]
Swap elements “i” and “j”.
Increment “i” and “j”
[1, 4, 5, 2, 5, 3]
1
3
2 < 3
[1, 4, 5, 2, 5, 3]
Swap elements “i” and “j”.
Increment “i” and “j”
[1, 2, 5, 4, 5, 3]
2
4
5 > 3
[1, 2, 5, 4, 5, 3]
Do not swap elements.
Increment “j”
[1, 2, 5, 4, 5, 3]
2
5
Step 20-24.
Sort the sub-array [ 7, 7, 9, 8, 10]. pivot = 10; i = 0; j = 0
Compare
Current array
Procedure
Result array
i
j
7 > 10
[7, 7, 9, 8, 10]
Do not swap elements.
Increment “j”
[7, 7, 9, 8, 10]
0
1
7 > 10
[7, 7, 9, 8, 10]
Do not swap elements.
Increment “j”
[7, 7, 9, 8, 10]
0
2
9 > 10
[7, 7, 9, 8, 10]
Do not swap elements.
Increment “j”
[7, 7, 9, 8, 10]
0
3
8 > 10
[7, 7, 9, 8, 10]
Do not swap elements.
Increment “j”
[7, 7, 9, 8, 10]
0
4
We split the array. Now the general array looks like this: [1, 2] [3] [5, 4, 5] [6] [7, 7, 9, 8] [10]
Then we sort the resulting 3 subarrays according to the above algorithm. It turned out 3 subarrays because in the second subarray ([7, 7, 9, 8, 10]) the pivot was the largest element in the subarray
Step 25.
Sort the sub-array [1, 2]. pivot = 2, i = 0, j = 0,
Compare
Current array
Procedure
Result array
i
j
1 < 2
[1, 2]
Swap elements “i” and “j”.
Increment “i” and “j”
[1, 2]
0
1
We split the array. Now the general array looks like this: [1] [2] [3] [5, 4, 5] [6] [7, 7, 9, 8] [10]
Step 26-27.
Sort the sub-array [5, 4, 5]. Pivot = 5, i = 0, j = 0,
Compare
Current array
Procedure
Result array
i
j
5 = 5
[5, 4, 5]
Do not swap elements.
Increment “j”
[5, 4, 5]
0
1
4 < 5
[5, 4, 5]
Swap elements “i” and “j”.
Increment “i” and “j”
[4, 5, 5]
1
1
Step 27-29.
Sort the sub-array [7, 7, 9, 8]. pivot = 8, i = 0, j = 0,
Compare
Current array
Procedure
Result array
i
j
7 < 8
[7, 7, 9, 8]
Swap elements “i” and “j”.
Increment “i” and “j”
[7, 7, 9, 8]
1
1
7 < 8
[7, 7, 9, 8]
Swap elements “i” and “j”.
Increment “i” and “j”
[7, 7, 9, 8]
2
2
9 > 8
[7, 7, 9, 8]
Do not swap elements.
Increment “j”
[7, 7, 9, 8]
2
3
We split the array. Now the general array looks like this: [1] [2] [3] [4, 5] [5] [6] [7, 7] [8] [9] [10]
Step 30.
Sort the sub-array [4, 5]. pivot = 5, i = 0, j = 0,
Compare
Current array
Procedure
Result array
i
j
4 < 5
[4, 5]
Swap elements “i” and “j”.
Increment “i” and “j”
[4, 5]
1
1
We split the subarray. Now the general array looks like this: [1] [2] [3] [4] [5] [5] [6] [7, 7] [8] [9] [10]
Step 31.
Sort the sub-array [7, 7]. pivot = 7, i = 0, j = 0,
Compare
Current array
Procedure
Result array
i
j
7 = 7
[7, 7]
Swap elements “i” and “j”.
Increment “i” and “j”
[7, 7]
1
1
We split the subarray. Now the general array looks like this: [1] [2] [3] [4] [5] [5] [6] [7] [7] [8] [9] [10] The array sorting is finished.
PlantUML diagram of the algorithm
Sample code
Below you will get link to github repo with Kotlin, Java, and Python code
Insertion Sort appeared a long time ago. Intuitively, we use it to sort the cards in hand during card games. This is the easiest way to illustrate how this algorithm works.
How the algorithm works
Each element of the input sequence is scanned one at a time and compared with each element starting from the beginning of the list. The selected item takes its place in the list as soon as its place is found. And so with each element of the list.
The time complexity of the algorithm is O(n^2), but on small amounts of data, the algorithm sometimes performs very well.
Video shows how algorithm works
Sequence of operations
For example, let’s take the list [5, 3, 1, 6, 4].
We take the first item from the list. This is “5”. If it were a list of one element, then we would say that a list of one element is always sorted.
Moving on to the next item. This is “3”. Where should be “3” relative to “5”. Answer: before “5”. To do this, we must put “3” in front of “5”, and move “5”.
Now the list has taken the following form [3, 5, 1, 6, 4]. We can now say that the sublist [3, 5] is sorted.
Take the next item. This is “1”. Compare with the elements in front of it. 5 > 1, 3 > 1. Obviously, “1” must come before “5” and “3”.
Let’s move the sublist [3, 5] forward and put “1” in front of the sublist.
Now the list has taken the following form [1, 3, 5, 6, 4]. We can now say that the sublist [1, 3, 5] is sorted.
Take the next item. This is “6”. Compare with the elements in front of it. 5 <6. It makes no sense to check further since the sublist [1, 3, 5] was sorted. Item “6” remains in place.
Now the list has taken the following form [1, 3, 5, 6, 4]. We can now say that the sublist [1, 3, 5, 6] is sorted.
Take the next item. This is “4”. Compare with the elements in front of it. 6 > 4, 5 > 4, 3 < 4. There is no point in checking further since the sublist [1, 3, 5, 6] has been sorted. Obviously, “4” must come before “6”, “5”, but before “3”.
Let’s move the sublist [5, 6] forward, and put “4” in front of the sublist.
Now the list has taken the following form [1, 3, 4, 5, 6]. We have reached the end of the list and we can say that the list is all sorted from the original list.
Diagram
Sample code
Below you will get a link to GitHub repo with Kotlin, Java, and Python source code
I’ve got source from Google repo. Here is structure:
java – contains java code native – jni code.
When I try to build native I receive next error:
'latinime::MultiBigramMap::BigramMap::BigramMap(const latinime::MultiBigramMap::BigramMap&amp;)':
jni/src/suggest/core/dictionary/multi_bigram_map.h:56:9: error: base class 'class latinime::NgramListener' should be explicitly initialized in the copy constructor [-Werror=extra]
Full stacktrace:
➜ native ndk-build
[arm64-v8a] Compile++ : jni_latinime_common_static <= com_android_inputmethod_keyboard_ProximityInfo.cpp
[arm64-v8a] Compile++ : jni_latinime_common_static <= com_android_inputmethod_latin_BinaryDictionary.cpp
[arm64-v8a] Compile++ : jni_latinime_common_static <= com_android_inputmethod_latin_BinaryDictionaryUtils.cpp
[arm64-v8a] Compile++ : jni_latinime_common_static <= com_android_inputmethod_latin_DicTraverseSession.cpp
In file included from jni/src/suggest/core/session/dic_traverse_session.h:25:0,
from jni/com_android_inputmethod_latin_DicTraverseSession.cpp:24:
jni/src/suggest/core/dictionary/multi_bigram_map.h: In copy constructor 'latinime::MultiBigramMap::BigramMap::BigramMap(const latinime::MultiBigramMap::BigramMap&)':
jni/src/suggest/core/dictionary/multi_bigram_map.h:56:9: error: base class 'class latinime::NgramListener' should be explicitly initialized in the copy constructor [-Werror=extra]
BigramMap(const BigramMap &bigramMap)
^
cc1plus: all warnings being treated as errors
make: *** [obj/local/arm64-v8a/objs/jni_latinime_common_static/com_android_inputmethod_latin_DicTraverseSession.o] Error 1
To avoid it I’ve edited Android.mk in jni dir and replace: