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. tcpdump http://www.cnblogs.com/daisin/articles/5512957.html

    http://www.cnblogs.com/daisin/articles/5512957.html

  2. 如何设置esxi的网卡与网络

    很多朋友安装了vmware esxi后,不懂得服务器上的网卡该如何设置以及如何使用,我们在这里来介绍一下vmware esxi的网卡设置 工具/原料   一台服务器,配有两块千兆网卡 在服务器安装好v ...

  3. CMake 简介与使用

    cross platform make的缩写. 是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目文件.通过编写CMakeLists ...

  4. Linux 文件管理(系统函数)

    //read函数 #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* STDIN_FILEN ...

  5. [AC自己主动机+状压dp] hdu 2825 Wireless Password

    题意: 给n.m,k ,再给出m个单词 问长度为n的字符串.至少在m个单词中含有k个的组成方案有多少种. 思路: 因为m最大是10,所以能够採取状压的思想 首先建立trie图,在每一个单词的结束节点标 ...

  6. python导入模块报错:ImportError: No module named mysql.connector(安装 mysql)

    python的版本是 $ python --version Python 2.7.12 报错代码如下 import mysql.connector 报错信息是 ImportError: No modu ...

  7. 【BZOJ2870】最长道路tree 点分治+树状数组

    [BZOJ2870]最长道路tree Description H城很大,有N个路口(从1到N编号),路口之间有N-1边,使得任意两个路口都能互相到达,这些道路的长度我们视作一样.每个路口都有很多车辆来 ...

  8. Jquery来对form表单提交(mvc方案)

    来自:http://www.cnblogs.com/lmfeng/archive/2011/06/18/2084325.html 我先说明一下,这是asp.net mvc 里面的用法, Jquery来 ...

  9. Spring整合Velocity模版引擎

    1. 首先通过pom.xml自动加载velocity扩展包到工程: <dependency> <groupId>velocity</groupId> <art ...

  10. JS HTML DOM 事件对象(onclick、onmouseenter)

    HTML DOM 事件允许Javascript在HTML文档元素中注册不同事件处理程序. 事件通常与函数结合使用,函数不会在事件发生前被执行! (如用户点击按钮). HTML DOM 事件 DOM:  ...