用java实现快排

来源:互联网 发布:烟台南山学院教务网络 编辑:程序博客网 时间:2024/05/14 09:33
public class Main {    public static void swap(int arr[], int low, int high)    {        int temp;        temp = arr[low];        arr[low] = arr[high];        arr[high] = temp;    }    public static int partition(int [] arr, int low, int high)    {        int point;        point = arr[low];        while(low<high)        {            while(low<high && arr[high]>=point)            {                high--;            }            swap(arr,low,high);            while(low<high && arr[low]<=point)            {                low++;            }            swap(arr, low, high);        }        return low;    }    public static void quicksort(int [] arr, int p, int r)    {        if(p>=r || arr == null)            return ;        int q = partition(arr,p,r);        quicksort(arr, p,q-1);        quicksort(arr,q+1, r);    }    public static void main(String[] args) {        int [] arr = {5,4,6,3,2,0,7,8,1,9};        quicksort(arr,0,arr.length-1);        for(int i=0; i<arr.length;i++)        {            System.out.print(arr[i]+" ");        }    }}

代码截图
这里写图片描述

实验截图
这里写图片描述