常见排序算法记录

冒泡排序

public class bubbleSort {
    public static void main(String[] args) {
        int []dp={9,8,7,6,5,4,3,2,1,0};
        bubbleSort(dp);
        for (int x:dp) {
            System.out.println(x);
        }
    }
    static void bubbleSort(int[] arr)
    {
        int n=arr.length;
        //外层i表示有n-1轮,即每轮确定一个数的最终位置,确认n-1个位置即确定n个数的位置
        for(int i=0;i<n-1;i++)
        {
            //每轮执行一次遍历,每轮得出一个最大值,即确定一个数的最终位置
            for (int j =0 ; j <n-i-1 ; j++) {
                if(arr[j]>arr[j+1])
                {
                    //j和j+1位置元素交换
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
    }
}

选择排序

public class selectSort {
    public static void main(String[] args) {
        int []dp={9,8,7,6,5,4,3,2,1,0};
        selectSort(dp);
        for (int x:dp) {
            System.out.println(x);
        }
    }
    static void  selectSort(int []arr)
    {
        int n=arr.length;
        //外层i表示当前确定的是数组的第i个数
        for (int i = 0; i < n; i++) {
            //预定设置k为i
            int k=i;
            for (int j = i+1; j <n ; j++) {
                //如果发现后边有比k预设值还要小的,就更换
                if(arr[k]>arr[j])
                {
                    k=j;
                }
            }
            //一轮循环选出[i+1,n]中最小的,放到i位置,如果k和i一样,则没有变化,不用交换
            if(i!=k)swap(arr, i, k);
        }
    }
    public static void swap(int[] A, int i, int j) {
        if (i != j) {
            A[i] ^= A[j];
            A[j] ^= A[i];
            A[i] ^= A[j];
        }
    }
}

归并排序

import java.util.Arrays;
import java.util.List;

public class mergeSort {
    public static void main(String[] args) {
        int []dp={9,8,7,6,5,4,3,2,1,0};
        mergeSort(0,9,dp);
        for (int x:dp) {
            System.out.println(x);
        }
    }
    public static void mergeSort(int left,int right,int []ars){
        //当划分到长度为1,即一个数时进行合并
        if(left<right)
        {
            int mid=left+(right-left)>>1;
            //一直分半分半
            mergeSort(left, mid, ars);
            mergeSort(mid+1, right, ars);
            //上面函数递归出来后进行合并
            merge(left, right, mid, ars);
        }
    }
    static void merge(int left,int right,int mid,int []a)
    {
        int length=right-left+1;
        int []temp=new int[length];
        int tmpIndex=0;
        int l=left,r=mid+1;
        //两半数组进行比较,合成一个temp数组
        while(l <= mid && r <= right)
        {
            if(a[l]<=a[r])
            {
                temp[tmpIndex++]=a[l++];
            }else {
                temp[tmpIndex++]=a[r++];
            }
        }
        while (l<=mid)temp[tmpIndex++]=a[l++];
        while (r<=right)temp[tmpIndex++]=a[r++];
        tmpIndex=0;
        //再把temp数组赋回原来的数组
        while (tmpIndex<length)
        {
            //原数组下表应该从left开始,因为当前是left-right区间
            a[left+tmpIndex]=temp[tmpIndex];
            tmpIndex++;
        }
    }

}

快速排序

import java.util.HashMap;
import java.util.Map;

public class quickSort {
    public static void main(String[] args) {
        int []dp={9,8,7,6,5,4,3,2,1,0};
//        quickSort(dp,0,9);
        quickSort2(0,9,dp);
        for (int x:dp) {
            System.out.println(x);
        }
    }
    private static void quickSort(int[] array, int start,int end) {
        if (start <end) {
            int key = array[start];//用待排数组的第一个作为基准值
            int i = start;//i所处位置要不然和j一样,要不然就是比key大的位置(始终i<=j)
            for (int j = start + 1; j <= end; j++) {
                //当有比基准值小的数,放到前面
                if (key > array[j]) {
                    swap(array, j, ++i);
                }
            }
            //最后i的位置即为基准值的位置,左边都比key小,右边都比key大
            array[start] = array[i];//先挪,然后再把中枢放到指定位置
            array[i] = key;
            quickSort(array, start, i - 1);
            quickSort(array, i + 1, end);
        }
    }
    public static void swap(int[] A, int i, int j) {
        if (i != j) {
            A[i] ^= A[j];
            A[j] ^= A[i];
            A[i] ^= A[j];
        }
    }
     static void quickSort2(int left,int right,int arr[])
     {
         //如果l>=j递归结束
         if(left>=right)return;
         int i=left,j=right;
         int temp=arr[left];//基准值一般以左边界
         //出循环的条件是i=j
         while(i!=j)
         {
             //先右往左找比基准值小的
             while(arr[j]>=temp&&i<j)j--;
             //将比基准值小的放到前面
             if(i<j)arr[i++]=arr[j];
             //再左往右找比基准值大的
             while(arr[i]<=temp&&i<j)i++;
             //将比基准值大的放到后面
             if(i<j)arr[j--]=arr[i];
         }
         //循环结束i==j
         //再把基准值赋值到下标为i的位置
         arr[i]=temp;
         //递归两边
         quickSort2(left, i-1, arr);
         quickSort2(i+1, right, arr);
     }

}

堆排序(大根)

public class headSort {
    public static void main(String[] args) {
        int []a={3,6,8,5,7};
        heapSort(a);
        for (int x:a) {
            System.out.print(x+" ");
        }
    }
    //堆排序
    public static void heapSort(int[] arr) {
        //构造大根堆
        heapInsert(arr);
        int size = arr.length;
        while (size > 1) {
            //固定最大值
            swap(arr, 0, size - 1);
            size--;
            //构造大根堆
            heapify(arr, 0, size);
        }
    }

    //构造大根堆(通过新插入的数上升)
    public static void heapInsert(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            //当前插入的索引
            int currentIndex = i;
            //父结点索引
            int fatherIndex = (currentIndex - 1) / 2;
            //如果当前插入的值大于其父结点的值,则交换值,并且将索引指向父结点
            //然后继续和上面的父结点值比较,直到不大于父结点,则退出循环
            while (arr[currentIndex] > arr[fatherIndex]) {
                //交换当前结点与父结点的值
                swap(arr, currentIndex, fatherIndex);
                //将当前索引指向父索引
                currentIndex = fatherIndex;
                //重新计算当前索引的父索引
                //当达到堆顶 -1/2=0
                fatherIndex = (currentIndex - 1) / 2;
            }
        }
    }
    //将剩余的数构造成大根堆(通过顶端的数下降)
    public static void heapify(int[] arr, int index, int size) {
        int left = 2 * index + 1;
        int right = 2 * index + 2;
        while (left < size) {
            int largestIndex;
            //判断孩子中较大的值的索引(要确保右孩子在size范围之内)
            if (arr[left] < arr[right] && right < size) {
                largestIndex = right;
            } else {
                largestIndex = left;
            }
            //比较父结点的值与孩子中较大的值,并确定最大值的索引
            if (arr[index] > arr[largestIndex]) {
                largestIndex = index;
            }
            //如果父结点索引是最大值的索引,那已经是大根堆了,则退出循环
            if (index == largestIndex) {
                break;
            }
            //父结点不是最大值,与孩子中较大的值交换
            swap(arr, largestIndex, index);
            //将索引指向孩子中较大的值的索引
            index = largestIndex;
            //重新计算交换之后的孩子的索引
            left = 2 * index + 1;
            right = 2 * index + 2;
        }

    }
    //交换数组中两个元素的值
    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值