Algorithm-sort (bubble, select, insert, fast)

Source: Internet
Author: User

I. The bubble sort 1.1 concept repeatedly visited the series to be sorted and compared two elements at a time. If their order is wrong, they will be exchanged. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. 1.2 operation 1. Compare adjacent elements. If the first is bigger than the second, exchange the two of them. 2. perform the same operation on each adjacent element, starting from the first pair to the last one at the end. At this point, the final element should be the largest number. 3. Repeat the preceding steps for all elements except the last one. 4. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number. Mean Time Complexity: Worst space complexity: Best algorithm: Least (n²) least (n²) NO1.3 code [java]/*** bubble sort * searches the entire value column and compares adjacent elements, if the relative order of the two is incorrect, exchange them * @ param numbers to sort data * @ return */private void maoPao (int [] numbers) {for (int I = numbers. length-1; I> = 0; I --) {// loop all unordered data for (int j = 0; j <I; j ++) {// compare if (numbers [j]> numbers [j + 1]) {int temp = numbers [j]; numbers [j] = numbers [j + 1]; numbers [j + 1] = temp ;}}ii. Select sorting 2.1. First, find the minimum element in the unordered sequence and store it to the starting position of the sorting sequence. Then, then from the remaining unordered Elements And put it at the end of the sorting sequence (currently sorted ). And so on until all elements are sorted. 2.2 operation 1. Find the largest first place in all sequences. 2. Remove the first place, find the largest second place among all the remaining sequences. 3. Similarly, until sorting is completed. Average time complexity: Worst space complexity. Best algorithm: round (n²) round (n) total, O (1) auxiliary occasionally shows code 2.3 [java]/*** select sorting: * search for the entire value column to find the minimum value. Exchange the value with the value at the first position in the value column. * Search for the remaining value column (except the first one) to find the minimum value, and then exchange it with the value at the second position in the value column. * @ Param numbers: Data to be sorted * @ return */private void xuanZe (int [] numbers) {for (int I = 0; I <numbers. length; I ++) {int max = I; for (int j = I + 1; j <numbers. length-1; j ++) {if (numbers [j] <numbers [max]) {max = j ;}} if (max! = I) {int temp = numbers [I]; numbers [I] = numbers [max]; numbers [max] = temp ;}}} 3. Insert sort 3.1 the concept of working principle is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position and insert it. Insert sorting usually uses in-place sorting (that is, sorting of the extra space of O (1). Therefore, during the scanning from the back to the forward, the sorted elements need to be moved backward repeatedly to provide the insert space for the new elements. 3.2 operation 1. Starting from the first element, this element can be considered to have been sorted. 2. The next element is retrieved, scan the sorted element sequence from the back to the front 3. If the element (sorted) is greater than the new element, move the element to the next position 4. Repeat Step 3, wait until the sorted element is located at a position smaller than or equal to the new element. 5. Insert the new element to this position. 6. Repeat Step 2 ~ 5. The average time complexity is the worst. The best algorithm for spatial complexity is round (n²) Total O (n), which requires auxiliary space O (1) No [java]/*** insert sorting: * sort the first two orders, and then sort the first three orders and the first two orders, sort the data until the sorting ends * @ param numbers * @ return */private void chaRu (int [] numbers) {for (int I = 1; I <numbers. length; I ++) {int one = numbers [I]; for (int j = I-1; j> = 0; j --) {if (numbers [j]> one) {numbers [j + 1] = numbers [j]; numbers [j] = one ;}}}} iv. Hill sorting 4.1 concept 4.2 operation the hill sorting method is proposed based on the following two properties of insert sorting: 1. Insert sorting is almost sorted The efficiency of data operations is high, that is, the efficiency of Linear sorting can be achieved. 2. However, insertion sorting is generally inefficient, because insertion and sorting can only move one digit at a time <The average time complexity is the worst space complexity. The best algorithm varies depending on the step sequence. O (n) non-optimal algorithm 5. Fast sorting 5.1 concept fast sorting using Divide and conquer policies) sub-lists is divided into two sub-sequences ). Step: 1) set two variables I and J. When sorting starts: I = 1, J = N; 2) use the first array element as the key data and assign it to X, that is, X = A [1]; 3) search forward from J, that is, search forward from the back (J = J-1), find the first value less than X, exchange this value with X. 4) Search backward from I, that is, search backward from the beginning (I = I + 1) and find the first value greater than X, let this value exchange with X; 5) Repeat steps 3rd and 4 until I = J; the bottom of recursion is that the number of columns is zero or one, that is, it is always sorted. Although this algorithm is always recursive, it always exits, because in each iteration, it will at least place an element at its final position. 5.2 average operational time complexity worst space complexity optimal algorithm random (n log n) varies depending on implementation methods and sometimes [java] public void kuaiSu (int [] pData, int left, int right) {int temp; int I = left; int j = right; int middle = pData [left]; while (true) {while (++ I) <right-1 & pData [I] <middle); while (-- j)> left & pData [j]> middle); if (I> = j) break; temp = pData [I]; pData [I] = pData [j]; pData [j] = temp;} pData [left] = pData [j]; pData [j] = middle; if (left <j) kua ISu (pData, left, j); if (right> I) kuaiSu (pData, I, right );} 6. Merge and sort 6.1 combine two sorted sequences into one operation. The Merge Sorting Algorithm depends on the merge operation. 6.2 The merge operation process is as follows: 1. Apply for a space so that the size is the sum of the two sorted sequences. This space is used to store the merged sequence. 2. set two pointers, the initial position is the start position of the two sorted sequences respectively. 3. Compare the elements pointed to by the two pointers. Select a relatively small element and place it in the merging space, move the pointer to the next position. 4. Repeat Step 3 until a pointer reaches the end of the sequence. 5. Copy all the remaining elements of the other sequence directly to the end of the merging sequence. Average time complexity: Worst space complexity. optimal Algorithm round (n log n) O (n) is sometimes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.