Data structure-Simple sorting

Source: Internet
Author: User

In our business, there are a lot of things that need to be sorted against the data to be returned, but sorting is a very time-consuming operation, especially when the amount of data is large, and we sometimes say that the sort of data is important, but it is also very time consuming. Here is a brief introduction to simple sorting, these sorting algorithms perform slowly, but the algorithm logic is simple, at some point, more efficient than other complex sorting algorithms, but also can help us understand other complex sorting algorithms.

First, bubble sort

Bubble algorithm collation: Repeat the two adjacent elements, if the first one is larger than the second, swap the position of two elements, repeat this action, you can move the maximum value to the back. A trip (from the first element to be compared to the last one to compare) can select the largest element in a non-comparable sequence and repeat until the last element that is not to be compared, to complete the sort.

The steps for the bubbling algorithm are as follows:

    • Calculates the size n of the array A to sort, and the array subscript starts at 0.
    • Sets a flag that identifies the sorted array subscript position, which is i=n-1 (meaning that the sort has not been started).
    • Make j=0,1,2.....i-1, loop compare the size of a[j] and a[j+1], and if a[j]>a[j+1], then swap the values of both.
    • Make i=i-1, loop the last step of processing until i<0.

Complexity of Time:

Regardless of the initial state of the array, it is necessary to n-1 the order, each order comparison n-j times, if the initial state is sorted, then the number of moves is 0. If the initial state is reversed, then the move is 3 times per comparison. The calculation of the time-to-responsibility calculation is as follows: Suppose the comparison number C and the number of moves m.

Cmin = N (n-1)/2 = O (n2), mmin = 0

Cmax = N (n-1)/2 = O (n2), Mmax = 3n (n-1)/2 = O (n2)

So the complexity of time is compared to O (N2)

The code is as follows:

1   /**2 * Bubble Sort <br/>3 * Invariant, the portion of coordinates greater than I is always ordered, that is, the arrangement of this part will not change in the sorting process4      * 5      * @paramArray6      */7      Public voidBubblesort (Double[] Array) {8         if(Array = =NULL|| Array.Length < 2) {9             return;Ten         } One  A         intSize =Array.Length; -          for(inti = size-1; I >= 0; i--) { -              for(intj = 0; J < I; J + +) { the                 if(Double.compare (Array[j], array[j + 1]) > 0) { -                     //Swap the Datas -ARRAY[J] = Array[j] + array[j + 1]; -Array[j + 1] = Array[j]-array[j + 1]; +ARRAY[J] = array[j]-array[j + 1]; -                 } +             } A         } at}
Bubble Sort Code

There is a variant for the bubble sort, and the following code, after selecting the maximum value, moves the unordered minimum value in the array to the front of the array.

1     /**2 * Bubble Sort <br/>3 * Invariant, the portion of coordinates greater than I is always ordered, that is, the arrangement of this part will not change in the sorting process4      * 5      * @paramArray6      */7      Public voidTwowaybubblesort (Double[] Array) {8         if(Array = =NULL|| Array.Length < 2) {9             return;Ten         } One  A         intleft = 0; -         intright = Array.length-1; -          for(inti = right; I >= left; i--) { the             intj =Left ; -  -              for(; J < I; J + +) { -                 if(Double.compare (Array[j], array[j + 1]) > 0) { +                     //Swap the Datas -ARRAY[J] = Array[j] + array[j + 1]; +Array[j + 1] = Array[j]-array[j + 1]; AARRAY[J] = array[j]-array[j + 1]; at                 } -             } -  -              for(j = j-1; j > left; j--) { -                 if(Double.compare (Array[j-1], array[j]) > 0) { -                     //Swap the Datas inARRAY[J] = Array[j] + array[j-1]; -ARRAY[J-1] = array[j]-array[j-1]; toARRAY[J] = array[j]-array[j-1]; +                 } -             } theleft++; *         } $}
View Code

Second, choose the sort

Select Sort is an improvement on the bubbling sort, with the number of exchanges reduced from O (N2) to O (n), but the number of comparisons is O (N2). In the Java language, this optimization performance is not as high as bubbling, but in other languages that manipulate memory, the choice of sorting is much faster than bubbling sorting. The Java language Exchange value simply changes the reference, without operating the underlying operations such as memory movement, all of which have little effect.

Select a collation: Just like bubble sort, simply place each trip in the sort, without exchanging data, just choose where to handle the largest element, i.e. subscript.

Select the algorithm for sorting:

    • The array is divided into two regions: ordered and unordered, the initial state is empty and the unordered area is A[1...N].
    • For the first order of 0<i<n, select a maximum value in the unordered area A[i,n], i.e. a[k], and exchange values for A[K] and A[i].
    • i++, A[1...i-1] is set to an ordered area, the A[I...N] is set to unordered, and the previous operation is looped until the i=n ends the sort.

The average complexity is O (N2), as is the time complexity and bubble sort.

The implementation code is as follows:

1     /**2 * Select Sort <br/>3 * Invariant, the portion of coordinates less than I is always ordered, that is, the arrangement of this part will not change in the sorting process4      * 5      * @paramArray6      */7      Public voidSelectsort (Double[] Array) {8         if(Array = =NULL|| Array.Length < 2) {9             return;Ten         } One  A         intSize =Array.Length; -         intMinindex = 0; -         DoubleTmpvalue =-1; the  -          for(inti = 0; i < size; i++) { -Minindex =i; -              for(intj = i + 1; J < size; J + +) { +                 if(Double.compare (Array[minindex], array[j]) > 0) { -Minindex =J; +                 } A             } at  -             //Swap the Datas -Tmpvalue =Array[i]; -Array[i] =Array[minindex]; -Array[minindex] =Tmpvalue; -         } in}
Select Sort Code

Third, insert sort

Insert sorting rules: Insert a data into the ordered data that has been sorted, so as to get a new sequential data with a number plus one, the algorithm is suitable for the ordering of a small amount of data, the time responsible degree or O (N2), is a stable sorting algorithm.

Algorithm for inserting sorting:

    • The array to be sorted is divided into two groups, sorted and unsorted, with the initial state: The sorted set is a[0], and the unsorted is a[1...n-1].
    • In the 0<i<n order, select the first element A[i] in the unordered list A[I...N], insert it into the list of sort numbers, or insert a list of ordinal numbers in order. Form a sorted set for A[0...I], for a sorted set of a[i+1...n-1]. The loop is processed until the i=n is reached. Ends the sort operation.

Time of Responsibility or O (N2)

The implementation code is as follows:

1   /**2 * Insert Sort, mainly local order. 3      * 4      * @paramArray5      */6      Public voidInsertsort (Double[] Array) {7         if(Array = =NULL|| Array.Length < 2) {8             return;9         }Ten  One         intSize =Array.Length; A         intj = 0; -          for(inti = 0; i < size; i++) { -             DoubleTmpvalue =Array[i]; thej =i; -              while(J > 0 && double.compare (array[j-1], tmpvalue) > 0) { -ARRAY[J] = array[--j]; -             } +ARRAY[J] =Tmpvalue; -         } +}
Insert Sort Code

Data structure-Simple sorting

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.