java常见排序算法

import lei.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main{
    public static void main(String[] args) {
        int a[]={3,5,8,7,4,2,9,1,13,10};
        int b[]={3,5,8,7,4,2,9,1,13,10};
        int c[]={3,5,8,7,4,2,9,1,13,10};
        int d[]={3,5,8,7,4,2,9,1,13,10};
        int e[]={8,5,7,3};
        int f[]={2,3,1,4,5};
        System.out.println(Arrays.toString(bubbleSort(a)));
        System.out.println(Arrays.toString(selectSort(b)));
        System.out.println(Arrays.toString(insertionSort(c)));
        System.out.println(Arrays.toString(shellSort(d)));
        System.out.println(Arrays.toString(mergeSort(e)));
        quickSort(f,0,f.length-1);
        System.out.println(Arrays.toString(f));
    }
    public static int[] bubbleSort(int[]array){
        int len=array.length;
        int temp;
        for (int i=0;i<len-1;i++){//从第一位比较到倒数第二位
            for (int i2=0;i2<len-i-1;i2++){//依次进行冒泡排序,每次排序之后下次冒泡尾部需要排序到的元素就少一个
                if (array[i2]>array[i2+1]){//冒泡
                    temp=array[i2];
                    array[i2]=array[i2+1];
                    array[i2+1]=temp;
                }
            }
        }
        return array;

    }
    public static int[] selectSort(int [] array){
        int len=array.length;
        for (int i=0;i<len-1;i++){
            int minIndex=i,temp;//默认最小值为i
            for (int i2=i+1;i2<len;i2++){
                if (array[minIndex]>array[i2]){
                    minIndex=i2;
                }
            }
            temp=array[i];
            array[i]=array[minIndex];
            array[minIndex]=temp;

        }
        return array;
    }
    public static int[] insertionSort(int [] array){
        int len=array.length;
        for (int i=1;i<len;i++){
            int j,temp=array[i];
            for (j=i;j>0&&array[j-1]>temp;j--){
                array[j]=array[j-1];
            }
            array[j]=temp;
        }
        return array;
    }
    public static int [] shellSort(int []array){
        for (int gap=array.length/2;gap>0;gap=gap/2){
            for (int i=gap;i<array.length;i++){
                for (int j=i-gap;j>=0;j=j-gap){
                    if (array[j]>array[j+gap]){
                        int temp=array[j];
                        array[j]=array[j+gap];
                        array[j+gap]=temp;
                    }
                }
            }
        }
        return array;
    }
    public static int[] mergeSort(int []array){
        int []temp=new int[array.length];
        mergeSort(array,0,array.length-1,temp);
        return array;
    }
    //传入的参数中left为左边第一个元素,right为右边最后一个元素
    public static void mergeSort(int [] array,int left,int right,int[] temp){
        if (left<right){//
            int mid=(left+right)/2;
            mergeSort(array,left,mid,temp);//对左边数组进行排序递归操作
            mergeSort(array,mid+1,right,temp);//对右边的数组进行排序递归操作
            int leftFirst=left;//左边的第一个元素
            int rightFirst=mid+1;//右边的第一个元素
            int t=0;
            //把较小的数先移到新数组中
            while(leftFirst<=mid&&rightFirst<=right){
                if (array[leftFirst]<array[rightFirst]){
                    temp[t++]=array[leftFirst++];
                }else {
                    temp[t++]=array[rightFirst++];
                }
            }

            //把左边剩余的移入数组
            while (leftFirst<=mid){
                temp[t++] = array[leftFirst++];
            }
            //右边剩余的移入数组
            while(rightFirst<=right){
                temp[t++]=array[rightFirst++];
            }
            t=0;
            //把新数组中的数据覆盖
            while (left<=right){
                array[left++]=temp[t++];
            }

        }
    }
    //[2,3,1,4,5]
    public static void quickSort(int[]array,int start,int end){
        if (start >= end) {
            return;
        }
        int startValue = array[start];//定义基准值
        int startIndex = start;//开始位置下标
        int endIndex = end;//末尾位置下标
        while (startIndex<endIndex){
            //从右边开始找到第一个比他小的值,用endIndex进行记录
            while (startIndex<endIndex&&array[endIndex]>=startValue){
                endIndex--;
            }
            //从左边开始找到第一个比他大的值,用startIndex进行记录
            while (startIndex<endIndex&&array[startIndex]<=startValue){
                startIndex++;
            }
            //交换之前记录过的位置
            //交换值
            int temp=array[endIndex];
            array[endIndex]=array[startIndex];
            array[startIndex]=temp;
        }
        //将初始值设为当前的start和end碰撞的元素
        array[start] = array[startIndex];
        //将两个元素的值进行互换
        array[startIndex] = startValue;
        quickSort(array, start, startIndex-1);//递归调用
        quickSort(array, startIndex+1, end);
    }



}
Last modification:April 19, 2022
如果觉得我的文章对你有用,请随意赞赏