简体   繁体   中英

How do you Bubble Sort Largest to Smallest

Sorry am kind of lame at this. I've looked on here for a way to Bubble sort so that I can get an array to go from largest number to smallest. I've found some error in my current iteration of the sort, I can't seem to get the array to sort once it compares a smaller number to a bigger number. Here what I am using thus far.

//bubble sort
    for(int i=0;i<size;i++)
    {
        for(int v=1;i<(size-i);i++)
        {
            if(arrInt[v-1]<arrInt[v])
            {
                temp = arrInt[v-1];
                arrInt[v-1]=arrInt[v];
                arrInt[v]=temp;
            }
        }
    }
int n = arrInt.length;
int temp = 0;    
for (int i = 0; i < n; i++) {
   for (int v = 1; v < (n - i); v++) {
       if (arrInt[v - 1] < arrInt[v]) {
          temp = arrInt[v - 1];
          arrInt[v - 1] = arrInt[v];
          arrInt[v] = temp;
       }

   }
}  

Try this. Update - Replaced j with v

The problem is the inner loop should be from 1 to n. Instead your inner loop stops early.

Also you are testing i in the inner loop condition, but you should be testing v.

Try this:

//bubble sort
for(int i=0;i<size;i++)
{
    for(int v=1;v<size;v++)
    {
        if(arrInt[v-1]<arrInt[v])
        {
            temp = arrInt[v-1];
            arrInt[v-1]=arrInt[v];
            arrInt[v]=temp;
        }
    }
}

Bubble Sort Method for Descending Order

public static void BubbleSort( int[ ] arr){
    int records=arr.length-1;
    boolean notSorted= true;   // first pass
    while (notSorted) {
        notSorted= false;    //set flag to false awaiting a possible swap
        for( int count=0;  count < records;  count++ ) {
            if ( arr[count] < arr[count+1] ) {  // change to > for ascending sort
                arr[count]=arr[count]+arr[count+1];
                arr[count+1]=arr[count]-arr[count+1];
                arr[count]=arr[count]-arr[count+1];
                notSorted= true;     //Need to further check        
            }
        }
    }
} 

In this method when array is sorted then it does not check further.

Usually I implement Bubble sort like this,

for(int i=0;i<size-1;i++) {
    for(int v=0;v<(size-1-i);v++){
        if(arrInt[v]<arrInt[v+1])
        {
            temp = arrInt[v];
            arrInt[v]=arrInt[v+1];
            arrInt[v+1]=temp;
        }
    }
}

You know what is the problem is, in your code??? Look at the inner loop, you are initializing v but checking and changing i . Must be a copy paste error.. :P

Hope it helped...

Here you go:

int x = 0;

for(int i = 0; i < array.length; i++)
    for(int j = 0; j < array.length; j++)
        if(array[i] > array[j + 1])
            x = array[j + 1];
            array[j + 1]= array[i];
            array[i] = x;

x here is a temporary variable you only need for this operation.

here's a complete running program for you. Hope that keeps you motivated

package test;

public class BubbleSort {

private static int[] arr = new int[] { 1, 45, 65, 89, -98, 2, 75 };

public static void sortBubbleWay() {
    int size = arr.length-1;
    int temp = 0; // helps while swapping
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i; j++) {
            if (arr[j] < arr[j+1]) { /* For decreasing order use < */
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

private static void showShortedArray() {
    for (int elt : arr) {
        System.out.println(elt);
    }
}

public static void main(String args[]) {
    sortBubbleWay();
    showShortedArray();
}
}//end of class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM