如何使用冒泡排序对树形图进行排序?
27527-683
27525-1179
27525-1571
27525-1813
27525-4911
27526-1303
27526-3641
27525-3989
27525-4083
27525-4670
27526-4102
27526-558
27527-2411
27527-4342
这是键列表,在映射中将其声明为字符串
然后我想按升序对其进行排序。
如何在地图内使用冒泡排序方法?
其中键的值是一个列表。 为了得到:
27525-1179
27525-1571
27525-1813
27525-3989
27525-4083
27525-4670
27525-4911
27526-558
27526-1303
27526-3641
27526-4102
27527-683
27527-2411
27527-4342
27527-683
27525-1179
27525-1571
27525-1813
27525-4911
27526-1303
27526-3641
27525-3989
27525-4083
27525-4670
27526-4102
27526-558
27527-2411
27527-4342
this is the list of key where it is declared as string in a map
then i want to sort it in ascending order.
how can i use a bubble sorting method inside a map?
where the value of the key is a list.
in order to get :
27525-1179
27525-1571
27525-1813
27525-3989
27525-4083
27525-4670
27525-4911
27526-558
27526-1303
27526-3641
27526-4102
27527-683
27527-2411
27527-4342
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够在树上执行有序遍历。但是,如果你坚持这样做,那就是你会做的。
由于您没有指定语言,因此我提供伪代码。
You should be able to just perform an in-order traversal on your tree. Bu if you insist here is what you would do.
Since you don't specify a lnaguage, I present psuedocode.
一般来说,您只需使用与正常情况相同的冒泡排序算法,只是您的比较条件在这里进行了调整,以查看键和值来确定什么大于什么,即首先比较键,如果它们相等,则然后如果键不匹配,则比较值,然后使用值的差异来获得交换或不交换的结果。但如果您在现实场景中使用冒泡排序,那么它的效率会很差。
乔恩(Jon)在我之前收到了这篇文章,但基本上他写的内容看起来是正确的,除非你想要嵌套循环中的 if 的复杂条件,当然,
因为他还指出这些键/值的实际提取/使用方式将取决于问题或标签中缺少的语言。
In general you just use the same bubble sort algorithm as normal it's just your comparison condition that's tweaked here to look at both the key and the value to determine what is greater than what, that is compare the keys first and if they're equal then compare the values if the keys don't match then use the difference in the values to get your result of swap or don't swap. Bubble sort is bad efficiency-wise though if you're using this in a real world scenario.
Jon got the post in before me but basically what he wrote looks right except you'd want a complex condition for the if within the nested loop, like
of course as he also stated how these keys/values are actually extracted/used will depend on the language, which is lacking in the question or tags.