[Java] 蓝桥杯ALGO-59 算法训练 快速排序

栏目: Java · 发布时间: 7年前

内容简介:问题描述输入格式输出格式

问题描述

快速排序是最经常使用的一种 排序 方式,对于给定的n个数组成的一个数组,请使用快速排序对其进行排序。

输入格式

第一行一个数N。

输出格式

共N行,每行一个数,表示所求序列。

样例输入

5

1

4

2

3

样例输出

1

2

3

4

数据规模和约定

共10组数据。

对100%的数据,N<=10^5,所有数均为非负数且在int范围内。

package algo59;
// 自己写的快速排序在遇到一个极端情况,会出现超时
import java.io.*;
import java.util.Arrays;
 
public class Main {
 
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        num = new int[n];
        for (int i = 0; i < n; i++) {
            num[i] = Integer.parseInt(reader.readLine());
        }
        reader.close();
//        quickSort();
        Arrays.sort(num);
        
        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i]);
        }
    }
    
    private static int[] num;
 
//    private static void quickSort() {
//        quickSort(0, num.length - 1);
//    }
//    
//    private static void quickSort(int l, int r) {
//        if (l < r) {
//            int pivot = partion(l, r);
//            quickSort(l, pivot - 1);
//            quickSort(pivot + 1, r);
//        }
//    }
//    
//    private static int partion(int l, int r) {
//        swap(l, (l + r) / 2);
//        int pivot = l;        
//        while (true) {
//            while (l <= r && num[l] < num[pivot]) {
//                l++;
//            }
//            while (l <= r && num[r] > num[pivot]) {
//                r--;
//            }
//            
//            if (l < r) {
//                swap(l, r);
//                l++;
//                r--;
//            } else {
//                break;
//            }
//        }
//        swap(r, pivot);
//        return r;
//    }
//    
//    private static void swap(int i, int j) {
//        int temp = num[i];
//        num[i] = num[j];
//        num[j] = temp;
//    }
}
❤❤点击这里 -> 订阅PAT、蓝桥杯、GPLT天梯赛、LeetCode题解离线版❤❤ [Java] 蓝桥杯ALGO-59 算法训练 快速排序

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Lean Startup

The Lean Startup

Eric Ries / Crown Business / 2011-9-13 / USD 26.00

更多中文介绍:http://huing.com Most startups fail. But many of those failures are preventable. The Lean Startup is a new approach being adopted across the globe, chan ging the way companies are built and ......一起来看看 《The Lean Startup》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具