Principle and implementation of sorting algorithm

Source: Internet
Author: User

Algorithm one: Direct Insert sort

Algorithm implementation principle: is to calculate where a new element should be placed? Each time it comes in, one will be re-assembled in the original order.

Code implementation: Java

public int[] testinsertionsort (int[] data) {
//this Methord are very easy.
for ( int i = 1;i < Data. Length;i++) {
int temp = data[i];
int J =i;
while (j>0 && data[j-1]>temp) {
data[j] = Data[j-1];
j--;
}
data[j] = temp;
}
return data;

}

Algorithm Two: Hill Sort

The principle of the algorithm: it is to use a different length of the order, which is only in the direct insertion of the sort based on a number of Improvements.

Code implementation: Java

Public int[] Testinsertionshell (int[] Data) {
int length = Data. length;
for (int d = length/2;d > 0;d/=2) {
for (int i = d; I < Length;i++) {
int temp = data[i];
Int J = i;
while (j >= d && data[j-d] > Temp) {
data[j] = data[j-d];
j-= d;
}
data[j] = temp;
}
}
return data;
}

Algorithm Three: Select sort
The algorithm principle: each time sorts, finds the minimum value to put in the starting position, sequentially.
Code implementation: Java
Public int[] Testselectsort (int[] Data) {
int length = Data.Length
for (int i = 0;i < length-1; I++) {
Int J = i;
for (int k = i+1;k<length;k++) {
if (data[k]<data[j]) {
j = k;
}

}
if (j! = i) {
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}

}

return data;
}

Algorithm Four: Improved selection sorting
Algorithm implementation principle: the maximum and minimum values are found during the sorting process.
Code implementation: Java
Public int[] Testmodifiedselectsort (int[] Data) {
int length = Data.Length
Forint i =0; I < length/2;i++) {
int maxindex = length-i-1;
int minindex = i;
Forint k= i+1; K < Length;k++) {
If (data[k]<data[minindex]) {
Minindex = k;
}
}
If (minindex!=i) {
int temp = data[minindex];
data[minindex] = data[i];
data[i] = temp;
}
for (int m = length-i-2;m>i;m--) {
if (data[m]>data[maxindex]) {
Maxindex = m;
}
}
if (maxindex!=length-i-1) {
int temp = data[maxindex];
data[maxindex] = data[length-1-i];
data[length-1-i] = temp;
}

}
return data;
}

Algorithm five: Bubble sort
Algorithm implementation principle: each time the order, you can get a maximum Value.
Code implementation:
Public int[] Testbubblesort (int[] Data) {
int length = Data.Length
for (int i = 0; i < length-1;i++) {
for (int j = 0;j < length-i-1;j++) {
if (data[j] > data[j+1]) {
int temp = data[j+1];
data[j+1] = data[j];
data[j] = temp;
}
}
}
return data;

}

Algorithm Six: Quick Sort
Algorithm principle: use the sorting method in Split.
Code implementation:
Public int[] Testquicksort (int[] data,int left,int Right) {

If (left < Right) {
int i = left;
Int J = right;
int x = data[i];
while (i < J) {
while (i<j && data[j] > X) {
j--;
}
if (i < j) {
data[i++] = data[j];
}
while (i < J && Data[i] < x) {
i++;
}
if (i < j) {
data[j--] = data[i];
}
}
data[i] = x;
Testquicksort (data,left,i-1);
Testquicksort (data,i+1,right);

}
return data;


}

Algorithm Seven: heap Sequencing
The algorithm principle: is constructs the heap, unceasingly will arrange the order root node and the leaf node to exchange, carries on the constant adjustment heap.
Code implementation:
public void Buildheapsort (int[] data,int root,int Length) {
int lchild = root *2 +1;
If (lchild < Length) {
int largest = lchild;
int rchild = Lchild +1;
If (rchild < Length) {
If (data[lchild] < data[rchild]) {
largest = rchild;
}
If (data[root] < data[largest]) {
int temp = data[largest];
data[largest] = data[root];
data[root] = temp;
Buildheapsort (data, largest, length);
}
}
}

}

Public int[] Heapsort (int[] Data) {
int length = Data.length;
for (int root = Length/2-1; root >= 0; root--) {
buildheapsort (data, root, length);
}
for (1; i >= 2; i--) {
int temp = Data[0";
Data[0] = data[i ];
data[i] = temp;
Buildheapsort (data, 0,--length);
}
return data;
}



Principle and implementation of sorting algorithm

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.