Sorting of data structures

Source: Internet
Author: User
Tags rounds

1. Insert Sort1.1 Direct Insert SortDirect insertion sorting is the insertion of unsorted data into an appropriate position in the ordered sequence.  The process is as follows: 1, first compare the array of the first two data, and sort, 2, compare the third element with the first two rows of data, and put the third element in the appropriate position, 3, compare the fourth element with the first three ordered data, and put the fourth element into the appropriate position;   4, until the last element into the appropriate position.  If there is initial data: 25 11 45 26 12 78.  1, first compare the size of 25 and 11, 11 small, position interchange, the first round after sorting, the order is: [11, 25, 45, 26, 12, 78].  2, for the third data 45, which is greater than 11, 25, so the position is unchanged, the order is still: [11, 25, 45, 26, 12, 78].  3, for the fourth data 26, which is greater than 11, 25, less than 45, so it is inserted between 25 and 45, the order is: [11, 25, 26, 45, 12, 78].  .......   4. The final order is: [11, 12, 25, 26, 45, 78]. The direct insert sort is stable. The average time complexity of the direct insert sort is O (N2). The Java code is implemented as follows:
1 public     void sort (int[] arr) {2         int tmp, 3 for         (int i = 1; i < arr.length; i++) {4             //data to be inserted 5             tmp = Arr[i]; 6             Int J; 7             for (j = i-1; J >= 0; j--) {8                 //judgment is greater than TMP, greater then one 9                 if (Arr[j] > tmp) {ten                     arr[j+1 ] = arr[j];11                 }else{12                     break;13                 }14             }15             arr[j+1] = tmp;16             System.out.println (i + ":" + Arrays.tostring (arr));         }18     }
1.2 Hill Sort (minimum incremental sort)

Hill sort is strictly based on the idea of insertion sorting, also known as narrowing the incremental sort.

The process is as follows: 1, an array containing n elements, divided into N/2 sequence, the first data and the n/2+1 data is a pair ...      2, the comparison and exchange of each pair of data, arranged in order; 3, then divided into N/4 sequence, again sorted, 4, repeating the above process, with the sequence reduced and up to 1, sorting completed.  If there is initial data: 25 11 45 26 12 78.  1, the first round of sorting, the array is divided into 6/2=3 sequence, the 1th data and 4th data is a pair, 2nd data and 5th data is a pair, 3rd data and 6th data is a pair, each comparison of the data sorted, sorted after the order: [25, 11, 45, 26, 12, 78].   2, the second round of sorting, will be sorted on the upper line of the array into a sequence of 6/4=1, at this time, one by one to compare the data, sorted by the insertion sort of the array, the order is: [11, 12, 25, 26, 45, 78]. For the insertion sort, if the original array is basically ordered, the sorting efficiency can be greatly improved. In addition, the use of direct insertion sorting for a smaller number of sequences increases the efficiency of the data that needs to be moved less.  As a result, hill sequencing has a high execution efficiency. Hill sort is not stable, O (1) extra space, time complexity is O (n (logn) ^2).
1 public     void sort (int[] arr) {2         //I means the n/2+1 element in the hill sort (or n/4+1) 3         //J represents the element in the hill sort from 0 to N/2 (N/4) 4         // R represents the value of N/2+1 or n/4+1 in Hill sort 5         int i, J, R, TMP, 6         //Stroke Group 7 for         (r = ARR.LENGTH/2; r >= 1; r = R/2) {8             F or (i = r; i < Arr.length; i++) {9                 tmp = arr[i];10                 j = i-r;11                 //One-round sequencing                 , J >= 0 && TMP < ARR[J]) {                     Arr[j+r] = arr[j];14 J-=                     r;15                 }16                 arr[j+r] = tmp;17             }18             SYSTEM.OUT.PRINTLN (i + ":" + arrays.tostring (arr));         }20     }
2. Exchange sorting2.1 Bubble Sort

Principle: compare two adjacent elements and exchange large value elements to the right end.

Idea: Compare adjacent two numbers in turn, place decimals in front, and large numbers on the back. That is, in the first trip: first compare the 1th and 2nd numbers, put the decimals before the large number. Then compare the 2nd and 3rd numbers, place the decimal before the large number, and then continue until you compare the last two numbers, put the decimals before the decimal, and put the large number behind. Repeat the first step until all sorts are complete.

Example: To sort an array: int[] arr={6,3,8,2,9,1};

First trip Sort:

First Order: 6 and 3 comparison, 6 greater than 3, swap position: 3 6 8 2 9 1

Second Order: 6 and 8 comparison, 6 less than 8, not swap position: 3 6 8 2 9 1

Third Order: 8 and 2 comparison, 8 greater than 2, swap position: 3 6 2 8 9 1

Fourth order: 8 and 9 comparisons, 8 less than 9, not swapped position: 3 6 2 8 9 1

Fifth Order: 9 and 1 comparison: 9 greater than 1, swap position: 3 6 2 8 1 9

The first trip was a total of 5 comparisons, sorted results: 3 6 2 8 1 9

---------------------------------------------------------------------

Second trip sort:

First Order: 3 and 6 comparison, 3 less than 6, not swap position: 3 6 2 8 1 9

Second Order: 6 and 2 comparison, 6 greater than 2, swap position: 3 2 6 8 1 9

Third Order: 6 and 8 comparison, 6 greater than 8, not swap position: 3 2 6 8 1 9

Fourth order: 8 and 1 comparisons, 8 greater than 1, Exchange position: 3 2 6 1 8 9

The second trip took a total of 4 comparisons, sorted results: 3 2 6 1 8 9

---------------------------------------------------------------------

Third trip Sort:

First Order: 3 and 2 comparison, 3 greater than 2, swap position: 2 3 6 1 8 9

Second Order: 3 and 6 comparison, 3 less than 6, not swap position: 2 3 6 1 8 9

Third Order: 6 and 1 comparison, 6 greater than 1, swap position: 2 3 1 6 8 9

The second trip took a total of 3 comparisons, sorted results: 2 3 1 6 8 9

---------------------------------------------------------------------

Sequence Four:

First Order: 2 and 3 comparison, 2 less than 3, not swap position: 2 3 1 6 8 9

Second order: 3 and 1 comparison, 3 greater than 1, swap position: 2 1 3 6 8 9

The second trip took a total of 2 comparisons, sorted results: 2 1 3 6 8 9

---------------------------------------------------------------------

The order of five times:

First Order: 2 and 1 comparison, 2 greater than 1, swap position: 1 2 3 6 8 9

The second trip took a total of 1 comparisons, sorted results: 1 2 3 6 8 9

---------------------------------------------------------------------

Final Result: 1 2 3 6 8 9

---------------------------------------------------------------------

This shows: N numbers to sort complete, a total of N-1 sequencing, every I trip to the order of (n-i) times, so you can use the double loop statement, the outer control loop how many times, the inner layer control each trip cycle times, namely

    public static void Bubblesort (int[] arr) {        int temp;//defines a temporary variable for        (int i=0;i<arr.length-1;i++) {//bubbling number of            times for (int j=0;j<arr.length-i-1;j++) {                if (Arr[j+1]<arr[j]) {                    temp = arr[j];                    ARR[J] = arr[j+1];                    ARR[J+1] = temp;}}}}    
2.2 Quick Sort

Just look at the above explanation is still a little vague, you can understand it through an example, the following is a set of data to analyze the sorting process:

Original array:{3,7,2,9,1,4,6,8,10,5}

Expected Result:{1,2,3,4,5,6,7,8,9,10}

It took a little time to masturbate. This quick sort:

Demo Step parsing:

1. At the beginning of the selection of the last element of the array 5 as the benchmark value, that is, the final sorting result should be divided by 5 to the left and right sides.

2. Starting from the left, look for a value greater than 5, and then swap with 5 (because if the value of the smaller than 5 is supposed to be in front of 5, after the value of 5 is changed to go to the back of 5), all the way to find 7, the 7 and 5 exchange, the end of this traversal.

3. Starting from the right, since 7 is already the last round of the sequence will not move it, starting from 10, all the way to the left, looking for a value smaller than 5, and then swapped with 5 (because if the value of 5 than the original should be ranked behind 5, the value of 5 smaller than after the exchange went to the rear of 5), came to find 4, Swap 4 with 5 to end this traversal.

4. Starting from the left, since the 3 and 4 are the first two rounds have been ordered to no longer move it, starting from 2, all the way to the right, looking for a value greater than 5, and then exchange with 5 (for the same step 2), came all the way to find 9, 9 and 5 Exchange, the end of this traversal.

5. Starting from the right, because the number of numbers in the back of 9 is the last few rounds of the sequence will not move it, starting from 1, all the way to the right, looking for a smaller than 5 value, and then exchange with 5 (the same step 3), suddenly found 1, 1 and 5 exchange, the end of this traversal.

6. This time, found that 5 of the left and right sides are lined up, so the end of the order, 5 of the left and right sides of the next round of the order, the rules ibid, until it can not be split down, that is, complete the overall quick sorting.

Java Code
  1. /**
  2. * Quick Sort
  3. * @author It_zjyang
  4. */
  5. Public class QuickSort {
  6. /** 
  7. * Divide an array of elements, small on the left, large on the right
  8. * @param a
  9. * @param start
  10. * @param end
  11. * @return
  12. */
  13. public static int divide (int[] A, int start, int end) {
  14. //each with the rightmost element as the base value
  15. int base = A[end];
  16. once the//start equals end, it means that the left and right two pointers are merged into the same position and can end the round cycle.
  17. While (Start < end) {
  18. While (Start < end && A[start] <= base)
  19. //Traverse from the left, if it is smaller than the reference value, continue to the right.
  20. start++;
  21. at the end of the while loop above, it means that the value of the current A[start] is larger than the base value and should be exchanged with the base value
  22. if (Start < end) {
  23. //Exchange
  24. int temp = A[start];
  25. A[start] = A[end];
  26. A[end] = temp;
  27. //Exchange, at this time the value of the exchange is also transferred to the correct position (the reference value to the right), so the right side also to move forward one
  28. end--;
  29. }
  30. While (Start < end && A[end] >= base)
  31. //From the right to start the traversal, if it is larger than the reference value, continue to go to the left
  32. end--;
  33. at the end of the while loop above, it means that the value of the current A[end] is smaller than the base value and should be exchanged with the base value
  34. if (Start < end) {
  35. //Exchange
  36. int temp = A[start];
  37. A[start] = A[end];
  38. A[end] = temp;
  39. //After the exchange, the value of the swap at this time is also transferred to the correct position (the reference value to the left), so the left side should also move back one
  40. start++;
  41. }
  42. }
  43. //Return to start or end, where start and end are the datum values
  44. return end;
  45. }
  46. /** 
  47. * Sort
  48. * @param a
  49. * @param start
  50. * @param end
  51. */
  52. public static void sort (int[] A, int start, int end) {
  53. if (Start > End) {
  54. //If there is only one element, then there is no need to go down the line.
  55. return;
  56. }
  57. else{
  58. //If there is more than one element, continue dividing both sides recursively sort down
  59. int partition = Divide (A, start, end);
  60. Sort (A, start, partition-1);
  61. Sort (A, partition+1, end);
  62. }
  63. }
  64. }

Sorting of data structures

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.