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. linux IP动态变动之后 , 需要做的杂项操作

    linux的动态ip经常变来变去,目前还没找到固定它不变化的方法.所以每次变动之后都需要做以下的操作,极其麻烦.(必须找到让linux IP 固定的方法) 1.先找到变化之后的动态ip地址 ifcon ...

  2. Vsphere笔记06 Vcenter 部署流程 1

    Vcenter 部署流程 1   一.环境需求   1.需要两台装着WIN2K8 R2 64X的服务器   2.启用一台要添加活动目录角色,并且配置DC,DC的参数如下: 域名:justech-dc. ...

  3. redis 底层数据结构 压缩列表 ziplist

    压缩列表是列表键和哈希键的底层实现之一.当一个列表键只包含少量列表项,并且每个列表项要么就是小整数,要么就是长度比较短的字符串,redis就会使用压缩列表来做列表键的底层实现 当一个哈希键只包含少量键 ...

  4. X264参考手册

    艺搜简介 基本语法: x264 [options]-o outfile infile 注意与ffmpeg的输入输出文件位置恰好相反: ffmpeg[options][[infile options]- ...

  5. 在虚拟机VMware Workstation上安装win7系统

    之前讲过虚拟机的安装过程,虚拟机安装完成之后,就需要在虚拟机上安装操作系统了,这次就讲讲怎么在虚拟机上安装操作系统. 工具/原料   VMware Workstation win7系统盘 iso格式 ...

  6. Ant Design使用问题记录

    公司的测试管理平台前端使用的是Ant Design of React框架,后台使用的是python,数据库用的是mysql.没有参与前期的开发,听说是工作了10年积累下来的一个暂且可用的管理平台,开发 ...

  7. WPF 中双向绑定通知机制之ObservableCollection使用

    msdn中   ObservableCollection<T> 类    表示一个动态数据集合,在添加项.移除项或刷新整个列表时,此集合将提供通知. 在许多情况下,所使用的数据是对象的集合 ...

  8. B. No Time for Dragons(贪心)

    B. No Time for Dragons time limit per test 2.0 s memory limit per test 256 MB input standard input o ...

  9. JS实现当鼠标移动到图片上时,时图片旋转

    <div id="container" style="width:500px;height:400px;position:relative;margin:0 aut ...

  10. hadoop报错java.io.IOException: Bad connect ack with firstBadLink as 192.168.1.218:50010

    [root@linuxmain hadoop]# bin/hadoop jar hdfs3.jar com.dragon.test.CopyToHDFS Java HotSpot(TM) Client ...