2014-03-14 93 views
1

,所以我做的位於https://www.hackerrank.com/challenges/quicksort2和IM遇到問題纏繞我的頭周圍做這個,他們希望的方式提出了挑戰。他們所要求的程序使用的子陣列做快速排序,然後把這些子排列在一起,在最後拿出結果。這是一個挑戰,我將在下面有我的代碼。不用說,我沒有它的工作。提供所有代碼,挑戰是編寫分區方法有問題了快速排序法

打印子陣列 在此挑戰中,每次分區方法結束時打印數組,即打印每個已排序的子數組。子數組中的第一個元素數組應該被用作一個樞軸。在分割右側之前分割左側。樞軸不應該添加到任何一方。相反,把它放回中間的子陣列結合在一起的時候。

輸入格式

將有輸入的兩行:

n - the size of the array 
ar - the n numbers of the array 

輸出格式

打印在新的一行的每個劃分的子陣列。

約束

1<=n<=1000 
-1000<=x<= 1000 , x ∈ ar 
There are no duplicate numbers. 

採樣輸入

7 
5 8 1 3 7 9 2 

樣本輸出

2 3 
1 2 3 
7 8 9 
1 2 3 5 7 8 9 

代碼

import java.util.*; 

public class Solution { 
static int[] result; 
static void partition(int[] ar) { 
int p = 0; 
    int s = 0; 
    int l = 0; 
    int small[] = new int[ar.length]; 
    int pivot[] = new int[ar.length]; 
    int large[] = new int[ar.length]; 
    for(int i = 0; i < ar.length; i++){ 
     if(i == 0 || ar[i]==pivot[0]){ 
      pivot[p] = ar[i]; 
      p++; 
     } 
     else if(ar[i]>pivot[0]){ 
      large[l] = ar[i]; 
      l++; 
     } 
     else if(ar[i]<pivot[0]){ 
      small[s] = ar[i]; 
      s++; 
     } 
    } 
    if(s>2){ 
     int[] smallA = new int[s]; 
     for(int i = 0; i < s; i ++){ 
      smallA[i] = small[i]; 
     } 
     partition(smallA); 
    } 
    if(l>2){ 
     int[] largeA = new int[l]; 
     for(int i = 0; i < l; i ++){ 
      largeA[i] = large[i]; 
     } 
     partition(largeA); 
    } 

} 

static void printArray(int[] ar) { 
    for(int n: ar){ 
     System.out.print(n+" "); 
    } 
    System.out.println(""); 
} 

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 
    int n = in.nextInt(); 
    int[] ar = new int[n]; 
    result = new int[n]; 
    for(int i=0;i<n;i++){ 
     ar[i]=in.nextInt(); 
    } 
    partition(ar); 
}  
} 

我必須使用這種格式,我可以編輯分區方法,但每次挑戰

回答

0

我沒有檢查你的代碼的其餘規則停留,但這似乎工作。它更容易使用列表

import java.util.*; 

public class quickSorter 
{ 

    public quickSorter() 
    { 
    } 

    public List<Integer> partition(List<Integer> list){ 

     int pivot = list.get(0); 
     List<Integer> result; 
     List<Integer> leftSide = new ArrayList<>(); 
     List<Integer> rightSide = new ArrayList<>(); 

     for (int i=0;i<list.size();i++){ 
      if (list.get(i)<pivot){ 

       leftSide.add(list.get(i)); 
      } 
      else if (list.get(i)>pivot){ 
       rightSide.add(list.get(i)); 
      } 

     } 

     if (leftSide.size()>1){ 
      result = this.partition(leftSide); 
      leftSide=result; 
     } 
     if (rightSide.size()>1){ 
      result = this.partition(rightSide); 
      rightSide=result; 
     } 
     List<Integer> combined = new ArrayList<>(); 
     combined.addAll(leftSide); 
     combined.add(pivot); 
     combined.addAll(rightSide); 
     //print out 
     for (int j:combined){ 
       System.out.print(j+" "); 
     } 
     System.out.println(); 



     return combined; 
    } 


    public static void main(String[] a){ 

      quickSorter qs = new quickSorter(); 

      List<Integer> list = new ArrayList<>(); 

      System.out.println(); 
      Scanner in = new Scanner(System.in); 
      int n = in.nextInt(); 

      for(int i=0;i<n;i++){ 
       list.add(in.nextInt()); 
      } 
      System.out.println(); 
      List<Integer> sorted = qs.partition(list); 

    } 
} 
+0

我同意它會更容易使用數組列表,但不幸的是他們正在尋找的普通數組 – mig

+0

我無法看到任務 – StackExploded

+0

與所使用的陣列的類型什麼使它我的所有代碼比劃分方法,這表明他們希望的陣列通過被提供的其他。和說明書中他們陳述子陣列,而不是數組列表 – mig