The fast sorting method mainly utilizes a method of arrays to Arrays.sort () implementation.
Bubble method is used to compare the traversal array, through the constant comparison of the minimum or maximum value of a single traversal.
The Select Sort method is to use the first data of the array as the largest or smallest value, and then output the ordered array by comparing the loops.
The insertion sort is the selection of data in an array, which is sorted by constant insertion comparisons.
Copy Code code as follows:
Package com.firewolf.sort;
public class Mysort {
/**
* @param args
*/
public static void Main (string[] args) {
int array[] = {45,32,54,12,43,65,11,3,43,6,33,90,44,1,178};
Mysort mysort = new Mysort ();
Mysort.insertsort (array);
System.out.print ("Insert sort result:");
Mysort.printarray (array);
System.out.println ();
Mysort.bubblesort (array);
System.out.print ("bubble sort Result:");
Mysort.printarray (array);
Mysort.qsort (array);
System.out.println ();
System.out.print ("Quick sort Result:");
Mysort.printarray (array);
Mysort.shellsort (array);
System.out.println ();
System.out.print ("Hill Sort results:");
Mysort.printarray (array);
Mysort.selectsort (array);
System.out.println ();
System.out.print ("Select sort result:");
Mysort.printarray (array);
}
/**
* Direct Insert Sort
* Basic idea: In order to sort of a set of numbers, assuming that the front (n-1) [n>=2] number is already in order, now to put the number of n into the previous ordered number, so that the number of n is also ranked in good order. So repeatedly, until all the order is sorted.
*/
public void Insertsort (int[] array) {
int temp=0;
for (int i=1;i<array.length;i++) {
int j=i-1;
Temp=array[i];
for (; j>=0&&temp<array[j];j--) {
ARRAY[J+1]=ARRAY[J]; Moves the value greater than the temp to one unit after the whole
}
Array[j+1]=temp;
}
}
/**
* Bubble Sort
* Basic idea: In order to sort a group of numbers, the current has not yet lined up in the range of all the number of adjacent two numbers from top to bottom to compare and adjust, so that the larger number to sink, smaller upward. That is, whenever two adjacent numbers are compared and found to be in reverse order, they are interchanged.
*/
public void Bubblesort (int[] array) {
int temp;
for (int i=0;i<array.length;i++) {//number of trips
for (int j=0;j<array.length-i-1;j++) {//compare times
if (Array[j]>array[j+1]) {
TEMP=ARRAY[J];
ARRAY[J]=ARRAY[J+1];
Array[j+1]=temp;
}
}
}
}
/**
* Quick Sort
* Basic idea: Select a datum element, usually select the first element or the last element, through a scan, the backlog sequence is divided into two parts, one is smaller than the datum element, and the other is greater than or equal to the datum element, when the datum element is in the correct position after it is sorted, And then recursively sort the two parts of the partition in the same way.
* @param array
*/
public void qsort (int array[]) {
if (array.length>1) {
_qsort (array,0,array.length-1);
}
}
/**
* A quick sort of trip
* @param array
*/
private void _qsort (int[] array,int Low,int high) {
if (Low < high) {
int middle = getmiddle (array, low, high);
_qsort (array,low,middle-1);
_qsort (array, middle+1, high);
}
}
/**
* Get the middle value
*/
private int Getmiddle (int[] array,int Low,int high) {
int tmp = Array[low];
while (Low < high) {
while (Low < high && Array[high] >= tmp)
high--;
Array[low] = Array[high];
while (Lowlow++;
Array[high] = Array[low];
}
Array[low] = tmp;
return to Low;
}
/**
* Simple Selection sort
* Basic idea: In the number of groups to be sorted, select the smallest number and the number of the first position, and then in the remaining number of the smallest with the second position of the number of exchanges, so as to cycle to the penultimate and the last number of comparisons.
* @param array
*/
public void Selectsort (int[] array) {
int position=0;
for (int i=0;i<array.length;i++) {
int j=i+1;
Position=i;
int temp=array[i];
for (; j<array.length;j++) {
if (array[j]<temp) {
TEMP=ARRAY[J];
Position=j;
}
}
Array[position]=array[i];
Array[i]=temp;
}
}
/**
* Hill Sort (minimum increment sort)
* Basic idea: The algorithm first will be sorted by a group of numbers by an increment D (n/2,n to the number of sorted numbers) into several groups, the subscript of a record in each group D. Inserts a direct sort of all elements in each group, then groups it with a smaller increment (D/2), and then inserts the sort directly in each group. When the increment is reduced to 1 o'clock, the sort completes after a direct insert sort.
* @param array
*/
public void Shellsort (int[] array) {
Double d1=array.length;
int temp=0;
while (true) {
d1= Math.ceil (D1/2);
int d= (int) D1;
for (int x=0;x<d;x++) {
for (int i=x+d;i<array.length;i+=d) {
int j=i-d;
Temp=array[i];
for (; j>=0&&temp<array[j];j-=d) {
ARRAY[J+D]=ARRAY[J];
}
Array[j+d]=temp;
}
}
if (d==1)
Break
}
}
/**
* Print all elements in the array
*/
public void PrintArray (int[] array) {
for (int i = 0; i < Array.Length; i++) {
System.out.print (array[i]+ "");
}
}
}
Here are some examples of how sorting methods are used separately
Using arrays with sorting method to quickly sort
Copy Code code as follows:
Import Java.util.Arrays;
public class test2{
public static void Main (string[] args) {
Int[] a={5,4,2,4,9,1};
Arrays.sort (a); To sort
for (int i:a) {
System.out.print (i);
}
}
}
Bubble Sort algorithm
Copy Code code as follows:
public static int[] Bubblesort (int[] args) {//bubble sort algorithm
for (int i=0;i<args.length-1;i++) {
for (int j=i+1;j<args.length;j++) {
if (Args[i]>args[j]) {
int temp=args[i];
ARGS[I]=ARGS[J];
Args[j]=temp;
}
}
}
return args;
}
Select sorting algorithm
Copy Code code as follows:
public static int[] Selectsort (int[] args) {//select sorting algorithm
for (int i=0;i<args.length-1; i++) {
int min=i;
for (int j=i+1;j<args.length; j + +) {
if (Args[min]>args[j]) {
Min=j;
}
}
if (min!=i) {
int temp=args[i];
Args[i]=args[min];
Args[min]=temp;
}
}
return args;
}
Insert Sort algorithm
Copy Code code as follows:
public static int[] Insertsort (int[] args) {//Insert sort algorithm
for (int i=1;i<args.length;i++) {
for (int j=i;j>0;j--) {
if (Args[j]<args[j-1]) {
int temp=args[j-1];
ARGS[J-1]=ARGS[J];
Args[j]=temp;
}else break;
}
}
return args;
}
These are the four sorting methods in Java. Different methods of efficiency are not the same, the following is the comparison of different algorithms and data exchange when the large O representation.
Bubble sort: compare O (N2) data exchange O (N2)
Select sort: Compare O (N2) data exchange O (N)
Insert Sort: compare O (N2) copy data O (N)
In the practical application, we should try to choose the algorithm with high efficiency.