JAVA 排序

import java.util.Arrays;

/**
 * Created with Intellij IDEA
 * ClassName:Test
 * User:MaLe
 * Description:
 *
 * @Date:2021/11/11
 * @Time:9:13
 * @author:395645313@qq.com
 */
public class Test {


    //插入排序
    public static void insertSort(int[] arr){
        for (int i = 0; i <arr.length; i++) {
            int tmp=arr[i];
            int j = i-1;
            for (; j >=0; j--) {
                if (tmp<arr[j]){
                    arr[j+1]=arr[j];
                }else{
                    break;
                }
            }
            arr[j+1]=tmp;
        }
    }


    //选择排序
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length ; i++) {
            for (int j = i+1; j <arr.length; j++) {
                if (arr[i]>arr[j]){
                    int tmp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=tmp;
                }
            }
        }
    }


    //冒泡排序
    public static void bubbleSort(int[] arr){
        for (int i = 0; i <arr.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j <arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    int tmp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                    flg=true;
                }
            }
            if (!flg){
              break;
            }
        }
    }


    //快速排序
    public int partiStion(int[] nums,int start,int end){
        int tmp=nums[start];
        while(start<end){
            while(start<end && tmp<=nums[end]){
                end--;
            }
            nums[start]=nums[end];
            while(start<end && tmp>=nums[start]){
                start++;
            }
            nums[end]=nums[start];
        }
        nums[start]=tmp;
        return start;
    }
    public void quckly(int[] nums,int low,int high){
        if(low>=high) return;
        int pivot=partiStion(nums,low,high);
        quckly(nums,low,pivot-1);
        quckly(nums,pivot+1,high);
    }
    public void sortColors(int[] nums) {
        quckly(nums,0,nums.length-1);

    }


  //堆排序
  public static void shiftDown(int[] arr,int parent,int len) {
      int child = (2*parent )+1;
      while(child<len){
          if(child+1<len && arr[child]<arr[child+1]){
              child++;
          }
          if(arr[child]>arr[parent]){
              int tmp=arr[child];
              arr[child]=arr[parent];
              arr[parent]=tmp;
              parent=child;
              child=2*parent+1;
          }else{
              break;
          }
      }
    }
    public static void createHeap(int[] arr){
        for (int i = (arr.length-1-1) ; i >=0 ; i--) {
            shiftDown(arr,i,arr.length);
        }

    }
    public static void heapSort(int[] arr){
        //大根堆
        createHeap(arr);
        int end=arr.length-1;
        while(end>0){
            int tmp=arr[end];
            arr[end]=arr[0];
            arr[0]=tmp;
         shiftDown(arr,0,end);
         end--;
        }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值