Java sorting method

Source: Internet
Author: User

Java sorting method

This section describes the sorting method.



1. Bubble Sorting
2. Select sort
3. Insert sorting
4. Hill sorting
5. Quick sorting
6. Merge and sort



1. Bubble Sorting

Algorithm Description:

Set the number of records in the sequence to n
Generally, the sequence of the I-th bubble is from 1 to n-I + 1.
Compare the keywords of two adjacent records in sequence. If the reverse order occurs, it is exchanged.
The result is that, in the n-I + 1 record, the record with the largest keyword is exchanged to the position of n-I + 1. A maximum of N-1 troughs can be performed.


2. Select sorting

Algorithm Description:

First, we use n-1 comparisons to find the smallest number of n and exchange it with the first number -- the first row selects the order, and the smallest number of results is placed at the first element position.
By comparing the N-2 times, we can find the record with the small keyword times from the remainder n-1 number, and exchange it with the second number -- the second row selects the sorting.
Repeat the preceding process. The sorting is completed after a total of N-1 bytes are sorted.


Iii. Insert sorting

Algorithm Description

Records are stored in the array R [0 .... In n-1], R is divided into two subintervals at a certain time point in the sorting process. R [0... I-1] and R [I .... N-1], where: the former subinterval is an ordered area with sorted orders, and the latter subinterval is an unordered part.

Basic operations:

Insert the first record R [I] In the unordered zone to the ordered zone R [0 .... I-1] to make R [0... I] into a new ordered Zone

Operation Details:

When I (I ≥ 1) objects are inserted, r [0], r [1],…, R [I-1] has sorted.


4. Hill sorting

Algorithm Description:

First, set an integer of less than n. All elements whose distance is a multiple of the gap 1 are placed in the same group and sorted respectively in each group (the group adopts direct insertion sorting or other basic sorting methods ).
Then take the second increment So on, until the incremental gap is 1, that is, all elements are placed in the same group for sorting.

Algorithm analysis:

At the beginning, the gap value is large, and there are fewer elements in the subsequence, so the sorting speed is faster.
As the sorting progresses, the gap value gradually decreases, and the number of elements in the subsequence gradually increases. As most of the preceding elements are basically ordered, the sorting speed is still very fast.
The n Value After grouping is reduced, and the n value is smaller, while T (n) = O (n), so T (n) is reduced in general.
There are many methods to get the Gap. Shell proposes to get gap = n/2, gap = gap/2 ,..., Until gap = 1. If gap is odd, gap = gap + 1


5. Quick sorting

Algorithm Description:

Any record in the sequence to be sorted (for example, the first record) is used as the benchmark (pivot). Based on the keyword size of the record, the entire record sequence is divided into two subsequences: left and right.
The keywords of all records in the left subsequence are smaller than or equal to those of the benchmark record.
The keywords of all records in the subsequence on the right are greater than those of the benchmark record.
The benchmark record is placed in the middle of the two subsequences (which is also the final position of the record ).
Then the above methods are repeated for the two subsequences until all the records are placed in the corresponding positions.

A benchmark record is also called a pivot (or pivot) record. Take the first record of the sequence as the pivot record, and its keyword is Pivotkey. The pointer low points to the first record position of the sequence, and the pointer high points to the last record position of the sequence.



Algorithm analysis:

Quick sorting is a recursive process. The number of shards in quick sorting depends on the height of the recursive tree.
If the left-side sub-sequence of the record is the same as the right-side sub-sequence after a record is located, the next step is to sort the two sub-sequences whose length is halved, this is the ideal situation.


Vi. Merge Sorting

Algorithm Description:

If the initial sequence contains n records, it can be regarded as n ordered subsequences. The length of each subsequence is 1.
Merge them to obtain n/2 ordered subsequences with a length of 2 or 1.
Merge them in two ,...... This is repeated until an ordered sequence with a length of n is obtained.



