Sort (select, Hill, binary insert), and Hill binary insert

Source: Internet
Author: User

Sort (select, Hill, binary insert), and Hill binary insert

Select sorting method

1st times, pending sorting records r [1] ~ Select the smallest record from r [n] and exchange it with r [1]. For example, 2nd rows, in the record to be sorted, r [2] ~ Select the smallest record in r [n] and exchange it with r [2]. Similarly, the I-th record is in the record to be sorted r [I] ~ Select the smallest record in r [n] and exchange it with r [I] so that the ordered sequence continues to grow until all the records are sorted.

Initial sequence:{49 27 65 97 76 12 38}

Exchange between 12 and 49: 12{27 65 97 76 49 38}

2nd: 27: 12 27{65 97 76 49 38}

Exchange between 65 and 38: 12 27 38{97 76 49 65}

4th trip: 97 and 49 exchange: 12 27 38 49{76 97 65}

5th trip: 65 and 76 exchange: 12 27 38 49 65{97 76}

6th trip: 97 and 76 exchange: 12 27 38 49 65 76 97 completed

Code

Public class Sort {public static void main (String [] args) {int [] I = {1, 5, 6, 12, 4, 9, 3, 23, 39,403,596, 87}; System. out. println ("Result:"); xuanZe (I); System. out. println () ;}// select the sort algorithm public static void xuanZe (int [] x) {for (int I = 0; I <x. length; I ++) {int lowerIndex = I; // find the smallest index for (int j = I + 1; j <x. length; j ++) {if (x [j] <x [lowerIndex]) {lowerIndex = j ;}// exchange int temp = x [I]; x [I] = x [lowerIndex]; x [lowerIndex] = temp;} for (int I: x) {System. out. print (I + "");}}}

 

Time Complexity:O (N2).

Hill sorting

For an array of n elements, assume that the increment is h:

First trip: Starting from 1st elements and taking an element at intervals of h, we can finally get n/h elements, and sort these elements by inserting them directly.

Second trip: Starting from 2nd elements, take an element every h, just like the first trip.

...

Level h: Starting from the level h element, take an element every hour, just like the first one.

(At this time, the entire array is not ordered)

Then, reduce the value of h and repeat the above operation until h is reduced to 1 and sorting is completed.

Code

Public static void sort (int [] nums) {int len = nums. length/2; while (len> = 1) {for (int I = 0; I <len; I ++) {// directly Insert the sort into the group for (int k = I; k <nums. length-len; k + = len) {int j = k + len; int temp = nums [j]; while (k> = 0 & nums [k]> temp) {nums [j] = nums [k]; k-= len; j-= len;} nums [j] = temp;} len = len/2 ;}}

The time complexity isO (N * lgN).

Binary insertion sorting

The principle of Binary Search insertion sorting is a variant of direct insertion sorting. The difference is that, in order to reduce the number of element comparisons and improve the efficiency, the binary search algorithm is used to determine the insertion position.

Code

Public class BinarySearch1 {public static void main (String args []) {int array [] = {, 27}; binarySort (array, array. length); System. out. println (Arrays. toString (array);} // Binary Search public static int binarySearch (int array [], int low, int high, int temp) {int mid = 0; while (low <= high) {mid = (low + high)/2; if (array [mid] <temp & temp <= array [mid + 1]) return (mid + 1); else if (array [mid] <temp) low = mid + 1; else high = mid-1;} return high ;} // binary sorting public static void binarySort (int array [], int size) {int I, j, k, temp; for (I = 1; I <size; I ++) {temp = array [I]; if (array [I] <array [0]) k = 0; else k = binarySearch (array, 0, I, temp); for (j = I; j> k; j --) {array [j] = array [J-1];} array [k] = temp; System. out. println (Arrays. toString (array ));}}}

Time Complexity:O (N2); The space complexity isO (1 ).

I am the dividing line of tiantiao


Time complexity of various searches and sorting

Bubble Sorting is stable, and the algorithm time complexity is O (n ^ 2 ).

2.2 Selection Sort)

The basic idea of sorting is to process the order record sequence for n-1 times. The I-times processing is to swap the smallest person in L [I. n] with L [I. In this way, after I times, the position of the previous I record is correct.

The selected sorting is unstable, and the algorithm complexity is O (n ^ 2 ).

2.3 Insertion Sort)

The basic idea of insertion sorting is that after I-1 processing, L [1 .. I-1] has arranged the order. I-repeat only inserts L [I] into the proper position of L [1 .. I-1] so that L [1 .. I] is a sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L [I] and L [I-1], if L [I-1] ≤ L [I], then L [1 .. i] the order has been sorted, the I-th processing is over; otherwise, the position of switching L [I] and L [I-1] continues to compare L [I-1] and L [I-2], until a position j (1 ≤ j ≤ i-1) is found so that L [j] ≤ L [j + 1. Figure 1 demonstrates the insertion sorting process for the four elements, which requires three inserts: (a), (B), and (c.

Direct insertion and sorting are stable, and the algorithm time complexity is O (n ^ 2 ).

2.4 heap sorting

Heap sorting is A kind of tree-based sorting. During the sorting process, A [n] is considered as A Complete Binary Tree sequential storage structure, use the inner relationship between parent and child nodes in A Complete Binary Tree to select the smallest element.

Heap sorting is unstable, and the algorithm time complexity is O (nlog n ).

2.5 Merge Sorting

There are two ordered (ascending) sequences stored in the adjacent positions of the same array. It may be set to A [l .. m], A [m + 1 .. h], merge them into an ordered series, and store them in A [l .. h].

The time complexity is O (nlog2n) in both the best and worst cases ).

2.6 fast sorting

Quick sorting is an essential improvement of Bubble sorting. The basic idea is that after scanning, the length of the sorting sequence can be greatly reduced. In Bubble sorting, A scan can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may be reduced by 1. By performing a quick sorting scan, you can make sure that the numbers on the left of a certain number (based on it) are smaller than that on it, and the numbers on the right are larger than that on it. Then, we use the same method to process the numbers on both sides of it until there is only one element on the left and right of the benchmark.

Fast sorting is unstable, and the algorithm time complexity O (nlog2n) and Worst O (n ^ 2) are ideal ).

2.7 Hill sorting

In the direct insertion sorting algorithm, insert a number at a time to add only one node to the sequence, and it does not provide any help for inserting the next number. If a comparison is a long distance (called incremental) number that allows the number to move across multiple elements, then performing a comparison may eliminate the exchange of multiple elements. D. L. shell implemented this idea in the sorting algorithm named by him in 1959. The algorithm divides the number of groups to be sorted into several groups based on a certain increment d. The subscript difference between the records in each group is d. sort all the elements in each group, and then use a small increment to sort them in each group. When the increment is reduced to 1, the entire number to be sorted is divided into a group, and the sorting is completed.

Hill sorting is unstable, and its time complexity is O (n ^ 2 ).

Sorting type
Time Complexity
Space complexity
Stability

1
Insert sort
O (n2)
1


2
Hill sorting
O (n2)
1
×

3
Bubble Sorting
O (n2)
1


4
Select sort
O (n2)
1
×

5
Quick sorting
O (Nlogn)
O (logn)
×

6
Heap sorting
O (Nlogn)
1
×

7
Merge Sorting
O (Nlogn)
O (n)
√... Remaining full text>

Which algorithm is fixed for direct insertion and sorting?

Sorting algorithms include insertion, switching, selection, and merging.
Common insertion algorithms (direct, binary, and Hill insertion algorithms) are used to move data, locate the insertion point, and then exchange data for insertion.
The feature of the insert algorithm is how to find the insert point.
It should be said that the idea of directly inserting algorithms is fixed, no matter how you implement it.
You don't want to sort inserts. It's an exchange.

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.