Bubble sort / select Sort

Bubble sort is the time complexity of the algorithm is O (N ^ 2), the space complexity is O (1), the sorting algorithm is stable. Refers to the so-called stability, the table is equal to the original order of the elements in the array

It will not change after sorting.

Bubble sort algorithm after each round is the largest element shift, after the first round of sorting, the last one is the largest, after the end of the second round, the penultimate element is the largest, and so on, At last

It will be a number of columns to sort before.

public static int[] Bubble(int[] a){

if(a==null||a.length==0)
 return a;
//sort
int n=a.length-1;
for(int i=n;i>0;i--){
for(int j=0;j<i;j++){
if(a[j]>a[j+1])
     swap(a,j,j+1);//一般的交换函数
}

}
return a; }

Selection Sort

Select a different sort and bubble sort, which each round will select the smallest element. Time complexity and space complexity and bubble sort is the same

public  static  int [] selctSort ( int A) { 

IF (A == null | a.length == 0 )
 return A; 

for ( int I = 0; I <-a.length. 1; I ++ ) {
   int MINI = I ; // assumed that the current minimum 
 for ( int J = I +. 1; J <a.length; J ++ ) {
   MINI a = [J]> a [MINI] MINI:? J; // find the minimum index 
} 
the swap (A, MINI, I); 

} 
return A; 
}

 

Guess you like

Origin www.cnblogs.com/bowenqianngzhibushiwo/p/11619743.html