【HackerRank】QuickSort(稳定快排,空间复杂度O(n))
QuickSort
In the previous challenge, you wrote a partition method to split an array into 2 sub-arrays, one containing smaller elements and one containing larger elements. This means you 'sorted' half the array with respect to the other half. Can you repeatedly use partition to sort an entire array?
Guideline
In Insertion Sort, you simply went through each element in order and
inserted it into a sorted sub-array. In this challenge, you cannot focus
on one element at a time, but instead must deal with whole sub-arrays,
with a strategy known as "divide and conquer".
When partition is called on an array, two parts of the array get 'sorted' with respect to each other. If partition
is then called on each sub-array, the array will now be split into 4
parts. This process can be repeated until the sub-arrays are small.
Notice that when partition is called on just two numbers, they end up
being sorted.
Can you repeatedly call partition so that the entire array ends up sorted?
Print Sub-Arrays
In this challenge, print your array every time your partitioning method
finishes, i.e. print every sorted sub-array The first element in a
sub-array should be used as a pivot. Partition the left side before
partitioning the right side. The pivot should not be added to either
side. Instead, put it back in the middle when combining the sub-arrays
together.
Input Format
There will be two lines of input:
- n - the size of the array
- ar - the n numbers of the array
Output Format
Print every partitioned sub-array on a new line.
Constraints
1<=n<=1000
-1000<=x<= 1000 , x ∈ ar
There are no duplicate numbers.

题解:多用O(n)的空间实现Partition函数。不过感觉更清晰。
代码如下:
import java.util.*;
public class Solution { static int partition(int[] ar,int start,int end) {
ArrayList<Integer> smaller = new ArrayList<Integer>();
ArrayList<Integer> bigger = new ArrayList<Integer>();
int pivot = ar[start];
for(int i = start+1;i < end;i++){
if(ar[i] <= pivot)
smaller.add(ar[i]);
else
bigger.add(ar[i]);
} int i = 0;
for(i = 0;i<smaller.size();i++)
ar[start+i] = smaller.get(i);
ar[start+i] = pivot;
int p = start+i;
for(;i-smaller.size()<bigger.size();i++)
ar[start+i+1] = bigger.get(i-smaller.size()); return p; }
static void quickSort(int[] ar,int start,int end) {
if(start >= end - 1)
return;
int p = partition(ar,start,end);
quickSort(ar, start, p);
quickSort(ar, p+1, end);
printArray(ar, start, end);
} static void printArray(int[] ar,int start,int end) {
StringBuffer sb = new StringBuffer();
for(int i = start;i < end;i++)
sb.append(ar[i]).append(" ");
System.out.println(sb.toString());
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
for(int i=0;i<n;i++){
ar[i]=in.nextInt();
}
quickSort(ar,0,ar.length);
}
}
【HackerRank】QuickSort(稳定快排,空间复杂度O(n))的更多相关文章
- 63.如何对单链表进行快排?和数组快排的分析与对比[quicksort of array and linked list]
[本文链接] http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html [题目] 单链表的特点是:单向. ...
- 快排算法Java版-每次以最左边的值为基准值手写QuickSort
如题 手写一份快排算法. 注意, 两边双向找值的时候, 先从最右边起找严格小于基准值的值,再从最左边查找严格大于基准base的值; 并且先右后左的顺序不能反!!这个bug改了好久,233~ https ...
- QuickSort(快排)的JAVA实现
QuickSort的JAVA实现 这是一篇算法课程的复习笔记 用JAVA对快排又实现了一遍. 先实现的是那个easy版的,每次选的排序轴都是数组的最后一个: package com.algorithm ...
- 排序--QuickSort 快排
Quick の implementation 快排,就像它的名字一定,风一样的快.基本上算是最快的排序算法了.快排的基本思想是选择一个切分的元素.把这个元素排序了.所有这个元素左边的元素都小于这个元素 ...
- 快排 快速排序 qsort quicksort C语言
现在网上搜到的快排和我以前打的不太一样,感觉有点复杂,我用的快排是FreePascal里/demo/text/qsort.pp的风格,感觉特别简洁. #include<stdio.h> # ...
- Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》
前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...
- java排序,冒泡排序,选择排序,插入排序,快排
冒泡排序 时间复杂度:O(n^2) 空间复杂度O(1) 稳定性:稳定 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.这步做完后,最 ...
- iOS常见算法(二分法 冒泡 选择 快排)
二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number) { int start = 0; int end = l ...
随机推荐
- mysql explain22222 system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
MySQL性能分析及explain用法的知识是本文我们主要要介绍的内容,接下来就让我们通过一些实际的例子来介绍这一过程,希望能够对您有所帮助. .使用explain语句去查看分析结果 如explain ...
- pycharm2018.1.4激活破解方法与汉化包-2018年6月19日
记录下来备用,顺便分享给大家,有能力的还是希望能够支持正版!支持正版!支持正版! 方法1:激活服务器,最简单快速(截止2018年6月19日可用) 在激活Jetbrains旗下任意产品的时候选择激活服务 ...
- 安装VC6.0遇到的问题
1. 问题现象 安装VC6.0后,又安装了VS2005.用VC6.0打开以前的.dsw文件时,程序自动关闭.如下图所示. 具体操作是:选择一个.dsw文件,右键菜单[打开方式]中选择[Microsof ...
- weixin oauth 授权
1. 先了解下请求授权页面的构造方式: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_u ...
- js instanceof (2)
instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上.实例一:普遍用法 A instanceof B :检测B.prototype是否存在于参 ...
- azure绑定ssl,godaddy的ssl证书
域名是godaddy 申请的,证书也是godaddy 购买的,DV证书. godaddy购买证书后,申请ssl,需要输入,csr.网上找的csr生成工具,我使用 https://myssl.com/ ...
- CFindReplaceDialog学习
The CFindReplaceDialog class allows you to implement standard string Find/Replace dialog boxes in yo ...
- Laravel5.1 -控制器(初步了解)
首先道个歉 这篇笔记是前两天就应该写的,可大K有点事儿要忙 就耽误了,今天抽空学了学控制器,并写个笔记分享下. 为什么要使用控制器 像我们之前写一些逻辑呢都是在Route(路由)中,搞得Route文件 ...
- Python简单分布式爬虫
分布式爬虫采用主从模式.主从模式是指由一台主机作为控制节点,负责管理所有运行网络爬虫的主机(url管理器,数据存储器,控制调度器),爬虫只需要从控制节点哪里接收任务,并把新生成任务提交给控制节点.此次 ...
- idea 不能编译生成class文件
问题:开发工程中将idea中编译输出目录 out 删掉.发现再次编译不能生成class文件 解决方案:settings -> compiler 勾选自动编译选项