递归需要用到的快速排列

快速排列是一个简便的排序问题,在一个数组问题中可以快速排列,也就是制定一个x,就可以把数组中小于x的数放在x前面,大于x的数放在x后。

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.*;
public class Main {
    static Scanner sc = new Scanner(System.in);
    static void list1(int a[],int low,int high){
        if(low == high){
            return;
        }
        int x = a[low];
        while(low < high){
            while(low < high && a[high] > x)high--;
            a[low] = a[high];
            while(low < high && a[low] < x)low++;
            a[high] = a[low];
        }
        a[low] = x;


    }



    public static void main(String[] args) throws IOException {



    }

}

也就是说,首先定义一个x作为一个基准,一般x就是第一个元素。然后定义low和high,第一次从后往前找,如果找到比x小的数,那么这个数就填到low的地方,然后开始从前往后找,如果找到了比x打的数,那就把这个数填到刚才high的地方,一直重复,直到low=high,那么就把x填到这个位置来,就能得到想要的结果了

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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