二维数组的冒泡排序
你好,我知道如何为一维数组实现简单的冒泡排序。但对于二维或多维,这就是我遇到的问题。
到目前为止,我一直在使用它来对一维数组进行排序,效果非常好。但主要是整数,而不是字符串:
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] + ", ");
}
假设我有:
客户编号、姓名、姓氏、地址
String customers[][] = {{"123", "John", "Doe", "Somewhere"}, {"007", "James", "Bond", "MI5"}, {"1337", "Lolcat", "Izgud", "Saturn"}}
现在,我想选择对要排序的内容进行排序:客户编号、姓名、姓氏或地址。之后我想按升序或降序输出它,具体取决于我想要的。
我只是不知道如何用冒泡排序来实现这一点。我想留在冒泡排序,没有其他排序算法,我想了解冒泡排序在这种情况下如何工作。
对于上升和下降,我的想法是:我可以做一个 if 循环。例如if (asc == 1) then 输出升序,否则输出降序
。例如,然后将通过控制台询问 asc。
非常感谢任何帮助。
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.
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] + ", ");
}
Assuming I have:
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.
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.
Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
二维数组基本上只是由数组组成的一维数组。
只需使用相同的代码,只是移动内部数组而不是整数。
要了解一个数组是否比下一个数组“大”,请比较正确数组成员的字符串值(例如姓名或姓氏……)。为此,您可以使用 String CompareTo 方法。
最后注意:如果内部数组实际上是包含信息的对象,那么您给出的示例会更好。这样,您可以为所有字段设置单独的数据类型,而不是全部将其设为字符串。
例如:
编辑:要实际回答您的问题:
按以下方式更改您的程序:
更改临时声明:
并将行:更改
为:
比它可以工作并按名称
R排序
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.
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.
e.g.:
EDIT: to actually answer your question:
change your program as following:
change the temp declaration:
and change the line:
into:
than it 'll work and sort on the name
R
在二维数组中,所包含对象的类型从
int
或Integer
更改为String[]
(注意:这是一个字符串数组) 。这就是您需要将temp
的类型更改为的内容。最大的变化将是你的比较。您不能只使用
<
比较两个字符串数组 - 但您已经知道这一点。您需要做的是自己构建一个方法,该方法接受两个 String[] 参数并返回负数、0 或正数,具体取决于第一个数是否小于/等于/大于第二个。然后,您可以对该方法的结果进行<
/>
比较来确定排序顺序。如果您希望能够使用几种不同的排序标准,则需要通过(例如)传递另一个参数来告诉它如何工作来使比较函数更加通用,或者您将需要几种不同的比较函数,并使用
if
或switch
来决定在排序过程中使用哪一个。对于手动比较两个String数组,基本方法是:使用
String.compareTo()
比较第一个关键字符串。如果结果不为 0,则返回该值。如果为0,则第一个键相等,需要比较下一个键。如果你用完键并且仍然为 0,则两个元素的键相等,并且返回 0。In a 2D array, the type of your contained objects changes from
int
orInteger
toString[]
(note: that's an array of Strings). This is what you'll need to change the type oftemp
to.The biggest change will be to your comparison. You can't just compare two String arrays using
<
– but you already knew this. What you need to do is build yourself a method that takes twoString[]
arguments and returns a negative, 0 or positive number depending on whether the first is smaller/equal/larger than the second. You can then do a<
/>
comparison on the result from that method to establish your sort order.If you want to be able to use several different sort criteria, you'll either need to make the comparison function more versatile by (e.g.) passing in another parameter to tell it how to work, or you'll need several different comparison functions, and use an
if
orswitch
to decide which one to use during the sort.As for manually comparing two String arrays, the basic method is: Compare the first key strings using
String.compareTo()
. If the result is not 0, return that. If it is 0, then the first keys are equal and you need to compare the next keys. If you run out of keys and are still at 0, your two elements are equal on their keys and you return the 0.您可以保留相同的排序算法。它仍然不知道二维数组。您必须想出一个比较函数,该函数接受两个一维数组并判断哪一个更大。
You can keep the same sorting algorithm. It will still be unaware of the 2D array. You have to come up with a comparison function that takes two 1D arrays and says which one is the greater.
我猜您希望将“123”、“john”、“doe”和“某处”分组在一起。
我建议您使用一个对象,例如
添加常用的 getter 和 setter。
您可以拥有一个普通的 Person 对象数组,并使用冒泡排序算法对它们进行排序。您可以创建多个自定义比较器,将 ID、姓名、姓氏或地址与冒泡排序算法进行比较。
排序方法的签名应该类似于
I'm guessing you want "123", "john", "doe" and "somewhere" to be grouped together.
I suggest you use an object, say
adding the usual getters and setters.
you can have an ordinary array of Person objects, sorting them with your bubble sort algorithm. You can create several custom comparators that compare either id, name, surname or address to you bubble sort algo.
The signature of the sort method should be something like