【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 ...
随机推荐
- Python Theano TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)
参考: https://groups.google.com/forum/#!topic/theano-users/teA-07wOFpE 这个问题出现的原因是,我在读文件的时候,应该Train_X读成 ...
- 微信小程序 缓存说明
每个微信小程序都可以有自己的本地缓存,可以通过 wx.setStorage(wx.setStorageSync).wx.getStorage(wx.getStorageSync).wx.clearSt ...
- 更新mac系统和更新到Xcode7.3版本出现的: cannot create __weak reference in file using manual reference counting
之前的编程没有遇到过,应该是苹果官方那边又做了新规吧. 不过不要紧,只要根据这个就能解决报错问题. Set Build Settings -> Apple LLVM 7.1 - Languag ...
- Sql server还原失败(数据库正在使用,无法获得对数据库的独占访问权)
Sql server还原失败(数据库正在使用,无法获得对数据库的独占访问权) 问题分析:数据库还原的时候还有其他进程连在上面,导致无法获得独占造成的. 解决方案: 一.切断连接进程 .查询要还原的数据 ...
- jQuery插件-json2.js
from:http://blog.csdn.net/gjb724332682/article/details/46682743 前言 json2.js是一个json插件,下载地址:https://gi ...
- SQL Server 日期格式化输出
转自:http://hi.baidu.com/%BC%D1%C0%D6%B1%C8%BA%A3/blog/item/fdaf6c9525adfa0f7af480ec.html T-SQL Script ...
- jquery 插件 起步代码
/** * Created by W.J.Chang on 2014/6/25. */ ;(function($) { var methods= { check: function() { retur ...
- POJ1182食物链(并查集经典好题)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66964#problem/E 题目思路:主要有两种思路:1.带权并查集2.挑战程 ...
- 单片机c语言教程:C51循环语句
单片机c语言教程第十三课 C51循环语句 循环语句是几乎每个程序都会用到的,它的作用就是用来实现需要反复进行多次的操 作.如一个 12M 的 51 芯片应用电路中要求实现 1 毫秒的延时,那么就要执行 ...
- <2013 08 17> BucketList of girlfriend
BucketList of girlfriend 1.出国旅游 2.跟相爱的人结婚,生个健康可爱的孩子 3.说一口流利的英语 4.学素描和水彩 5.买个雅马哈钢琴,偶尔学着弹一首曲子 6.把泪腺堵住 ...