Eight sorting algorithms have not been used for a long time, do a summary today, convenient for later use when reference.
These eight sort algorithms are internal algorithms, the eight sorts of algorithms are:
1. Insert Sort
1) Direct Insert sort
2) Hill Sort
2. Select sort
1) Simple selection sorting
2) Heap Sorting
3. Exchange sorting
1) Bubble sort
2) Quick Sort
4. Merge sort
5. Base Sorting
A direct insert sort inserts a record into an ordered table that is already sorted, resulting in a new, sequential table with a 1 increase in the number of records. In practice, the first record of a sequence is considered an ordered sub-sequence, and then from the second, third 、...... The records are inserted one after the other until the entire sequence is ordered. Insert Sort algorithm Java implementation:
public class Directinsertionsort {
/**
* Direct Insert Sort
* @param args
*/
public static void Main (string[] args) {
Int[] Arrayint = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = Arrayint.length;
for (int i=1; i<length; i++) {
for (int j=i; j>0; j--) {
if (Arrayint[j] <= arrayint[j-1]) {
int k = Arrayint[j];
ARRAYINT[J] = arrayint[j-1];
Arrayint[j-1] = k;
}
}
}
for (int i=0; i<length; i++) {
System.out.println (Arrayint[i]);
}
}
}
Second, hill sort the entire sequence of records to be sorted into a number of sub-sequences to be directly inserted into the order, with the entire series of records "Basic order", then the whole record is inserted in order. Hill sort Java Implementation:
Public class Shellsort {
/**
* Hill sort
* @param args
*/
public static void main (St Ring[] (args) {
int[] Arrayint = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = Arrayint.length;
System.out.println ("length=" +length);
Shellsort (arrayint);
for (int i=0; i<length; i++) {
System.out.println ("arrayint[" + i + "]=" + arrayint[i]);
}
}
public static void Shellsort (int[] arrayint) {
int j = 0;
int temp = 0;
for (int increment=arrayint.length/2; increment>0; increment/=2) {
for (int i=increment; i<arrayint.length; i++) {
temp = Arrayint[i];
for (j=i; j>=increment; j-=increment) {
if (temp > Arrayint[j-increment]) {
ARRAYINT[J] = arrayint[j-increment];
}else{
Break
}
}
ARRAYINT[J] = temp;
}
}
}
}
Simple selection sorting in the set of numbers to be sorted, select the smallest (large) number and the first position of the number exchange, and then in the remaining number to find the smallest (large) and the second position of the number of exchanges, and so on, until the n-1 element and the nth element is compared. Simple Select sort Java implementation:
public class Simpleselectionsort {
/**
* Simple selection of sorting
* @param args
*/
public static void Main (string[] args) {
Int[] Arrayint = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = Arrayint.length;
for (int i=0; i<length; i++) {
for (int j=i+1; j<length; J + +) {
if (Arrayint[i] > Arrayint[j]) {
int k = Arrayint[i];
Arrayint[i] = Arrayint[j];
ARRAYINT[J] = k;
}
}
}
for (int i=0; i<length; i++) {
System.out.println (Arrayint[i]);
}
}
}
Four, heap sorting a sort of tree-shape selection is an effective improvement to the direct selection sort. Heap sort Java implementation: Five, bubble sort treatment of a set number of orders, top-down on the adjacent two number of comparisons and adjustments, so that the larger (small) number to sink, smaller (large) to the top. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged. Bubble sort Java Implementation:
public class Bubblesort {
/**
* Bubble Sort
* @param args
*/
public static void Main (string[] args) {
Int[] Arrayint = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = Arrayint.length;
for (int i=0; i<length-1; i++) {
for (int j=0; j<length-i-1; J + +) {
if (Arrayint[j] > arrayint[j+1]) {
int k = Arrayint[j];
ARRAYINT[J] = arrayint[j+1];
Arrayint[j+1] = k;
}
}
}
for (int i=0; i<length; i++) {
System.out.println (Arrayint[i]);
}
}
}
Six, quick sort by a trip sorting the data to be sorted into separate two parts, one part of all the data is smaller than the other part of all the data, and then according to the method of the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence. Quick Sort Java implementations:
public class QuickSort {
/**
* Quick Sort
* @param args
*/
public static void Main (string[] args) {
Int[] Arrayint = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int start = 0;
int end = Arrayint.length-1;
QuickSort (Arrayint, start, end);
}
public static void QuickSort (int[] arrayint, int start, int end) {
if (start >= end) {
Return
}
int i = start;
int j = END;
int value = Arrayint[start];
Boolean flag = true;
while (i! = j) {
if (flag) {
if (value > Arrayint[j]) {
int k = Arrayint[i];
Arrayint[i] = Arrayint[j];
ARRAYINT[J] = k;
i++;
Flag = false;
}else{
j--;
}
}else{
if (value < Arrayint[i]) {
int k = Arrayint[i];
Arrayint[i] = Arrayint[j];
ARRAYINT[J] = k;
j--;
Flag = true;
}else{
i++;
}
}
}
for (int m=0; m<arrayint.length; m++) {
System.out.print (Arrayint[m] + "");
}
System.out.println ();
QuickSort (Arrayint, start, j-1);
QuickSort (Arrayint, i+1, end);
}
}
The merge sort is an effective sorting algorithm based on the merging operation, which is a typical application of "divide and conquer method". Merge sort Java implementation:
public class MergeSort {
/**
* Merge Sort
* @param args
*/
public static void Main (string[] args) {
Int[] Arrayint = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int start = 0;
int end = Arrayint.length-1;
MergeSort (Arrayint, start, end);
for (int i=0; i<arrayint.length; i++) {
System.out.print (arrayint[i]+ "");
}
}
public static int[] MergeSort (int[] arrayint, int start, int end) {
int middle = (start+end)/2;
if (Start < end) {
MergeSort (Arrayint, start, middle);
MergeSort (Arrayint, middle+1, end);
Sort (arrayint, start, middle, end);
}
return arrayint;
}
public static void Sort (int[] arrayint, int start, int middle, int end) {
int[] arraytemp = new int[end-start+1];
int i = start;
Int j = Middle +1;
int k = 0;
while (I <= Middle && J <= End) {
if (Arrayint[i] < arrayint[j]) {
ARRAYTEMP[K] = Arrayint[i];
k++;
i++;
}else{
ARRAYTEMP[K] = Arrayint[j];
k++;
j + +;
}
}
while (I<=middle) {
ARRAYTEMP[K] = Arrayint[i];
k++;
i++;
}
while (j <= end) {
ARRAYTEMP[K] = Arrayint[j];
k++;
j + +;
}
for (int m=0; m<arraytemp.length; m++) {
Arrayint[m+start] = arraytemp[m];
}
}
}
The base sort belongs to "distributive sort", also known as "bucket Method". It is a part of the information through the key value, the elements to be sorted into some "barrels", in order to achieve the role of sorting. The Cardinal sort method is a sort of stability. Base sort Java Sort:
Eight sorting algorithms (internal sort)