Eight common methods of sorting in Java

Source: Internet
Author: User

Recent data found Java sorting is very interesting, including the common eight kinds of representative sorting method; I think the function of sorting is important, but more important is the idea of sorting; so simply describe the common sort method name and use the code example.

A. Insert sort (Direct insert sort, hill sort);
B. Exchange sort (bubble sort, quick sort);
C. Select sort (Direct select sort, heap sort);
D. Merge sort;
E. Allocation ranking (Cardinal sort);

Maximum required auxiliary space: merge sort;
Minimum required auxiliary space: heap sequencing;
Fastest speed: fast sorting;

Unstable: Quick sort, hill sort, heap sort.

Code block:

Package Com.sinolife.mtrs.apply.controller;

Import Java.util.Arrays;

/**
* @author Delin Li
* @version createtime:2017-12-7 04:10:37
* @Description
*/
public class Testsort {

/**
* @param args
*/
public static void Main (string[] args) {
int[] ins = {1,3,53,7,81,2,5,71,9};
Insertsort (INS);//Direct Insert Sort
Shellsort (INS);//Hill Sort,
Sampleselectsort (INS);//Simple selection sort
Heapsort (INS);//Heap sort
Bubblesort (INS);//Bubble sort
Quiksort (ins,0,ins.length-1);//Quick Sort
MergeSort (ins,0,ins.length-1);//merge sort
Radixsort (ins,10,2);//Base sort
print (INS);
}

public static void print (int[] data) {
for (int i = 0; i < data.length; i++) {
System.out.print (Data[i] + "");
}
}

Direct Insert Sort
/*public static void Insertsort (int arg[]) {
for (int i=1;i<arg.length;i++) {//To use the first number as a sorted value, and then insert the number of unordered numbers in the sorted list
Int J;
int temp = arg[i];//inserted value
for (j=i;temp<arg[j-1]&&j>0;j--) {
ARG[J] = arg[j-1];////through the loop, one after the other to find the location to insert.
}
arg[j]=temp;//Insertion
}
}*/


Hill
/*public static void Shellsort (int a[]) {
int n = a.length;
for (int i = n/2;i>0; i/=2) {
for (int j = i;j<n;j++) {//each element is directly inserted into the sort with data within its own group
if (A[j]<a[j-i]) {
int temp = A[j];
int k = j-i;
while (k>=0&&a[k]>temp) {
A[k+i] = a[k];
K-=i;
}
A[k+i] = temp;
}
}
}
}*/

Simple selection sorting
/*public static void Sampleselectsort (int[] a) {
int minv;//temporarily Save minimum value
int mini;//Temporary Save minimum subscript
for (int i= 0;i<a.length-1;i++) {
MINV = A[i];
MinI = i;
for (int j = i+1;j<a.length;j++) {
if (A[J]&LT;MINV) {
MINV = A[j];
MinI = j;
}
}
if (MINV! = a[i] && MinI!=i) {
A[mini] = A[i];
A[i] = MINV;
}
}
}*/

Heap Sort
/*public static void Swap (int[] data, int i, int j) {
if (i = = j) {
Return
}
Data[i] = Data[i] + data[j];
DATA[J] = Data[i]-data[j];
Data[i] = Data[i]-data[j];
}

public static void Heapsort (int[] data) {
for (int i = 0; i < data.length; i++) {
Createmaxdheap (data, data.length-1-i);
Swap (data, 0, data.length-1-i);
print (data);
}
}

public static void Createmaxdheap (int[] data, int lastIndex) {
for (int i = (lastIndex-1)/2; I >= 0; i--) {
Save the node currently being judged
int k = i;
If the child node of the current node exists
while (2 * k + 1 <= lastIndex) {
Biggerindex always records the value of a larger node, first assigning the left child node of the current judgment node
int Biggerindex = 2 * k + 1;
if (Biggerindex < LastIndex) {
If the right child node is present, the biggerindex should be equal to lastIndex at this time
if (Data[biggerindex] < Data[biggerindex + 1]) {
If the right child node value is greater than the left dial hand node value, then the value of the right child node is biggerindex recorded.
biggerindex++;
}
}
if (Data[k] < Data[biggerindex]) {
If the current node value is smaller than the maximum child node value, then the exchange of 2 is the value of the Biggerindex value assigned to K
Swap (data, k, biggerindex);
K = Biggerindex;
} else {
Break
}
}
}
}*/


Bubble sort
/*public static void Bubblesort (int[] a) {
for (int i = 0; i < a.length-1; i++) {
for (int j = i; J < A.length-i-1; J + +) {
if (A[j]>a[j+1]) {
int temp = A[j];
A[J] = a[j+1];
A[J+1] = temp;
}
}
}
}*/

Quick Sort
/*public static void Quiksort (int data[], int start, int end) {

if (end-start <= 0) {
Return
}
int last = start;
for (int i = start + 1; I <= end; i++) {
if (Data[i] < Data[start]) {
int temp = Data[++last];
Data[last] = Data[i];
Data[i] = temp;
}
}
int temp = Data[last];
Data[last] = Data[start];
Data[start] = temp;
Quiksort (data, start, last-1);
Quiksort (data, last + 1, end);
}*/


Merge sort
/*public static void MergeSort (int data[], int start, int end) {
if (Start < end) {
int mid = (start + end)/2;
MergeSort (data, start, mid);
MergeSort (data, mid + 1, end);
Merge (data, start, mid, end);
}
}

public static void merge (int data[], int start, int mid, int end) {
int temp[] = new Int[end-start + 1];
int i = start;
Int J = mid + 1;
int k = 0;
while (I <= mid && J <= End) {
if (Data[i] < data[j]) {
temp[k++] = data[i++];
} else {
temp[k++] = data[j++];
}
}

while (I <= mid) {
temp[k++] = data[i++];
}
while (j <= end) {
temp[k++] = data[j++];
}

for (k = 0, i = start; k < temp.length; k++, i++) {
Data[i] = temp[k];
}
}*/

Base sort
public static void Radixsort (int[] data, int radix, int d) {
Cache Array
int[] tmp = new Int[data.length];
Buckets used to record information about the elements to be sorted
The buckets array defines max-min buckets
int[] Buckets = new Int[radix];

for (int i = 0, rate = 1; i < D; i++) {

Resets the count array to start counting the next keyword
Arrays.fill (buckets, 0);
Copy the elements in data into the TMP array completely
System.arraycopy (data, 0, tmp, 0, data.length);

Calculate sub-keywords for each data to be sorted
for (int j = 0; J < Data.length; J + +) {
int subkey = (Tmp[j]/rate)% radix;
buckets[subkey]++;
}
for (int j = 1; j < Radix; J + +) {
BUCKETS[J] = Buckets[j] + buckets[j-1];
}
Sort the specified data by sub-keywords
for (int m = data.length-1; M >= 0; m--) {
int subkey = (Tmp[m]/rate)% radix;
Data[--buckets[subkey]] = tmp[m];
}
Rate *= Radix;
}

}

}

If you have any mistakes, please ask your friends!

Eight common methods of sorting in Java

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.