时间复杂度为 O(n)的算法:计数排序、基数排序
一,计数排序
package Sort;
import java.util.Arrays;
public class CountingSort {
public int[] countingSort(int[] A, int n) {
// write code here
int i,j,tmp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(A[i]>A[j]) {
tmp=A[i];
A[i]=A[j];
A[j]=tmp; }
return A; }
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] A = { 1, 2, 3, 5, 2, 6};
int n=6;
CountingSort cs = new CountingSort();
cs.countingSort(A, n);
System.out.println(Arrays.toString(A));
}
}