简体   繁体   English

您如何将气泡从大到小排序?

[英]How do you Bubble Sort Largest to Smallest

Sorry am kind of lame at this. 对不起,这有点me脚。 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 更新 -用v替换j

The problem is the inner loop should be from 1 to n. 问题是内部循环应为1到n。 Instead your inner loop stops early. 相反,您的内循环会提前停止。

Also you are testing i in the inner loop condition, but you should be testing v. 同样,您正在内循环条件下测试i,但是您应该测试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, 通常我会像这样实现Bubble排序,

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 . 看一下内部循环,您正在初始化v但检查并更改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. x这是您只需要执行此操作的临时变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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