0

I need to create a random array of int and have it sorted by my own class. Here is where I make my 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();
 }
}

I then am trying to use another class to sort it(the QuickSort class). My Question is how do I implement this class from the same folder so that I can use it. Here is the quickSort class:

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 about all the info.

4
  • What do you mean from the same folder? Commented Nov 15, 2011 at 19:47
  • list.QuickSort() is not valid Java. I suggest you include an example with compilable code.. Commented Nov 15, 2011 at 19:48
  • 3
    You probably want to call QuickSort.quickSort(list) Commented Nov 15, 2011 at 19:49
  • quicksort is just a different class i am trying to call from my MyProgram9 class. How would I do this Commented Nov 15, 2011 at 19:50

3 Answers 3

3

If you mean how to connect the two classes your code is wrong. You have to call the static quicksort method on the array. Like:

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);
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to replace the last line of you main method with

QuickSort.quickSort(list);

Comments

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.