The code implemented for each algorithm was not uploaded separately to GitHub, and needed to be downloaded in the group< Welcome to join iOS Development Learning Exchange Group:qq529560119>
Data structure and algorithm of the role: data structures and algorithms equivalent to the heart, and a variety of programming languages equivalent to martial arts of various moves such as the Dragon 18 palm and so on. Internal strength heart more solid, play out of the moves will be more hurt.
Sorting is a frequent operation within a computer that is designed to adjust a set of "unordered" record sequences to an "ordered" sequence of records.
The sorting algorithm is the most common algorithm in all the algorithms, for example, in the school physical education, queued according to height from low to high, this is a sort, college entrance examination according to the score from high to low descending admission, which is a sort of.
Stability of sequencing: Assume Ki>kj (1<=i<=n,1<=j<=n,i!=j), and Ri is ahead of RJ (i.e. i< j) in the sequence before sorting. If the sorted Ri is still ahead of RJ, it is said that the sorting method used is stable. Conversely, if it is possible to make RJ in the sorted sequence ahead of the RI, it is said that the sorting method used is stable. 1 Little son 700 2 small lifting 6803 small plum 7004 small Zhang Zihe 650
Stable sort: 1 little 700 3 small plums 7002 small lifting 6804 small Zhang Zihe 650
Several elements that affect sorting performance: Time performance: Compare and move, a good sort to minimize comparisons and move. Secondary space: Some secondary space is required to hold some temporary data. The complexity of the algorithm: The complexity of the algorithm itself, not the complexity of time.
The algorithm is categorized into internal and external sorts. If the entire ordering process does not require access to external memory, then this sort problem is called an internal sort. Conversely, if the number of records participating in a sort is large, the ordering of the entire sequence cannot be done in memory, then the sort problem is called an external sort.
Today we're going to talk about some of the classic sorting Algorithms for internal sorting: a simple sort: bubble sort, select sort, insert sort, and these algorithms improve after the hill sort, heap sort, quick sort.
Bubble sort Basic idea: 22 The key of the adjacent record, if the reverse order is exchanged until there is no reverse order of the records. is the "compare" and the "swap" of the record position between the records in the unordered sequence area to achieve a low-key record to drift, while the key is larger to the other end of the sink. Basic points: 1.22 note is the meaning of the adjacent two elements. 2. If there are n elements that need to be compared to n-1 times, each round is reduced by 1 times. 3. The end condition of the bubble sort: not carried out in a certain sequencing process.------Code-----Select the basic idea of sorting: by comparing n-i sub-keywords, the smallest records of the keywords are selected from the N-i+1 records and exchanged with the first (I<=i<=n) records. ---on the code---
Direct insertion Sorting basic idea: Insert a record into an ordered table that is already sorted, and get a new, sequential table with a 1 increase in the number of records. ---on the code---
Hill sort basic idea: the direct insertion algorithm has the advantage of direct insertion when the record is basically ordered or the number of records is few. These two conditions are harsh, both of which exist under special circumstances. This time a professor named Hill did something, grouping a record into several sub-sequences, and then making a basic insertion sort of several subsequence, so that the time it takes to sort a number of sub-sequences is less than the time it takes to sort the entire record. For example, a full record a direct insert sort takes 10 hours, but Prof Hill divides the sequence into three sub-sequences cde,cde three sub-sequences are 3,3,3 hours, which add up to just 9 hours, less than 10 hours. Look at this picture, do you understand? ---Look at the code---
The basic idea of heap sorting: We first recall the selection sort, select the smallest record each time, and finally become a complete and orderly record. So how many times do you need to compare the smallest records in the N records to be sorted? The answer is simple: n-1 times, why so many times? Because the comparison of one element does not make good use of the result of the previous comparison, the whole algorithm is compared many times, which results in the lower efficiency. In the improved algorithm of bubble sorting, the result of the last comparison is well utilized, and a flag variable is added to avoid a lot of repeated comparisons, so here the heap sort is a good real instance using the result of the previous comparison.
Here we first introduce the concept of a complete binary tree: If a binary tree depth is n, then the N-1 layer is completely symmetrical, and the nth layer is successively arranged from left to right, for example. Large heap Concept: the value of each node is greater than or equal to the value of the left and right nodes. Small top heap Concept: Each node's value is less than or equal to the value of the left and right nodes. Important: 1. From the above, we can conclude that the root node must be the largest or smallest of all nodes in the heap, and if the nodes are numbered starting from 1 in the way of sequence traversal, the nodes meet the following relationship: Ki>=k2i and ki>=k2i+1, or Ki<=k2i and ki< =k2i+1.2. Subscript I with 2i and 2i+1 is the relationship between parents and children. 3. Then the large top heap and the small top heap with the program traversal into the array, you must satisfy the following expression: Heap ordering is the use of heap sorting algorithm, its basic idea is: 1. The sequence to be sorted into a large top heap or a small top heap. 2. At this point the maximum value of the entire sequence is the root node of the heap top. To remove it. is to swap it with the end element of the heap array, at which point the element at the end is the maximum value. 3. The remaining N-1 sequences are then re-formed into a heap, which gives the maximum value in n elements. 4. You can get an orderly sequence in this way again and again.
Sorting algorithm (OC version)