# Bubble Sort Help

• December 24th, 2012, 02:32 PM
asundar
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:

Code :

``` 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(); } } }```

Code :

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

Code :

``` public void swap(int one,int two) { long temp=a[one]; a[one]=a[two]; a[two]=temp; }```
• December 24th, 2012, 02:42 PM
Norm
Re: Bubble Sort Help
Can you make a small complete program that compiles, executes and shows the problem?
• December 24th, 2012, 03:03 PM
asundar
Re: Bubble Sort Help
Sure thing:

Code :

```/* * 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:
Code :

```/* * 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:
Code :

```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```
• December 24th, 2012, 04:10 PM
asundar
Re: Bubble Sort Help
Quote:

Originally Posted by Norm
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.
• December 24th, 2012, 04:14 PM
Norm
Re: Bubble Sort Help