# Thread: trying to generate 1000 random numbers in ascending order virticaly

1. Member Join Date
May 2015
Posts
5
Rep Power
0

## trying to generate 1000 random numbers in ascending order virticaly

Ok
I have my algorithm and it finally works but the problem I'm having is in my random number generator I want it to print 1000 numbers in ascending order. the ascending order I got but it prints like 10-12 numbers and starts another 10 and so on till it gets to a thousand. another problem I'm having is that it doesn't print vertically. I put a new line in every position I can imagine and I get the same result. what am I doing wrong here?

Java Code:
```public final class QuickSort {

private static int[] theArray;

private static int arraySize;

public static void main(String[] args) {

QuickSort theSort = new QuickSort(1001);

theSort.generateRandomArray();

System.out.println(Arrays.toString(QuickSort.theArray));

theSort.quickSort(0, 9);

System.out.println(Arrays.toString(QuickSort.theArray));

}

QuickSort(int newArraySize) {

arraySize = newArraySize;

theArray = new int[arraySize];

generateRandomArray();

}

public void quickSort(int left, int right) {

if (right - left <= 0)
return; // Everything is sorted

else {

// It doesn't matter what the pivot is, but it must
// be a value in the array

int pivot = theArray[right];

System.out.println("Value in right " + theArray[right]
+ " is made the pivot");

System.out.println("left = " + left + " right= " + right
+ " pivot= " + pivot + " sent to be partitioned");

int pivotLocation = partitionArray(left, right, pivot);

System.out.println("Value in left " + theArray[left]
+ " is made the pivot");

quickSort(left, pivotLocation - 1); // Sorts the left side

quickSort(pivotLocation + 1, right);

}

}

public int partitionArray(int left, int right, int pivot) {

int leftPointer = left - 1;

int rightPointer = right;

while (true) {

while (theArray[++leftPointer] < pivot)
;

//printVirtArray(leftPointer, rightPointer);

System.out.println(theArray[leftPointer] + " in index "
+ leftPointer + " is bigger than the pivot value " + pivot);

while (rightPointer > 0 && theArray[--rightPointer] > pivot)
;

//printVirtArray(leftPointer, rightPointer);

System.out.println(theArray[rightPointer] + " in index "
+ rightPointer + " is smaller than the pivot value "
+ pivot);

//printVirtArray(leftPointer, rightPointer);

if (leftPointer >= rightPointer) {

System.out.println("left is >= right so start again");

break;

}

else {

swapValues(leftPointer, rightPointer);

System.out.println(theArray[leftPointer] + " was swapped for "
+ theArray[rightPointer]);

}

}

swapValues(leftPointer, right);

return leftPointer;

}

public void swapValues(int indexOne, int indexTwo) {

int temp = theArray[indexOne];
theArray[indexOne] = theArray[indexTwo];
theArray[indexTwo] = temp;

}

public void generateRandomArray() {

for (int i = 0; i < arraySize; i++) {

// Generate a random array with values between
// 0 and 1000

theArray[i] = (int) (Math.random() * 10000) + 0;

}

}
}```  Reply With Quote

2. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: trying to generate 1000 random numbers in ascending order virticaly

First, the Arrays.toString prints the array horizontally. If you want them printed vertically, then simply do it in a for loop.

Second, regarding your sorting algorithm, I think the following statement might be a problem.

Java Code:
`theSort.quickSort(0, 9);`
Regards,
Jim  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•