When I was learning data structures and algorithms, I tried to present the sorting algorithm in the form of animation to make it easier to understand and remember. This article is better to read with [Demo Learning Data Structures and Algorithms in Object-C - Sorting Algorithm](https://github.com/MisterBooo/Play-With-Sort-OC). Table of contents * Selection sort * Bubble sort * Insertion sort * Quick sort * Two-way quick sort * Three-way quick sort * Heap sort * Summary and harvest * References and Reading Selection Sort Selection sort is a simple and intuitive sorting algorithm. No matter what data is entered, the time complexity is O(n?). So when using it, the smaller the data size, the better. The only advantage may be that it does not take up extra memory space. 1. Algorithm steps 1. First find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence 2. Continue to look for the smallest (largest) element from the remaining unsorted elements, and then put it at the end of the sorted sequence. 3. Repeat step 2 until all elements are sorted. 2. Code Implementation
Bubble Sort Bubble Sort is also a simple and intuitive sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of visiting the sequence is repeated until there is no need to swap, that is, the sequence has been sorted. The name of this algorithm comes from the fact that smaller elements will slowly "float" to the top of the sequence through swapping. 1. Algorithm steps 1. Compare adjacent elements. If the first is larger than the second, swap them. 2. Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. After this step, the last element will be the last number. 3. Repeat the above steps for all elements except the last one. 4. Keep repeating the above steps for fewer and fewer elements each time, until there are no pairs of numbers that need to be compared. 2. Code Implementation
Insertion Sort Although the code implementation of insertion sort is not as simple and crude as bubble sort and selection sort, its principle should be the easiest to understand, because anyone who has played poker should be able to understand it in seconds. Insertion sort is the simplest and most intuitive sorting algorithm. Its working principle is to build an ordered sequence. For unsorted data, it scans from the back to the front in the sorted sequence, finds the corresponding position and inserts it. 1. Algorithm steps 1. Treat the first element of the sequence to be sorted as an ordered sequence, and the second element to the first element as an unsorted sequence. 2. Scan the unsorted sequence from beginning to end, and insert each scanned element into the appropriate position of the ordered sequence. (If the element to be inserted is equal to an element in the ordered sequence, the element to be inserted is inserted after the equal element.) 2. Code Implementation
Merge Sort Merge sort is an effective sorting algorithm based on the merge operation. This algorithm is a very typical application of the Divide and Conquer method. As a typical algorithm application of the divide and conquer idea, merge sort is implemented in two ways: >1. Top-down recursion (all recursive methods can be rewritten using iteration, so there is the second method) >2. Bottom-up iteration; This article uses **top-down** merge sort 1. Algorithm steps 1. Apply for space whose size is equal to the sum of the two sorted sequences. This space is used to store the merged sequence. 2. Set two pointers, whose initial positions are the starting positions of the two sorted sequences; 3. Compare the elements pointed to by the two pointers, select the relatively small element to put into the merge space, and move the pointer to the next position; 4. Repeat step 3 until a pointer reaches the end of the sequence; 5. Copy all remaining elements of the other sequence directly to the end of the merged sequence. 2. Code Implementation
Quick Sort Quicksort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires Ο(nlogn) comparisons. In the worst case, it requires Ο(n2) comparisons, but this is uncommon. In fact, quicksort is often significantly faster than other Ο(nlogn) algorithms because its inner loop can be implemented efficiently on most architectures. Quick sort uses a divide and conquer strategy to divide a list into two sub-lists. Quick sort is another typical application of the divide and conquer idea in sorting algorithms. In essence, quick sort should be regarded as a recursive divide and conquer method based on bubble sort. The name of quick sort is simple and crude, because as soon as you hear this name, you know its purpose, which is fast and efficient! It is one of the fastest sorting algorithms for processing large data. 1. Algorithm steps 1. Pick an element from the sequence, called the "pivot"; 2. Reorder the sequence, placing all elements smaller than the base value in front of the base value, and all elements larger than the base value in the back of the base value (the same number can be on either side). After this partition is exited, the base value is in the middle of the sequence. This is called a partition operation; 3. Recursively sort the subsequences of elements less than the base value and the subsequences of elements greater than the base value; The optimization of quick sort can be considered to use insertion sort when the partition interval is small. 2. Code Implementation
Multi-way quick sort Too many duplicate keys reduce Quick Sort to O(n^2) After using double quick sort, our quick sort algorithm can easily handle arrays containing a large number of elements. The optimization of quick sort can be considered to use insertion sort when the partition interval is small. 1. Algorithm diagram 2. Code Implementation
Three-way quick sort For arrays containing a large amount of repeated data, three-way quick sort has a huge advantage For general random arrays and nearly ordered arrays, the efficiency of three-way quick sort is not perfect, but it is within a very acceptable range. Therefore, in some languages, three-way quick sort is the default sorting algorithm used in language library functions. For example, Java:) The optimization of quick sort can be considered to use insertion sort when the partition interval is small. 1. Algorithm diagram 2. Code Implementation
Stacking order Heapsort refers to a sorting algorithm designed using the heap data structure. A heap is a structure that approximates a complete binary tree and satisfies the properties of a heap: the key value or index of a child node is always smaller than (or larger than) its parent node. Heapsort can be said to be a selection sort that uses the concept of a heap to sort. There are two methods: Max-top heap: The value of each node is greater than or equal to the value of its child node, and is used for ascending order in the heap sort algorithm; Mini-top heap: The value of each node is less than or equal to the value of its child node, which is used for descending order in the heap sort algorithm; The average time complexity of heap sort is O(nlogn). 1. Algorithm steps 1. Create a heap H[0...n-1]; 2. Swap the head of the heap (the highest value) and the tail of the heap; 3. Reduce the size of the heap by 1 and call shift_down(1) to adjust the top data of the new array to the corresponding position; 4. Repeat step 2 until the heap size is 1 2. Code Implementation
Ending and Harvest Summarize: In the process of relearning data structures and algorithms, I fully realized the importance of learning these so-called **basic knowledge**, and understood that in order to further improve the level of iOS development, the basic links cannot be ignored. It also happened that in this study, the deep traversal of the graph was used to solve the problem of finding the backtracking source in the process of studying the buried points. Gains: > 1. Basic sorting whiteboard programming > 2. Add categories to runtime > 3. runtime's objc_msgSend() > 4. Deep copy and shallow copy > 5. Use of GCD semaphores If you have gained something from reading this, please give it a star on [Github](https://github.com/MisterBooo/Play-With-Sort-OC)** References and Reading * [A GitBook online book about sorting algorithms, "Top Ten Classic Sorting Algorithms", implemented in JavaScript & Python & Go](https://github.com/hustcc/JS-Sorting-Algorithm) * [Learn Data Structure and Algorithm in JavaScript](https://juejin.im/post/594dfe795188250d725a220a) * [Sorting animation](https://github.com/JiongXing/JXSort) |
>>: What AI can and cannot do for cybersecurity
Reviewer of this article: Zhou Xiaobo, Doctor of ...
Activities are a means to quickly achieve specifi...
Most mature products have their own points system...
For entrepreneurs, although mini program developm...
As a coder, you must be familiar with source code...
Does anyone have this feeling? Many operators are...
In the early morning of November 1, 2023, A group...
The sun rises in the east and it rains in the wes...
How to make holiday marketing most effective in t...
Before operating new media , the first thing we n...
[September 10 news] Automobile manufacturer Nissa...
Hello, your little cutie is online~ How about it,...
1. Introduction to the audience of Mayu advertisi...
According to the latest research, the use of gener...
Science and Technology Daily, Beijing, September ...