There are not many algorithms involved in ios development. Unless your application is really large, Or you want your application to have a very good performance, you can think of algorithm-related performance optimization, what is actually available in ios development is sorting. Of course, if you are playing games, you may involve a lot of algorithms or optimization problems, but this is not the scope of this article.
In the subsequent articles, I will introduce the eight algorithms in detail.
2. Bubble Sorting 2.1
In the previous two blogs, the insertion sorting is based on "insert records one by one", and the selection sorting is based on "select", so the Bubble Sorting is based on "Exchange ". Each time we start from the first record, the first and second records are compared, and the second and third records are put later. This is a bubble sort. After each Bubble sorting, the records with the largest median value in the unordered sequence come to the end of the sequence, so they are called Bubble sorting.
Code 2.2
Compared with simple sorting, Bubble Sorting exchanges more frequently. It uses constant exchange to bring out the largest number. The average Bubble Sorting time and the worst (reverse) time are o (n ^ 2 ). In the best case, although there is no exchange, the number of comparisons is not reduced, and the time complexity is still o (n ^ 2 ). In addition, the Bubble Sorting is stable.
3. Fast sorting 3.1 leads
Quick sorting is an improvement of Bubble sorting. After a Bubble Sorting is completed, the maximum value is generated. Can you select a value first and then scan the sequence to be sorted, divide the records smaller than the value and those greater than the value into two separate sequences, and then perform the preceding operations on the two sequences respectively. This is the quick sorting. We call the selected value the pivot value. If the pivot value is the maximum value in the sequence, the quick sorting becomes a Bubble sorting.
Code 3.2
The first is to refer to data structure, which is popular on the Internet. The second is to refer to introduction to algorithms, which is complicated to implement.
1 // fast sorting (scanning in the middle at both ends) 2 void quickSort1 (int * a, int low, int high) 3 {4 int slow tkey = a [low]; // take a [low] as the pivot value 5 int I = low, j = high; 6 if (low> = high) 7 return; 8 // a Quick Sort 9 while (I <j) {// bidirectional scan 10 while (I <j & a [j]> = 1_tkey) 11 j --; 12 a [I] = a [j]; 13 while (I <j & a [I] <= pivotkey) 14 I ++; 15 a [j] = a [I]; 16} 17 a [I] = pivotkey; // place the pivot value 18 // sort 19 quickSort1 (, low, I-1); 20 quickSort1 (a, I + 1, high); 21} 22 23 // Quick Sort (take the value of the last record as the pivot value, one-way scan array) 24 void quickSort2 (int * a, int low, int high) 25 {26 int counter tkey = a [high]; // take a [high] as the pivot value 27 int I = low-1, temp, j; 28 if (low> = high) 29 return; 30 // a Quick Sort 31 for (j = low; j
Quick Sorting Algorithm:
1 quick sorting method 2 3 static void Main (string [] args) 4 {5 6 // 5 times comparison 7 for (int I = 1; I <= 5; I ++) 8 {9 List <int> list = new List <int> (); 10 // insert 200 random numbers to the array. 11 for (int j = 0; j <200; j ++) 12 {13 Thread. sleep (1); 14 list. add (new Random (int) DateTime. now. ticks ). next (0, 10000); 15} 16 Console. writeLine ("\ n" + I + "Times comparison:"); 17 Stopwatch watch = new Stopwatch (); 18 watch. start (); 19 var result = list. orderBy (single => single ). toList (); 20 watch. stop (); 21 Console. writeLine ("\ n system-defined quick sorting time-consuming:" + watch. elapsedMilliseconds); 22 Console. writeLine ("Top 10 output:" + string. join (",", result. take (10 ). toList (); 23 watch. start (); 24 new QuickSortClass (). quickSort (list, 0, list. count-1); 25 watch. stop (); 26 Console. writeLine ("\ n the time-consuming fast sorting written by myself:" + watch. elapsedMilliseconds); 27 Console. writeLine ("Top 10 output:" + string. join (",", list. take (10 ). toList (); 28 Console. readKey (); 29} 30} 31 32 public class QuickSortClass33 {34 35 // <summary> 36 /// // split function 37 /// </summary> 38 //// // <param name = "list"> array to be sorted </param> 39 // <param name = "left"> left subscript of the array </param> 40/ ///// <param name = "right"> </param> 41 // <returns> </returns> 42 public int Division (List <int> list, int left, int right) 43 {44 // first select a baseline element 45 int baseNum = list [left]; 46 while (left <right) 47 {48 // search forward from the right end of the array until a number smaller than the base value is found (including the base value) 49 while (left <right & list [right]> = baseNum) 50 right = right-1; 51 // Finally, elements smaller than baseNum are found, the only thing to do is to put this element in the base position 52 list [left] = list [right]; 53 // start from the left end of the array and start searching backwards, keep finding numbers larger than the base until (including equal base numbers) 54 while (left <right & list [left] <= baseNum) 55 left = left + 1; 56 // finally, an element larger than baseNum is found. The task is to put this element in the final position 57 list [right] = list [left]; 58} 59 // put baseNum in the left position 60 list [left] = baseNum; 61 // Finally, we find that the left value is smaller than left, the value on the right of the left position is 62 larger than that on left. So far, we have completed the first sorting 63 return left; 64} 65 public void QuickSort (List <int> list, int left, int right) 66 {67 // The left subscript must be smaller than the right subscript. Otherwise, the array will be split over 68 if (left <right) 69 {70, retrieve the reference number 71 int I = Division (list, left, right) of the next split; 72 // recursively cut a group of values on the left of the reference number, so that the values are fully sorted 73 QuickSort (list, left, I-1); 74 // recursively cut a set of values on the right of the reference label, so that 75 QuickSort (list, I + 1, right) are sorted in full; 76} 77} 78}
3.3 Efficiency Analysis
The quick sorting time is related to the symmetry of the Division. The average time complexity of fast sorting is o (n * logn). For the reason why o (n * logn), see chapter 7th of Introduction to algorithms, the recursive tree method is used in the book to describe the average time of fast sorting. And the constant factor is very small, so in terms of average time, fast sorting is a good internal sorting method. In the best case (symmetric for each division), the time complexity o (n * logn ). In the worst case (each division is asymmetrical, for example, when the input sequence is ordered or backward), the time complexity is o (n ^ 2 ), therefore, it is not recommended to use quick sorting when the sequence to be sorted is ordered or backward. In addition, quick sorting is unstable.
In the best case, each division is symmetric. Because the pivot value is not considered, the size of the two subproblems cannot be greater than n/2, at the same time, the quick sorting time is o (n), so the run time recursive expression:
T (n) <= 2 T (n/2) + o (n ). For more information about this recursive solution, see the analysis on the efficiency of Merge Sorting in the next blog. The solution is T (n) = o (n * logn ).
In the worst case, each division is asymmetrical. T (n) = T (n-1) + o (n) can be solved using a recursive tree, the cost of layer I is n-I + 1. there are n layers in total. N-1-1 n for each layer. Therefore, this recursive formula is interpreted as T (n) = o (n ^ 2), which is a bubble sort.