java中从最高数到最低数的冒泡排序

发布于 2024-09-26 08:04:46 字数 841 浏览 1 评论 0原文

我正在寻找java中的冒泡排序代码,它与我在互联网上搜索时看到的通常的东西相反。 我不太明白下面的代码,我只知道它对一堆数字从最低到最高进行排序。下面的代码是否可以修改,以便不从最低到最高输出数字。它输出它从最高到最低?

int i;
    int array[] = {12,9,4,99,120,1,3,10};
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
      System.out.print( array[i]+"  ");
    System.out.println();
    bubble_srt(array, array.length);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
    System.out.println();
    System.out.println("PAUSE");
  }

  public static void bubble_srt( int a[], int n ){
    int i, j,t=0;
    for(i = 0; i < n; i++){
      for(j = 1; j < (n-i); j++){
        if(a[j-1] > a[j]){
          t = a[j-1];
          a[j-1]=a[j];
          a[j]=t;
        }
      }
    }
  }

I'm looking for a bubblesort code in java that is opposite of the usual thing that I'm seeing when I search the internet.
I don't really understand the code below, all I know is that it sorts a bunch of numbers from lowest to highest. Is the code below modifiable so that instead of outputting the numbers from lowest to highest. It outputs it as highest to lowest?

int i;
    int array[] = {12,9,4,99,120,1,3,10};
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
      System.out.print( array[i]+"  ");
    System.out.println();
    bubble_srt(array, array.length);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
    System.out.println();
    System.out.println("PAUSE");
  }

  public static void bubble_srt( int a[], int n ){
    int i, j,t=0;
    for(i = 0; i < n; i++){
      for(j = 1; j < (n-i); j++){
        if(a[j-1] > a[j]){
          t = a[j-1];
          a[j-1]=a[j];
          a[j]=t;
        }
      }
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2024-10-03 08:04:46

更改

if(a[j-1] > a[j]){

if(a[j-1] < a[j]){

change

if(a[j-1] > a[j]){

to

if(a[j-1] < a[j]){
只是在用心讲痛 2024-10-03 08:04:46

关于代码的一些话:

如果将交换方法移出内部循环,它会变得更具可读性,并且更容易推理独立部分。

public void swap (int i, int j, int [] arr) {
    int tmp = arr [i];
    arr [i] = arr [j];
    arr [j] = tmp;
}

可爱的小方法很容易理解和测试,这一点很重要。

不要在 for 之外声明索引变量。这使得推理代码变得更加困难 - 变量在循环之外不需要就可见。在旧代码中,在内循环之外声明 tmp 不会带来任何好处。声明在运行时是免费的。

public static void bubbleSort (int a[], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n-i); j++) {
            if (a[j-1] > a[j]) {
                swap (j, j-1, a);
            }
        }
    }
}

    // ... missing ...

不要重复自己。将重复的代码移至方法中。

public static void show (int [] arr)
{
    for (int i : arr) 
        System.out.print (i + " ");
    System.out.println ();
}

可爱的小方法很容易测试。尽可能使用简化的 for 循环,以避免差一错误,并且对代码更改更加稳健 - 例如,它们也适用于列表。

    int array[] = {12, 9, 4, 99, 120, 1, 3, 10};
    System.out.println ("Values Before the sort:\n");
    show (array);
    bubbleSort (array, array.length);
    System.out.print ("Values after the sort:\n");
    show (array);
    System.out.println ("PAUSE");
}

通过简化的代码,可以更容易地推断出哪个部分的作用。

if (a[j-1] > a[j]) {

只需要更改

if (a[j-1] < a[j]) {

即可颠倒顺序。

Some words about your code:

If you move the swap-method out of your inner loop, it get's more readable, and more easy to reason about the independent parts.

public void swap (int i, int j, int [] arr) {
    int tmp = arr [i];
    arr [i] = arr [j];
    arr [j] = tmp;
}

Sweet little methods are easy to understand and test, which is important.

Don't declare the index variables outside the for. This makes it harder to reason about your code - the variables are visible without necessity outside the loop. In the old code, you gain nothing from declaring tmp outside of the inner loop. Declaration is cost-free at runtime.

public static void bubbleSort (int a[], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n-i); j++) {
            if (a[j-1] > a[j]) {
                swap (j, j-1, a);
            }
        }
    }
}

    // ... missing ...

Don't repeat yourself. Move duplicated code into a method.

public static void show (int [] arr)
{
    for (int i : arr) 
        System.out.print (i + " ");
    System.out.println ();
}

Sweet little methods are easy to test. Use the simplified for-loop, whenever possible, to avoid off-by-one-errors, and to be more robust to code changes - they work for Lists too, for example.

    int array[] = {12, 9, 4, 99, 120, 1, 3, 10};
    System.out.println ("Values Before the sort:\n");
    show (array);
    bubbleSort (array, array.length);
    System.out.print ("Values after the sort:\n");
    show (array);
    System.out.println ("PAUSE");
}

With the simplified code, it get's more easy to reason about, what which part does.

if (a[j-1] > a[j]) {

needs just to be changed

if (a[j-1] < a[j]) {

to reverse the order.

空城之時有危險 2024-10-03 08:04:46

您可以更改冒泡排序以满足您的需求,也可以保留原样并向后遍历已排序的数组。对于两者,你应该尝试理解这么一小段代码,而不是简单地要求修改后的代码。

you could change the bubblesort to satisfy your needs or leave it as is and walk the sorted array backwards. for both, you should try to understand such a little piece of code instead of simply asking for the modified code.

寂寞花火° 2024-10-03 08:04:46

for(i = array.length -1; i >=0; i--)
{
System.out.println(array[i]);
}

应该可以。你从数组的末尾开始并向后走

for(i = array.length -1; i >=0; i--)
{
System.out.println(array[i]);
}

Should work. You start at the end of the array and go backwards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文