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))的更多相关文章

  1. 63.如何对单链表进行快排?和数组快排的分析与对比[quicksort of array and linked list]

    [本文链接] http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html [题目] 单链表的特点是:单向. ...

  2. 快排算法Java版-每次以最左边的值为基准值手写QuickSort

    如题 手写一份快排算法. 注意, 两边双向找值的时候, 先从最右边起找严格小于基准值的值,再从最左边查找严格大于基准base的值; 并且先右后左的顺序不能反!!这个bug改了好久,233~ https ...

  3. QuickSort(快排)的JAVA实现

    QuickSort的JAVA实现 这是一篇算法课程的复习笔记 用JAVA对快排又实现了一遍. 先实现的是那个easy版的,每次选的排序轴都是数组的最后一个: package com.algorithm ...

  4. 排序--QuickSort 快排

    Quick の implementation 快排,就像它的名字一定,风一样的快.基本上算是最快的排序算法了.快排的基本思想是选择一个切分的元素.把这个元素排序了.所有这个元素左边的元素都小于这个元素 ...

  5. 快排 快速排序 qsort quicksort C语言

    现在网上搜到的快排和我以前打的不太一样,感觉有点复杂,我用的快排是FreePascal里/demo/text/qsort.pp的风格,感觉特别简洁. #include<stdio.h> # ...

  6. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  7. 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》

    前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...

  8. java排序,冒泡排序,选择排序,插入排序,快排

    冒泡排序 时间复杂度:O(n^2) 空间复杂度O(1) 稳定性:稳定 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.这步做完后,最 ...

  9. iOS常见算法(二分法 冒泡 选择 快排)

    二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number)  { int start = 0; int end = l ...

随机推荐

  1. LeetCode581. Shortest Unsorted Continuous Subarray

    Description Given an integer array, you need to find one continuous subarray that if you only sort t ...

  2. Yii简单使用阿里云短信教程!

    首先我们下载官方完整包的SDK官方标明了啊,PHP版本一定要不低于5.5下载后目录如下: 我们只需要将目录里的api_sdk复制出来到Yii项目的根目录的common模块下面的extensions文件 ...

  3. https原理与实践

    HTTPS 原理与证书实践   分类: Web应用   1.1 网络安全知识 1.1.1 网结安全出现背景 网络就是实现不同主机之间的通讯,网络出现之初利用TCP/IP协议簇的相关协议概念,已经满足了 ...

  4. select option 不可以选

    <select> <option>Volvo</option> <option>Saab</option> <option disab ...

  5. 通过Get方式传递数据

    1:因为get传参数有个特点就是不能超过256字节.如果数据大的话会溢出. 解决办法: $data=json_encode($data_array); 然后在拼接超链接: <a href=&qu ...

  6. 第一百九十八节,jQuery EasyUI,ProgressBar(进度条)组件

    jQuery EasyUI,ProgressBar(进度条)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 ProgressBar(进度条) ...

  7. Eclipse Java 构建路径

    Eclipse Java 构建路径 设置 Java 构建路径 Java构建路径用于在编译Java项目时找到依赖的类,包括以下几项: 源码包 项目相关的 jar 包及类文件 项目引用的的类库 我们可以通 ...

  8. WPF 属性系统 依赖属性之内存占用分析

    关于WPF的属性系统园子内有不少这方面的文章.里面大都提到了WPF依赖属性的在内存方面的优化.但是里面大都一笔带过.那么WPF到底是怎么样节约内存的.我们通过WPF属性和普通的CLR属性对比来看一下W ...

  9. OpenCV学习笔记廿一:opencv_contrib模块

    一,简介: 该库为新加入代码库的算法.

  10. awk合并两个文件并显示

    问题: 答案: