in Java, when using array to sort the function, there are four kinds of methods: fast sorting method, bubbling method, choosing Sort method, inserting sort method.
The fast sorting method mainly uses a method arrays.sort () implementation in arrays.
The bubbling method uses a traversal array to compare the minimum or maximum values by a continuous comparison.
The selection sort method is to use the first data of the array as the maximum or minimum value, and then to output an ordered array by comparing loops.
The insertion sort is the selection of data in an array, which is then sorted by the constant insertion comparison. Below I will explain their implementation method one by one for your reference.
<1> quick sorting using arrays with sorting methods
1 ImportJava.util.Arrays;
2 Public classtest2{
3 Public Static voidMain (string[] args) {
4 int[] a={5,4,2,4,9,1};
5Arrays.sort (a);//to sort
6 for(intI:A) {
7System.out.print (i);
8}
9}
Ten}
<2> Bubble Sorting algorithm
1 Public Static int[] Bubblesort (int[] args) {//Bubble Sort Algorithm
2 for(inti=0;i<args.length-1;i++) {
3 for(intj=i+1;j<args.length;j++) {
4 if(Args[i]>args[j]) {
5 intTemp=args[i];
6ARGS[I]=ARGS[J];
7Args[j]=temp;
8}
9}
Ten}
One returnArgs
A}
<3> Selection Sorting algorithm
1 Public Static int[] Selectsort (int[] args) {//Select sorting Algorithm
2 for(inti=0;i<args.length-1; i++) {
3 intMin=i;
4 for(intJ=i+1;j<args.length; j + +) {
5 if(Args[min]>args[j]) {
6Min=j;
7}
8}
9 if(min!=i) {
Ten intTemp=args[i];
OneArgs[i]=args[min];
AArgs[min]=temp;
-}
-}
the returnArgs
-}
<4> Insert Sort algorithm
1 Public Static int[] Insertsort (int[] args) {//Insert Sort Algorithm
2 for(inti=1;i<args.length;i++) {
3 for(intj=i;j>0;j--) {
4 if(Args[j]<args[j-1]) {
5 intTEMP=ARGS[J-1];
6ARGS[J-1]=ARGS[J];
7Args[j]=temp;
8}Else Break;
9}
Ten}
One returnArgs
A}
These are the four sorting methods in Java. Different methods of efficiency are different, the following is the comparison of the algorithms and the large o representation of the data exchange.
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 practical application, we should try to choose the algorithm with high efficiency.
=========================================================
Reprinted from: http://www.cnblogs.com/zxl-jay/archive/2011/09/24/2189711.html
=========================================================
Four kinds of sorting methods in Java using arrays