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

Thread: Buble sort issue

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

    Default Buble sort issue

    this program will output the series of number IF among the random series there is a number larger than the consecutive number.
    BUT, what happens if the numbers will be randomly order in the right order? (for example 3 5 6 7).
    the "if" will not work, therefore - swapped will be false and as a result the program will not print anything..... how can i fix it?
    thanks!

    public class MyArray {

    public static void main(String[] args) {
    int[] arr = new int[10];
    for(int i=0; i<arr.length; i++)
    arr[i] = (int) (1 + Math.random()*100);
    for(int i=0; i<arr.length; i++)
    System.out.print(arr[i]+" ");
    System.out.println();
    int x = arr.length;
    boolean swapped;
    // Sort the array using bubble sort
    do {
    x--;
    swapped = false;
    for(int j=0; j<x; j++) {
    // Switch locations between arr[j] and arr[j+1]
    if(arr[j]>arr[j+1]) {
    swapped = true;
    int temp = arr[j+1];
    arr[j+1] = arr[j];
    arr[j] = temp;
    }
    }
    } while(swapped);
    for(int i=0; i<arr.length; i++)
    System.out.print(arr[i]+" ");
    System.out.println();
    }
    }


  2. #2
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Buble sort issue

    In a bubble sort, you go through an array 1..n elements n-1 times. First pass you get the biggest or smallest value and swap it with whatever was in x[1]. On the second pass you go from 2..n and store the result in x[2], and so on. As you compare each value x[2]..x[n] with x[1] you may have to swap several times in a pass.

    Try sticking in a System.out.println(); for each comparison in each pass and for the array at the end of a pass for the whole array. Do you achieve the objective of the first pass by the end of the first pass? The others?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Buble sort issue

    Hi!
    This is the corresponding algorithm
    ...

    Hope this helps
    Last edited by copeg; November 7th, 2011 at 09:43 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Buble sort issue

    Karapet, please read the forums rules and the following: The problems with cross-posting

Similar Threads

  1. Bucket Sort Help
    By itispj in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2011, 10:59 PM
  2. [SOLVED] Bucket or Bin Sort
    By itispj in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2011, 07:14 PM
  3. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 09:47 PM
  4. Insertion Sort
    By Kimimaru in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2010, 06:26 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, 10:44 AM