二维阵列冒泡排序 [英] 2-dimensional array bubble sort

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

问题描述

嘿,我正在研究一个涉及使用冒泡排序对二维整数数组进行排序的项目。我已经半工作了,我唯一的问题是它似乎正在失去价值,我假设当它到达阵列中一条线的末尾时。它还必须运行几次才能获得完全正确的值。

这是我到目前为止所得到的:

Hey, I''m working on a project that involves sorting a two-dimensional array of integers using bubble sort. I''ve got it semi-working, my only issue is that it seems to be ''losing'' values, I''m assuming when it gets to the end of a line in the array. It also has to run several times to get the values it keeps in completely correct order.
Here''s what I''ve got so far:

展开 | 选择 | Wrap | 行号

推荐答案

我不确定是否对2D数组进行排序,但我认为你必须重新考虑你的算法。据我所知,这个算法查看数组中的每个2x2''块''并将最小值移动到左上角 - 但这不一定是整个数组中的最小值。您应该搜索整个2D数组以找到最小值,并使用第一个元素切换,然后继续下一个。


此外,a和b对应于你的阵列的大小?如果你有一个阵列10x10,是a和b 10还是9?如果它们是10,你将使用数组[10] [10](第11行,第11列)访问数组的边界区域。如果它们是9,你应该没问题。
I''m not sure about sorting 2D arrays, but I think you''ll have to reconsider your algorithm. As far as I can tell, this algorithm looks at every 2x2 ''block'' in the array and moves the smallest value to the top left - but this isn''t necessarily the smallest value in the entire array. You should be searching through the entire 2D array to find the smallest value, and switch it with the first element, then continue with the next.

Also, are a and b corresponding to the size of your array? If you have an array 10x10, are a and b 10 or 9? If they are 10, you will be accessing out of bounds areas of your array with array[10][10] (the 11th row, 11th column). If they are 9, you should be fine.


你能否用psudocode或单词描述你的算法应该如何工作。请告诉我你的前后条件是什么。比如,你会接受3x4或8x5的输入吗?您的输出将如何排序?
Can you (in psudocode or words) describe how your algorithm is supposed to work. And please let me know what your pre and post conditions are. Like, will you accept input that is 3x4 or 8x5? How will your output be sorted?


您应该将您的代码与此处。你可能想重新考虑使用buble sort。 :)


另外,当你说2D数组排序时,你的意思是什么?将最小的数字放在按行排序的第0行中,直到它被填充,然后将下一个最小的数字放入第1行,依此类推?如果是这样,那就没有意义了,只需制作一维数组,对其进行排序然后像二维数组一样进行访问。


希望这会有所帮助。


Adrian
You should compare your code against that in here. And you may want to reconsider using buble sort. :)

Also, when you say sort a 2D array, what do you mean? That the smallest numbers are put in the 0th row sorted by column until it is filled and then put the next smallest into the 1st row and so on? If so, there''s no point, just make a 1D array, sort that and then access that like a 2D array.

Hope this helps.


Adrian


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

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