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.


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

Results 1 to 2 of 2

Thread: I think something wrong with my Bubble sort

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Thanked 0 Times in 0 Posts

    Default I think something wrong with my Bubble sort

     <import java.util.Scanner;
    public class RockefellerA7 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please Enter Number of Rows: ");
    		int row = input.nextInt();
    		System.out.println("Please Enter Number of Columns: ");
    		int column = input.nextInt();
    		int [][] arr = new int[row][column];
    		System.out.println("Enter Values: ");
    		for(int i = 0; i < arr.length; i++){
    			for(int j = 0; j < arr[i].length; j++){
    				arr[i][j] = input.nextInt();		
    		boolean swapped = false;
    		do {
    			swapped = false;
    			for(int i = 0; i < arr.length-1; i++){
    				for(int j = 0; j< arr[i].length-1; j++){
    					if (i== arr.length - 1 && j== arr.length-1){
    					else if (j == arr[i].length-1){
    						if (arr[i][j]> arr[i][0]){
    							int temp = arr[i][0];
    							arr[i][0] = arr[i+1][0];
    							arr[i+1][0] = temp;
    							swapped = true;
    					else {
    						if(arr[i][j] < arr[i+1][j+1]){
    						int temp = arr[i][j];
    						arr[i][j] = arr[i+1][j+1];
    						arr[i+1][j+1] = temp;
    						swapped = true;
    		while (swapped);
    		for (int i = 0; i < arr.length; i++){
    			for (int j = 0; j < arr[i].length; j++){
    				System.out.printf("%4d", arr[i][j]);

    I am entering {25,8,12,13,5,3,88,0,2,1,92,4}
    and my output is:
    92 88 12 13
    5 22 8 0
    2 1 3 4

    Here is my program. I have to sort my array so it will go smallest to largest but it isnt working HELP
    three conditions it has to fill:
    1. if Iis the row size -1 and j is the column -1 do nothing
    2. else if j is the column size -1 check the values and swap
    3. other wise just check the values and swap

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,635 Times in 2,585 Posts

    Default Re: I think something wrong with my Bubble sort

    it isnt working
    Please explain what "isnt working" means. Post the program's output and add some comments describing what is wrong with the output and show what it should be.

    For easier testing and to ensure everyone is testing with the same values, define the arr array with numbers for testing and comment out getting the numbers from the user.
    For example:
    arr = {{2,3,4},{4,5,6}, {5,4,3}};

    Please edit your post and wrap your code with
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Bubble Sort Help
    By asundar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 24th, 2012, 05:14 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. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 08:47 PM
  4. 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
  5. Javascript Bubble Sort
    By Fidelacchius in forum What's Wrong With My Code?
    Replies: 17
    Last Post: May 24th, 2010, 04:43 PM