2011-11-15 31 views
0

intのランダムな配列を作成し、それを自分のクラスでソートする必要があります。私は私の配列を作るのはここです:int型のランダム配列を作成します。 Java

public class MyProgram9{ 
public static void main(String[] args){ 

    int[] list = new int[10]; 
    for (int i=0; i<10; i++){ 
     int n = (int)(Math.random()*9 + 1); 
     list[i] = n; 

     System.out.println(list[i] + " "); 
    } 
    list.QuickSort(); 
} 
} 

私はそれ(クイックソートクラス)をソートする別のクラスを使用しようとしています。私の質問は、私がそれを使用できるように、同じクラスからこのクラスを実装する方法です。 quickSortクラスは次のとおりです。

public class QuickSort{ 
public static void quickSort(int[] list){ 
quickSort(list, 0, list.length - 1); 
    } 

private static void quickSort(int[] list, int first, int last) { 
if (last > first) { 
    int pivotIndex = partition(list, first, last); 
    quickSort(list, first, pivotIndex - 1); 
    quickSort(list, pivotIndex + 1, last); 
} 
} 

/** Partition the array list[first..last] */ 
private static int partition(int[] list, int first, int last) { 
int pivot = list[first]; // Choose the first element as the pivot 
int low = first + 1; // Index for forward search 
int high = last; // Index for backward search 

while (high > low) { 
    // Search forward from left 
    while (low <= high && list[low] <= pivot) 
    low++; 

    // Search backward from right 
    while (low <= high && list[high] > pivot) 
    high--; 

    // Swap two elements in the list 
    if (high > low) { 
    int temp = list[high]; 
    list[high] = list[low]; 
    list[low] = temp; 
    } 
} 

while (high > first && list[high] >= pivot) 
    high--; 

// Swap pivot with list[high] 
if (pivot > list[high]) { 
    list[first] = list[high]; 
    list[high] = pivot; 
    return high; 
} 
else { 
    return first; 
    } 
    } 
} 

ごめんなさい。あなたのコードが間違っている2つのクラスを接続する方法を意味する場合

+0

であなたmainメソッドの最後の行を交換する必要がありますか? – Tudor

+0

'list.QuickSort()'は有効なJavaではありません。私はあなたがおそらく ' – mre

+1

は宿題のような音..あなたは、コンパイルコードでの例が含ま示唆しています。どうすればいい? – Laf

答えて

3

。配列のstatic quicksortメソッドを呼び出さなければなりません。同様:

public class MyProgram9{ 
public static void main(String[] args){ 

    int[] list = new int[10]; 
    for (int i=0; i<10; i++){ 
     int n = (int)(Math.random()*9 + 1); 
     list[i] = n; 

     System.out.println(list[i] + " "); 
    } 
    QuickSort.quicksort(list); 
} 
} 
0

あなたが同じフォルダから何を意味するのです

QuickSort.quickSort(list); 
0
import java.util.Random; 

public class Array { 

    public static void main(String[] args) { 
     Random random= new Random(); 
     int numbers[]= new int[10]; 
     for (int i = 0; i < 10; i++) 
     { 
     int number= random.nextInt(100); 
     System.out.println(number); 
     numbers[i]=number; 
     } 
     for (int j = 0; j < numbers.length; j++) { 
      System.out.println(numbers[j]); 
     } 
    } 

} 
関連する問題