Создание случайного массива типа int.Джава - PullRequest
0 голосов
/ 15 ноября 2011

Мне нужно создать случайный массив int и отсортировать его по своему классу. Вот где я делаю свой массив:

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;
   }
  }
}

Извините за всю информацию.

Ответы [ 3 ]

3 голосов
/ 15 ноября 2011

Если вы имеете в виду, как соединить два класса, ваш код неверен. Вы должны вызвать статический метод быстрой сортировки в массиве. Как:

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 голосов
/ 23 июня 2015
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]);
        }
    }

}
0 голосов
/ 15 ноября 2011

Вам нужно заменить последнюю строку основного метода на

QuickSort.quickSort(list);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...