桶排序及数组实现栈和队列

     桶排序是将数组分到有限数量的桶子里,每个桶子在个别排序,桶排序时间复杂度为O(n),空间复杂度O(n),不是基于比较的排序。

    代码示例数值为0-200value之间的数值排序

       public static void bucketSocket(int[] arr){

          if(arr==null||arr.length<2){

             return; 

         }

         int max=Integer.MIN_VALUE;

         for(int i=0;i<arr.length;i++){

             max=Math.max(max,arr[i]);}

         int[] bucket=new int[max+1];

          for(int i=0;i<arr.length;i++){

             bucket[arr[i]]++;  }

          int i=0;

          for(int j=0;j<bucket.length;j++){

               while(bucket[j]-->0){

                  arr[i++]=j;   }

              }}

 数组实现栈及数组实现队列代码实现:

    publicclass Code_Array_To_Stack_queue {
      public static class ArrayStack {
           private Integer[] arr;
           private Integer size;


           public ArrayStack(int initSize){
                 if (initSize < 0) {
                      throw newIllegalArgumentException("The init size is less than 0");


                 }
                 arr = new Integer[initSize];
                 size = 0;
           }


           public Integer peek() {
                 if (size == 0) {
                      return null;
                 }
                 return arr[size - 1];
           }


           public void push(int obj) {
                 if (size == arr.length) {
                      throw newArrayIndexOutOfBoundsException("The queue is full");


                 }
                 arr[size++] = obj;
           }

           public Integer pop() {
                 if (size == 0) {
                      throw newArrayIndexOutOfBoundsException("The queue is empty");
                 }
                 return arr[--size];
           }
      }

      public static class ArrayQueue {
           private Integer[] arr;
           private Integer size;
           private Integer first;
           private Integer last;

           public ArrayQueue(int initSize){
                 if (initSize < 0) {
                      throw newIllegalArgumentException("The init size is less than 0");
                 }
                 arr = newInteger[initSize];
                 size = 0;
                 first = 0;
                 last = 0;
           }

           public Integer peek() {
                 if (size == 0) {
                      return null;
                 }
                 return arr[first];
           }

           public void push(int obj) {
                 if (size == arr.length) {
                      throw newArrayIndexOutOfBoundsException("The queue is full");
                 }
                 size++;
                 arr[last] = obj;
                 last = last == arr.length- 1 ? 0 : last + 1;
           }

           public Integer poll() {
                 if (size == 0) {
                      throw newArrayIndexOutOfBoundsException("The queue is empty");
                 }
                 size--;
                 int temp = first;
                 first = first ==arr.length - 1 ? 0 : first + 1;
                 return arr[temp];
           }
           public static void main(String[] args) {

           }
      }
}

       系统排序:Arrays.sort(arr)综合排序中当元素较少时用插入排序,小Int、小char、小double等用quick,反之用merge稳定性好。    

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值