Erstellen Sie eine zufällige array von Typ int. Java
Brauche ich, um eine zufällige array von int-und haben es sortiert durch meine eigene Klasse. Hier ist, wo ich mein array:
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();
}
}
Ich dann versuche, mit einer anderen Klasse zu Sortieren(QuickSort-Klasse). Meine Frage ist, wie Binde ich diese Klasse aus dem gleichen Ordner, so dass ich es verwenden kann. Hier ist der quickSort-Klasse:
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;
}
}
}
Sorry für die info.
- Was meinst du aus dem selben Ordner?
list.QuickSort()
ist keine gültige Java -. Ich schlage vor, Sie enthalten ein Beispiel mit kompilierbare code..- Klingt nach Hausaufgaben...
- Sie wollen wahrscheinlich zu call
QuickSort.quickSort(list)
- quicksort ist einfach eine andere Klasse, die ich bin versucht zu rufen, aus meiner MyProgram9 Klasse. Wie kann ich dies tun
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wenn du meinst, wie die Verbindung der beiden Klassen Ihr code ist falsch. Sie müssen rufen Sie die statische quicksort-Methode auf das array. Wie:
Ersetzen Sie die Letzte Zeile deiner main-Methode mit