排序算法

import java.util.Arrays;

/**

  • @Author: Xia

  • @Date: 2021/2/22 8:48

  • @Email:x2358114512@163.com

  • /
    public class SortEight{
    public static void main(String[] Args){

      int[] nums = new int[]{1,2,2,2,12,2,14,5,6,7,8,190};
      bubbleSort(nums);
      quickSort(0, nums.length-1, nums);
      insertSort(nums);
      shellSort(nums);
      selectSort(nums);
      heapSort(nums);
      megerSort(0, nums.length-1, nums);
      radixSort(nums);
      System.out.println(Arrays.toString(nums));

    }

    private static void radixSort(int[] nums) {

      int[][] bucket = new int[10][nums.length];  //10个桶,每个桶长度为nums.length
      int[] tem = new  int[10];    //下标对应的桶,元素对应哪个桶装的个数
      int maxNum = max(nums);
      int len = (maxNum+"").length();
      for(int i=0, d=1; i<len; i++,d*=10){
          for(int j=0; j<nums.length; j++){
              int yushu = nums[j]/d%10;
              bucket[yushu][tem[yushu]] = nums[j]; //nums[j]放到桶中【拿一个桶,该桶里有几个元素】
              tem[yushu]++;    //
          }
          int idx = 0;
          for(int k=0; k<tem.length; k++){
              if(tem[k]!=0){
                  for(int m = 0; m<tem[k]; m++){
                      nums[idx] = bucket[k][m];
                      idx++;
                  }
              }
              tem[k] = 0;  //清空,下次存放到桶中数据
          }
      }

    }

    private static int max(int[] nums) {

      int maxNum = Integer.MIN_VALUE;
      for(int i=0; i<nums.length; i++){
          if(nums[i]>maxNum){
              maxNum = nums[i];
          }
      }
      return maxNum;

    }

    private static void heapSort(int[] nums) {

      int start = (nums.length-1)/2;  //从第一个非叶子结点从下至上调整为大顶堆
      for(int i=start; i>=0; i--){
          toHeap(i, nums.length, nums);
      }
      for(int i=nums.length-1; i>0; i--){
          swap(0, i, nums);
          toHeap(0, i, nums);
      }

    }

    private static void toHeap(int idx, int length, int[] nums) {

      int leftIdx = 2*idx+1; int rightIdx = 2*idx+2;
      int maxIdx = idx;
      if(leftIdx<length && nums[maxIdx]<nums[leftIdx]){
          maxIdx = leftIdx;
      }
      if(rightIdx<length && nums[maxIdx]<nums[rightIdx]){
          maxIdx = rightIdx;
      }
      if(idx!=maxIdx){
          swap(idx, maxIdx, nums);
          toHeap(maxIdx, length, nums);
      }

    }

    private static void megerSort(int start, int end, int[] nums) {

      if(start<end){
          int middle = start+(end-start)/2;
          megerSort(start,middle, nums);
          megerSort(middle+1,end, nums);
          meger(start,middle,end,nums);
      }

    }

    private static void meger(int start, int middle, int end, int[] nums) {

      int[] tem = new int[end-start+1];  int idx = 0;
      int left = start; int right = middle+1;
      while(left<=middle && right<=end){
          if(nums[left]>nums[right]){
              tem[idx] = nums[right];
              right++;
          }else{
              tem[idx] = nums[left];
              left++;
          }
          idx++;
      }
      while(left<=middle){
          tem[idx] = nums[left];
          left++;idx++;
      }
      while(right<=end){
          tem[idx] = nums[right];
          right++;idx++;
      }
      for(int i=0; i<tem.length; i++){
          nums[i+start] = tem[i];
      }

    }

public static void bubbleSort(int[] nums){
    int len = nums.length;
    for(int i=0; i<len-1; i++){
        for(int j=0; j<len-1-i; j++){
            if(nums[j]>nums[j+1]){
                swap(j, j+1, nums);
            }
        }
    }
}

public static void swap(int i, int j, int[] nums){
    int tem = nums[i];
    nums[i] = nums[j];
    nums[j] = tem;
}

public static void quickSort(int start, int end, int[]nums){
    if(start<end){
        int tem = nums[start];
        int l = start; int r = end;
        while(l<r){
            while(l<r && tem<=nums[r]){
                r--;
            }nums[l] = nums[r];
            while(l<r && tem>=nums[l]){
                l++;
            }nums[r] = nums[l];
        }
        nums[l] = tem;
        quickSort(start, l-1, nums);
        quickSort(l+1, end, nums);
    }
}

public static void insertSort(int[] nums){
    int len = nums.length;
    for(int i=1; i<len; i++){
        if(nums[i-1]>nums[i]) {
            int tem = nums[i];
            int j;
            for (j = i - 1; j >= 0 && nums[j] >  tem; j--) {
                nums[j + 1] = nums[j];
            }
            nums[j+1] =  tem;
        }
    }
}

public static void shellSort(int[] nums){
    int len = nums.length;
    for(int d=len/2; d>0; d/=2){
        for(int i=d; i<len; i++){
            for(int j=0; j<len-d; j+=d){
                if(nums[j]>nums[j+d]){
                    swap(j, j+d, nums);
                }
            }
        }
    }
}

public static void selectSort(int[] nums){
    int len = nums.length;
    for(int i=0; i<len; i++){
        int minIdx = i;
        for(int j=i; j<len; j++){
            if(nums[j]<nums[minIdx]){
                minIdx = j;
            }
        }
        if(minIdx!=i){
            swap(i, minIdx, nums);
        }
    }

}

}

全部评论

相关推荐

来个大佬救一下,为上投了都是石沉大海了,没实习经历的话怕秋招直接进不了面。什么实习这么难找,基本
心态爆炸了:现在正式的岗位都少,实习基本不咋招的,除了大厂,中小企业其实没那么多岗位需求,就算是有,大多都是招一两个廉价劳动力,同时,他们也会希望你一来就能干活的,没时间培训你,就让你了解公司的项目,你了解完就可以开始干活。再者是,很多低质量的实习其实用处没有那么大的。我去年也是找实习找到破防,最后去了一家深圳的小公司实习,工作对我来说很简单,甚至不如我在学校做的项目,秋招的时候,这段实习经历也并没有帮上什么忙,投递简历,依旧非常低的回复率。低回复率是常态,尤其是找实习,找不到,那就把重心放在优化自己的简历和项目,多看八股文,锻炼自己的面试能力,多看别人的面经,自己模拟面试,等秋招的时候,只要有那么寥寥几次,好好抓住那几次机会。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 13:54
点赞 评论 收藏
分享
06-14 19:09
门头沟学院 Java
darius_:给制造业搞的,什么物料管理生产管理,设备管理点检,最最关键的就是一堆报表看板。个人觉得没啥技术含量都是些基本的crud,但是业务很繁琐那种
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 11:45
你不要过来啊啊啊啊啊啊啊
码农索隆:对面:“今天你不面也得面”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务