Package cn. hncu. sortMethods; public class SortMethods {/* elements in the output array */private static void print (int [] a) {for (int num: a) {System. out. print (num +);} System. out. println ();} private static void swap (int [] a, int I, int j) {int temp; temp = a [I]; a [I] = a [j]; a [j] = temp;} public static void main (String [] args) {int a [] = {0, 21, 25, 49, 25,16, 15,8,-,-9, 67, 34,5,}; // 1-1 bubble sort bubbleSort (a); // 1-2 optimized bubble sort // bubbleSort2 (); // 2 select sorting // selectSort (a); // 3-1 insert sorting directly // insertSort (a); // 3-2 use binary optimization insert sorting // binaryInsertSort (); // 4 Hill sorting // shellSort (a); // 5 quick sorting // quickSort (a, 0,. length-1); // 6 merge sort // mergeSort (a, 0,. length-1); print (a);} private static void mergeSort (int [] a, int left, int right) {if (left
Mid) {// The left subsequence has been completed. So the rest is the right sequence, and the remainder of the copy right sequence can be for (int I = r; I <= right; I ++) {B [k ++] = a [I] ;}} else {// copies the remainder of the Left subsequence for (int I = p; I <= mid; I ++) {B [k ++] = a [I] ;}} private static void quickSort (int [] a, int p, int r) {if (p
A [p, Q-1], a [q], a [q + 1, r] int q = partition (a, p, r ); // a [q] quickSort (a, p, q-1); // The left-side sub array a [p, Q-1] quickSort (a, q + 1, r ); // subarray a [q + 1, r]} private static int partition (int [] a, int p, int r) on the right of the row {// optimized: use the random selection policy to determine pivot-Randomly select an element in the array and exchange it with the first element. Then, use the first element as the pivot to divide int rand = (int) (Math. random () * (r-p); // rand is the random number (number of) in the interval // p = 10000, r = 15000 swap (a, p, p + rand); int I = p; // The first element is set to pivot int j = r + 1; int x = a [p]; while (true) {while (a [++ I]
X); // positioning pointer j <-- to find the illegal element to switch to the left if (I >= j) {break;} swap (a, I, j );} swap (a, p, j); // pivot a [p] to switch to the center position return j;} // 4-hill sort private static void shellSort (int []) {// grouping, the initial step is set to half the length of the array, that is, n/2, and then halved until 1for (int gap = (. length + 1)/2; gap> 0;) {// intra-group sorting for (int I = 0; ia [j + gap]) {swap (a, j, j + gap) ;}}// for Loop Correction if (gap> 1) {gap = (gap + 1)/2;} else if (gap = 1) {break ;}}// use binary optimization to insert the sorted private static void binaryInsertSort (int a []) {for (int I = 0; ite Mp) {// if the number of objects to be inserted is smaller than the intermediate element a [mid], the target is located in the left half area high = mid-1 ;} else {// if the number of objects to be inserted is greater than or equal to the value of intermediate element a [mid], the target falls in the right half of the region low = mid + 1 ;}} // The high Location of the last cell to be searched is the target, therefore, the insert position of the new element is high + 1 // move the elements from high to I one by one for (int j = I; j> high; j --) {a [j + 1] = a [j];} a [high + 1] = temp ;}} // 3-1 insert the sorted private static void insertSort (int [] a) {for (int I = 0; itemp, move a [j] to the position j + 1 and continue searching. While (a [j]> temp) {a [j + 1] = a [j]; j --; if (j <0) {break ;}} // otherwise, when a [j] <= temp, place the number to be inserted directly at a [j + 1] a [j + 1] = temp ;}} // 2 select the sort private static void selectSort (int [] a) {for (int I = 0; ia [j + 1]) {// ascending swap (, j, j + 1) ;}}}/* 1-2 Bubble Sorting optimization */private static void bubbleSort2 (int [] a) {boolean flag = false; // indicates that the entire sequence has not been ranked. // run n-1 times. Each time a number is arranged, the last one does not need to be arranged for (int I = 0; ia [j + 1]). {swap (a, j, j + 1); // if swap exists, the previous assumption is false;} if (flag) {// if the previous assumption is that the entire sequence has been sorted immediately, the function returns return ;}}}}








Related Article

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: 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.