带2D阵列的气泡排序 [英] Bubble-Sort with 2D Array

查看:69
本文介绍了带2D阵列的气泡排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我确实知道如何为1维数组实现简单的冒泡排序.但是对于二维或多维,这就是我遇到的问题.

Howdy, I do know how to implement a simple bubble-sort for 1dimensional array. But with 2dimensional or multidimensional, that's where I have my problems.

到目前为止,我一直在使用它来对1Dimensional Arrays进行排序,其工作方式就像是一个超级按钮.但是大多数都是整数,而不是字符串:

So far I've been using this to sort 1Dimensional Arrays, works like a charm. But mostly with integer numbers, not strings:

boolean sort;

do{
    sort = true;

    for (int i = 0; i < testarray.length - 1; i++){
        if(testarray[i] > testarray[i+1]){
            temp = testarray[i];
            testarray[i] = testarray[i+1];
            testarray[i+1] = temp;           
            sort = false;
        }
    }

}while(!sort);

    // Descending Output
    // for (int k = testarray.length - 1; k >= 0 ; k--){

    // Ascending Output
    for (int k = 0; k < testarray.length ; k++){
        System.out.print(testarray[k] + ", ");
    }

假设我有:

客户编号,姓名,姓氏,地址

Customernumber, Name, Surname, Address

String customers[][] = {{"123", "John", "Doe", "Somewhere"}, {"007", "James", "Bond", "MI5"}, {"1337", "Lolcat", "Izgud", "Saturn"}}

现在,我想选择排序方式:客户编号,姓名,姓氏或地址.然后,我要根据自己的需要将其升序或降序输出.

Now, I want to choose to sort what to sort after: customernumber, name, surname or address. And after that I want to output it ascending or descending, depending what I want.

我只是不知道如何使用冒泡排序来实现这一点.我想保持气泡排序,而没有其他排序算法,我想学习在这种情况下气泡排序的工作原理.

I just have no idea how to implement this with bubble-sort. I want to stay in bubble-sort, , no other sorting algorithm, I want to learn how bubble-sort works in such a situation.

对于上升和下降,我的想法是:我可以执行if-Loop.例如if (asc == 1) then output ascending, else output descending.然后,例如将通过Console询问asc.

For ascending and descending my idea would be: I could do an if-Loop. For example if (asc == 1) then output ascending, else output descending. The asc would then be asked via Console for example.

非常感谢您的帮助.

推荐答案

二维数组基本上就是由数组组成的一维数组.

A 2 dimensional array is basically just a 1 dimensional array consisting of arrays.

仅使用与您拥有的相同的代码,而不是整数,而是移动内部数组.

Just use the same code you have, only instead of ints, move the inner arrays.

要知道一个数组是否比另一个数组大",请比较正确的数组成员的字符串值(即名称或姓氏,..).为此,您可以使用String CompareTo方法.

To know if one array is 'bigger' than the next, compare the string values of the correct array member (so the name or surname,..). To do this you can use the String CompareTo method.

最后一点:如果内部数组实际上是一个包含信息的对象,那么您给出的示例会更好.这样,您可以为所有字段使用单独的数据类型,而不必全部使它们成为字符串.

Last note: The example you gave is better if the inner array is actually an Object containing the info. This way you can have separate Datatypes for all the fields instead of all making them Strings.

例如:

  class Person
  {
       int customerNumber;
       String name;
       String surName;
       String address;
  };

实际回答您的问题:

如下更改程序:

更改临时声明:

 String [] temp;

并更改行:

 if(testarray[i] > testarray[i+1])

进入:

 if(testarray[i][1] > testarray[i+1][1])

它会起作用并按名称排序

than it 'll work and sort on the name

R

这篇关于带2D阵列的气泡排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