Results 1 to 2 of 2
  1. #1
    apacheomega is offline Member
    Join Date
    May 2015
    Posts
    5
    Rep Power
    0

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

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

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2012, 04:07 PM
  2. Replies: 8
    Last Post: 11-23-2012, 04:39 PM
  3. Replies: 19
    Last Post: 10-29-2012, 01:51 AM
  4. generate pseudo random numbers in java
    By csr81 in forum Advanced Java
    Replies: 3
    Last Post: 03-01-2010, 07:08 AM
  5. Replies: 8
    Last Post: 04-19-2009, 05:50 PM

Posting Permissions

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