Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with sorting numbers

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with sorting numbers

    My goal is to generate 10,000 random numbers and sort them in arrays. I have the random numbers made easy enough and I have a small quick sort class but I am having trouble combining the two.

    This is the random number generator
    import java.util.*;
     
     
     
    public class NumberGenerator {
    	public static void main(String args[]){
    	Random Generator = new Random();
     
    	int[] myArray = new int[10000];
     
    	for(int idx = 0;idx <=10000; ++idx){
    	    int nums = Generator.nextInt(10000);
    	    myArray[idx] = nums;
     
    	}

    This is the quick sort

    public class QuickSort {
     
     
     
    	int partition(int array[], int left, int right)
    	{
     
    		 int i = left, j = right;
    	     int tmp;
    	     int pivot = array[(left + right) / 2];
     
    	     while (i <= j) {
    	            while (array[i] < pivot)
    	                  i++;
    	            while (array[j] > pivot)
    	                  j--;
    	            if (i <= j) {
    	                  tmp = array[i];
    	                  array[i] = array[j];
    	                  array[j] = tmp;
    	                  i++;
    	                  j--;
    	            }
    	      };
     
    	      return i;
    	}
     
    	void quickSort(int array[], int left, int right) {
    	      int index = partition(array, left, right);
    	      if (left < index - 1)
    	            quickSort(array, left, index - 1);
    	      if (index < right)
    	            quickSort(array, index, right);
    	}
    }

    Thanks for your help


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help with sorting numbers

    Is this for class and you have to make your own sort?

    If not, you can simply say:
    Arrays.sort(myArray);
    and your array will be sorted.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with sorting numbers

    having trouble combining the two
    Are you asking how to call a method in one class from a method in another class?

    One way is shown in post#2. Define the method to be called as static.
    The other way is to create an instance of the object and use that to call the method.

Similar Threads

  1. sorting name using Selectionsort
    By asdfg in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 20th, 2010, 09:44 AM
  2. Sorting/Lexicographic =)
    By jcs990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 12th, 2010, 11:19 PM
  3. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM
  4. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  5. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM