In all kinds of algorithms, the sorting algorithm is the most basic content. Here we mainly share a bubble sort, choose sort, insert sort, hill sort, quick sort and heap sort, and merge sort.
1. Bubble sort
Here is the most basic, do not say more.
public static void Bubblesort (int[] a) {int temp;for (int. i=1;i<a.length;i++) {for (int j=0;j<a.length-i;j++) {if (a[ J]>a[j+1]) {temp=a[j];a[j]=a[j+1];a[j+1]=temp;}} System.out.print ("+i+" The result is: "); for (int k=0;k<a.length;k++) {System.out.print (" \ T "+a[k]);} System.out.print ("\ n");}}
2. Select sort
Sorting is done mainly by choice and exchange, and looks like bubbles.
public static void Selectsort (int[] a) {int index,temp;for (int i=0;i<a.length-1;i++) {index=i;for (int j=i+1;j< a.length;j++) {if (A[j]>a[index]) {index=j;}} if (index!=i) {temp=a[i];a[i]=a[index];a[index]=temp;} System.out.print ("+i+" The result is: "); for (int k=0;k<a.length;k++) {System.out.print (" \ T "+a[k]);} System.out.print ("\ n");}}
3. Insert Sort
Sorting is done primarily by inserting unsorted data into the appropriate location.
static void Insertionsort (int[] a) {int i,j,t,h;for (i=1;i<a.length;i++) {t=a[i];j=i-1;while (j>=0 && t <a[j]) {a[j+1]=a[j];j--;} a[j+1]=t; System.out.print ("+i+" The result is: "); for (int k=0;k<a.length;k++) {System.out.print (" \ T "+a[k]);} System.out.print ("\ n");}}
4. Hill sort
Based on the idea of insertion sorting, the main is to divide n elements into N/2 sequence of numbers, the 1th data and the n/2+1 data is a pair, a cycle of each sequence to the order, and then into the N/4 sequence, again sorted, except when the rounding.
static void Shellsort (int[] a) {int i,j,h;int r,temp;int x=0;for (r=a.length/2;r>=1;r/=2) {for (i=r;i<a.length;i++ {temp=a[i];j=i-r;while (j>=0 && temp<a[j]) {a[j+r]=a[j];j-=r;} A[j+r]=temp;} x + +; System.out.print ("+x+" The result of the order is: "); for (h=0;h<a.length;h++) {System.out.print (" \ T "+a[h]);} System.out.print ("\ n");}}
5. Quick Sort
Quick sort by multiple comparisons and exchanges to achieve the sorting, first set a cutoff value, through the data of the cutoff value is divided into the left and right parts, the data is greater than or equal to the cutoff value of the array, less than the array to the left. Then the left and right sides of the data can be sorted independently. Repeat the above procedure.
static void QuickSort (int[] Arr,int left,int right) {int f,t;int rtemp,ltemp;ltemp=left;rtemp=right;f=arr[(left+right) /2];while (ltemp<rtemp) {while (arr[ltemp]<f) {++ltemp;} while (arr[rtemp]>f) {--rtemp;} if (ltemp<=rtemp) {t=arr[ltemp];arr[ltemp]=arr[rtemp];arr[rtemp]=t;--rtemp;++ltemp;}} if (ltemp==rtemp) {ltemp++;} if (left<rtemp) {quickSort (arr,left,ltemp-1);} if (ltemp<right) {quickSort (arr,rtemp+1,right);}}
6. Heap Sequencing
This is a little bit more complicated.
A complete heap ordering requires a repeated two-step process: Constructing the heap structure and sorting the heap output.
First, the original unordered data is placed in each node of a complete binary tree, and then the data of the parent-child node is compared to the upper layer of the complete binary tree.
Data that causes the parent node's data to be larger than the child nodes. It is necessary to use sieve operations to adjust the node data until all nodes finally meet the conditions of the heap structure.
static void Heapsort (int a[],int n) {//heap sort int i,j,h,k;int t;for (i=n/2-1;i>=0;i--) {//a[0,n-1] built large heap while I+1<n) {//I nodes have right subtree j=2*i+1;if ((j+1) <n) {if (a[j]<a[j+1])//Right left subtree is less than right subtree, then you need to compare right subtree J + +; Add 1 to the number, point to right subtree}if (A[i]<a[j]) {//Compare I and J for serial numbers t=a[i]; Exchange Data a[i]=a[j];a[j]=t; The heap is destroyed and needs to be re-adjusted i=j;} else{//Comparison the left and right subtree are large, the heap is not destroyed, no longer need to adjust the break;}} System.out.println ("Original composed heap:"), for (h=0;h<n;h++) {System.out.print ("\ T" +a[h]);} System.out.print ("\ n"); for (i=n-1;i>0;i--) {t=a[0]; With the first record Exchange A[0]=a[i];a[i]=t;k=0;while (2*k+1<i) {///I nodes have right subtree j=2*k+1;if ((j+1) <i) {if (a[j]<a[j+1]) {// Right left subtree is smaller than right subtree, you need to compare right subtree J + +; Add 1 to the number, point to right subtree}}if (A[k]<a[j]) {//Compare the data of I and J serial number t=a[k]; Exchange Data A[k]=a[j]; A[j]=t;k=j; Heap is destroyed, need to readjust}else{break;}} System.out.println ("n-i" + "Step Order Result:"); for (h=0;h<n;h++) {System.out.print ("\ T" +a[h]);} System.out.print ("\ n");}} /** * Parameter A is a two-tree array that is stored in a linear manner, and the input parameter n is the length of the array. * * @param args * *
7. Merge sort
A basic idea for sorting the original data sorted by sorting: First, the sequence of data containing n nodes is considered to consist of an ordered sub-table of n length 1.
* Combine it sequentially by 22 to get a number of ordered sub-tables with a length of 2;
static void Mergeone (int a[],int b[],int n, int len) {//complete once merged function int i,j,k,s,e;s=0;while (s+len<n) {e=s+2*len-1;if (E&G T;=n) {//Last paragraph may be less than Len node e=n-1;} Adjacent ordered segments Merge K=s;i=s;j=s+len;while (I<s+len && j<=e) {//If the two ordered tables are not closed, the loop compares if (A[i]<=a[j]) {//If the smaller elements are copied to the number Group B in b[k++]=a[i++];} Else{b[k++]=a[j++];}} while (I<s+len) {//non-merged parts are copied into array B b[k++]=a[i++];} while (j<=e) {b[k++]=a[j++]; The unincorporated portions are copied into array b}s=e+1; The beginning of the left segment in the next pair of ordered segments}if (s<n) {////To copy the remaining ordered segment from array A to array b for (; s<n;s++) {b[s]=a[s];}} static void mergesort (int a[],int n) {//merge sort int h,count,len,f;count=0; Sort step len=1; Sequential sequence length f=0; Variable f do flag int[] p=new int[n];while (len<n) {if (f==1) {///alternating between A and P Mergeone (P,a,n,len); P merged into A}else{mergeone (A,p,n,len); A merger to P}len=len*2; Increase sequential sequence length f=1-f; The F value is switched between 0 and 1 count++; System.out.printf ("+count+" sort result: "); for (h=0;h<size;h++) {System.out.print (" \ T "+a[h]);} System.out.print ("\ n");} if (f==1) { If sorting is done for (h=0;h<n;h++) {///To copy data from memory p back to array aa[h]=p[h];}}} /** * in the Mergeone () method, the input parameter A is an array that holds the data to be sorted, the input parameter B is an array to hold the merged data *, the parameter n represents the total number of elements in array A that need to be sorted, and the parameter Len represents the length of each ordered child table. * * Two-way combined sorting algorithm requires a larger secondary memory space, which is as large as the original sequence to be sorted. * * * @param args * *
In the centralized sorting algorithm that is shared here, the bubbling sort, the insert sort, and the merge sort are all stable sorting algorithms (that is, maintaining the relative order of records to sort).
No sort algorithm is absolutely good or bad, different ranking algorithms have their pros and cons, in the actual application of traditional Chinese medicine according to the actual situation to choose the sorting algorithm. If the amount of data is small, you can take the insertion sort or choose the sort, when the amount of data n is large, you should use the time complexity O (nlogn) sorting method, such as fast sorting, heap sorting or merge sort, use space to change time. If the original sorted data is randomly distributed, it is sorted quickly.
Source: Click Open Link http://download.csdn.net/detail/sdksdk0/9527723
Java Common sorting algorithm