排序算法JAVA实现三

来源:互联网 发布:俄罗斯生活知乎 编辑:程序博客网 时间:2024/06/05 22:57

时间复杂度为 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));    }}
0 0