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

Thread: Bubble Sort Help

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bubble Sort Help

    Hi everyone,

    I'm trying to do a bubble sort where it takes the largest element and takes it to the right. It works for the most part, but then for some reason the right-most element (after the sorting) becomes 0 and then it acts accordingly. It happens sometimes with arrays, so I don't really know why that is. Here are my bubblesort, swapping, and display codes:

        public void bubblesort()
        {
            int out,in;
     
            for(out=n;out>1;out--)
            {
                for(in=0;in<out;in++)
                {
                    if(a[in]>a[in+1])
                    {
                        swap(in,in+1);
                    }
                    display();
                }
            }
        }

        public void display()
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(a[j] + " ");
            }
            System.out.println();
            System.out.println();
        }

        public void swap(int one,int two)
        {
            long temp=a[one];
            a[one]=a[two];
            a[two]=temp;
        }


  2. #2
    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: Bubble Sort Help

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort Help

    Sure thing:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package array;
     
    /**
     *
     * @author ashwin
     */
    public class Array 
    {
        public long[] a;
        public int n;
     
        public Array(int max)
        {
            a=new long[max];
            n=0;
        }
     
        public void insertelement(int value)
        {
            a[n]=value;
            n++;
        }
     
        public void display()
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(a[j] + " ");
            }
            System.out.println();
            System.out.println();
        }
     
        public void bubblesort()
        {
            int out,in;
     
            for(out=n;out>1;out--)
            {
                for(in=0;in<out;in++)
                {
                    if(a[in]>a[in+1])
                    {
                        swap(in,in+1);
                    }
                    display();
                }
            }
        }
     
        public void swap(int one,int two)
        {
            long temp=a[one];
            a[one]=a[two];
            a[two]=temp;
        }
    }

    Main Class:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package array;
     
    /**
     *
     * @author ashwin
     */
    public class ArrayApp 
    {
        public static void main(String[] args)
        {
            int m=15000;
            Array arr=new Array(m);
     
            arr.insertelement(10);
            arr.insertelement(15);
            arr.insertelement(14);
            arr.insertelement(-5);
            arr.insertelement(10);
            arr.insertelement(1);
            arr.insertelement(43);
            arr.insertelement(27);
            arr.insertelement(33);
            arr.insertelement(6);
            arr.insertelement(8);
            arr.insertelement(39);
     
            System.out.print("Original array: ");
            arr.display();
            System.out.println();
            System.out.println();
     
            arr.bubblesort();
     
            System.out.print("New Array: ");
            arr.display();
        }
    }

    Output:
    run:
    Original array: 10 15 14 -5 10 1 43 27 33 6 8 39 
     
     
     
    10 15 14 -5 10 1 43 27 33 6 8 39 
     
    10 14 15 -5 10 1 43 27 33 6 8 39 
     
    10 14 -5 15 10 1 43 27 33 6 8 39 
     
    10 14 -5 10 15 1 43 27 33 6 8 39 
     
    10 14 -5 10 1 15 43 27 33 6 8 39 
     
    10 14 -5 10 1 15 43 27 33 6 8 39 
     
    10 14 -5 10 1 15 27 43 33 6 8 39 
     
    10 14 -5 10 1 15 27 33 43 6 8 39 
     
    10 14 -5 10 1 15 27 33 6 43 8 39 
     
    10 14 -5 10 1 15 27 33 6 8 43 39 
     
    10 14 -5 10 1 15 27 33 6 8 39 43 
     
    10 14 -5 10 1 15 27 33 6 8 39 0 
     
    10 14 -5 10 1 15 27 33 6 8 39 0 
     
    10 -5 14 10 1 15 27 33 6 8 39 0 
     
    10 -5 10 14 1 15 27 33 6 8 39 0 
     
    10 -5 10 1 14 15 27 33 6 8 39 0 
     
    10 -5 10 1 14 15 27 33 6 8 39 0 
     
    10 -5 10 1 14 15 27 33 6 8 39 0 
     
    10 -5 10 1 14 15 27 33 6 8 39 0 
     
    10 -5 10 1 14 15 27 6 33 8 39 0 
     
    10 -5 10 1 14 15 27 6 8 33 39 0 
     
    10 -5 10 1 14 15 27 6 8 33 39 0 
     
    10 -5 10 1 14 15 27 6 8 33 0 39 
     
    -5 10 10 1 14 15 27 6 8 33 0 39 
     
    -5 10 10 1 14 15 27 6 8 33 0 39 
     
    -5 10 1 10 14 15 27 6 8 33 0 39 
     
    -5 10 1 10 14 15 27 6 8 33 0 39 
     
    -5 10 1 10 14 15 27 6 8 33 0 39 
     
    -5 10 1 10 14 15 27 6 8 33 0 39 
     
    -5 10 1 10 14 15 6 27 8 33 0 39 
     
    -5 10 1 10 14 15 6 8 27 33 0 39 
     
    -5 10 1 10 14 15 6 8 27 33 0 39 
     
    -5 10 1 10 14 15 6 8 27 0 33 39 
     
    -5 10 1 10 14 15 6 8 27 0 33 39 
     
    -5 1 10 10 14 15 6 8 27 0 33 39 
     
    -5 1 10 10 14 15 6 8 27 0 33 39 
     
    -5 1 10 10 14 15 6 8 27 0 33 39 
     
    -5 1 10 10 14 15 6 8 27 0 33 39 
     
    -5 1 10 10 14 6 15 8 27 0 33 39 
     
    -5 1 10 10 14 6 8 15 27 0 33 39 
     
    -5 1 10 10 14 6 8 15 27 0 33 39 
     
    -5 1 10 10 14 6 8 15 0 27 33 39 
     
    -5 1 10 10 14 6 8 15 0 27 33 39 
     
    -5 1 10 10 14 6 8 15 0 27 33 39 
     
    -5 1 10 10 14 6 8 15 0 27 33 39 
     
    -5 1 10 10 14 6 8 15 0 27 33 39 
     
    -5 1 10 10 6 14 8 15 0 27 33 39 
     
    -5 1 10 10 6 8 14 15 0 27 33 39 
     
    -5 1 10 10 6 8 14 15 0 27 33 39 
     
    -5 1 10 10 6 8 14 0 15 27 33 39 
     
    -5 1 10 10 6 8 14 0 15 27 33 39 
     
    -5 1 10 10 6 8 14 0 15 27 33 39 
     
    -5 1 10 10 6 8 14 0 15 27 33 39 
     
    -5 1 10 6 10 8 14 0 15 27 33 39 
     
    -5 1 10 6 8 10 14 0 15 27 33 39 
     
    -5 1 10 6 8 10 14 0 15 27 33 39 
     
    -5 1 10 6 8 10 0 14 15 27 33 39 
     
    -5 1 10 6 8 10 0 14 15 27 33 39 
     
    -5 1 10 6 8 10 0 14 15 27 33 39 
     
    -5 1 6 10 8 10 0 14 15 27 33 39 
     
    -5 1 6 8 10 10 0 14 15 27 33 39 
     
    -5 1 6 8 10 10 0 14 15 27 33 39 
     
    -5 1 6 8 10 0 10 14 15 27 33 39 
     
    -5 1 6 8 10 0 10 14 15 27 33 39 
     
    -5 1 6 8 10 0 10 14 15 27 33 39 
     
    -5 1 6 8 10 0 10 14 15 27 33 39 
     
    -5 1 6 8 10 0 10 14 15 27 33 39 
     
    -5 1 6 8 0 10 10 14 15 27 33 39 
     
    -5 1 6 8 0 10 10 14 15 27 33 39 
     
    -5 1 6 8 0 10 10 14 15 27 33 39 
     
    -5 1 6 8 0 10 10 14 15 27 33 39 
     
    -5 1 6 0 8 10 10 14 15 27 33 39 
     
    -5 1 6 0 8 10 10 14 15 27 33 39 
     
    -5 1 6 0 8 10 10 14 15 27 33 39 
     
    -5 1 0 6 8 10 10 14 15 27 33 39 
     
    -5 1 0 6 8 10 10 14 15 27 33 39 
     
    -5 0 1 6 8 10 10 14 15 27 33 39 
     
    New Array: -5 0 1 6 8 10 10 14 15 27 33 39

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort Help

    Quote Originally Posted by Norm View Post
    Can you make a small complete program that compiles, executes and shows the problem?
    I was able to see my mistake; it was that I iterated n from 0 to out, instead of out-1 because arrays start at 0. I can still post my code, if you would like me to so I can help others that make the same mistake.

  5. #5
    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: Bubble Sort Help

    Glad you got it working.
    Thanks for sharing your code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. bubble sort problem please help!!
    By yanikapausini:) in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 24th, 2012, 04:15 PM
  2. bubble sort timer
    By gujinni in forum Other Programming Languages
    Replies: 1
    Last Post: October 15th, 2011, 09:30 AM
  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