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 2 of 2

Thread: How do you sort an array? [using bubble sort]

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do you sort an array? [using bubble sort]

    Here's my code:
    import java.util.*;
     
    class array1{
    	public static void main(String args[]){
    		Random number = new Random();
     
    		int num[] = new int[100];
     
    		// filling array with random numbers
    		for(int i=0;i<100;i++){
    			int randomnumber = 10+number.nextInt(89);
    			num[i] = randomnumber;
    			System.out.print(num[i]+" ");
    		}
                    // Sorting the array
    		for(int i=0;i<100;i++){
    			for(int j=0;j<100;j++){
    				if(num[j]>num[j+1]){
    					num[j]=num[j+1];
    					num[j+1]=num[j];
     
    				}
    			}
    		}
    	}
    }
    What should I put after the "num[j+1]=num[j]"?


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

    Default Re: How do you sort an array? [using bubble sort]

    This:
    num[j]=num[j+1];
    num[j+1]=num[j];
    Will not swap values. For example, let's say:
    num[j]=10
    num[j+1]=12
    After we run the first line of code, the values become:
    num[j]=12
    num[j+1]=12
    And after we run the second line of code, the values stay:
    num[j]=12
    num[j+1]=12

    Why? Because we never "remembered" the previous value of num[j]
    Instead, you want to:
    1. Store the value of num[j] in a variable (int var, for example)
    2. Do the first line of code from earlier
    3. Instead of the second line of code, we want: num[j+1] = var;

    Does that make sense?
    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/

Similar Threads

  1. Replies: 2
    Last Post: November 11th, 2012, 10:44 PM
  2. array bubble sort
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2012, 05:53 PM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  4. Bubble Sort Int Array Problem
    By thisbeme in forum Collections and Generics
    Replies: 1
    Last Post: September 11th, 2011, 09:24 AM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM

Tags for this Thread