码迷,mamicode.com
首页 > 其他好文 > 详细

【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

时间:2014-08-02 12:12:53      阅读:512      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   strong   io   

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.

bubuko.com,布布扣


 

题解:多用O(n)的空间实现Partition函数。不过感觉更清晰。

代码如下:

 1 import java.util.*;
 2 public class Solution {
 3        
 4     static int partition(int[] ar,int start,int end) {
 5         ArrayList<Integer> smaller = new ArrayList<Integer>();
 6         ArrayList<Integer> bigger = new ArrayList<Integer>();
 7         int pivot = ar[start];
 8         for(int i = start+1;i < end;i++){
 9             if(ar[i] <= pivot)
10                 smaller.add(ar[i]);
11             else 
12                 bigger.add(ar[i]);            
13         }
14       
15         int i = 0;
16         for(i = 0;i<smaller.size();i++)
17             ar[start+i] = smaller.get(i);
18         ar[start+i] = pivot; 
19         int p = start+i;
20         for(;i-smaller.size()<bigger.size();i++)
21             ar[start+i+1] = bigger.get(i-smaller.size());
22         
23         return p;
24         
25     }
26        static void quickSort(int[] ar,int start,int end) {
27            if(start >= end - 1)
28                return;
29            int p = partition(ar,start,end);
30            quickSort(ar, start, p);
31            quickSort(ar, p+1, end);
32            printArray(ar, start, end);
33        }   
34  
35       static void printArray(int[] ar,int start,int end) {
36          StringBuffer sb = new StringBuffer();
37          for(int i = start;i < end;i++)
38              sb.append(ar[i]).append(" ");
39          System.out.println(sb.toString());
40       }
41        
42       public static void main(String[] args) {
43            Scanner in = new Scanner(System.in);
44            int n = in.nextInt();
45            int[] ar = new int[n];
46            for(int i=0;i<n;i++){
47               ar[i]=in.nextInt(); 
48            }
49            quickSort(ar,0,ar.length);
50        }    
51    }

 

【HackerRank】QuickSort(稳定快排,空间复杂度O(n)),布布扣,bubuko.com

【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

标签:style   blog   http   color   java   os   strong   io   

原文地址:http://www.cnblogs.com/sunshineatnoon/p/3886535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!