冒泡 [英] Bubblesort

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

问题描述

你好,


我遇到程序问题。这似乎很容易,但由于某种原因,我只是没有得到它。我需要创建一个随机数组并调用我自己创建的bubblesort方法。然后我需要打印排序的数组。我创建了随机数组但我似乎无法弄清楚排序方法,它需要按降序排列。这就是我所拥有的:

hello,

I am having trouble with a program. It seems easy, but for some reason I am just not getting it. I need to create an array of random numbers and call a bubblesort method that I create myself. I then need to print the sorted array. I created the random array but I cant seem to figure out the sort method, which needs to follow in descending order. This is what I have:

展开 | 选择 | Wrap | 行号

推荐答案

你有两个问题:


1)你正在尝试打印一个数组并得到这个:[I @ 1eed786;这是'Java'打印数组的方式:$ = $ array,I == int,1ee786 ==数组的哈希码。如果你想要打印数组的内容,请阅读Arrays类的API文档。


2)你的冒泡排序算法就像瑞典的Nilfisk真空吸尘器:s * cks

很多时候:它通过制作阵列的副本开始很好但是你应该使用* b $ b只使用*副本进行比较和互换。


亲切的问候,


乔斯
You have two problems:

1) You are trying to print an array and get this: [I@1eed786; that''s Java''s way of
printing arrays: [ == array, I == int ,1ee786 == the hash code of the array. If you
want to print the contents of an array read the API documentation of the Arrays class.

2) Your bubble sort algorithm is like a Swedish Nilfisk vacuum cleaner: it s*cks
big times: it starts off nice by making a copy of the array but then you should
use *only* the copy for the comparisons and swaps.

kind regards,

Jos


如果你张贴洞,我会更喜欢代码,所以我可以测试它。

i我不确定,但我认为排序算法有一些东西(不确定)。 i循环很好但是使用j循环为什么不从数组的最后一个索引开始并向后移动。它会更好。

请发布孔码,以便我可以测试一下。


P.S.请使用代码标签


hsn
i woul prefer if you post the hole code so i can test it.
i am not sure, but i think there is something with the sort algorithm (not sure). the i loop is good but with the j loop why dont you start with the last index of the array and go backward. it would be ;better.
please post the hole code so i can test it.

P.S. PLEASE USE THE CODE TAGS

hsn


提示节省时间。在i循环和j循环中,每次循环时都调用数组的长度。最好将大小存储在变量中并在循环中使用变量。你不会感觉到区别。但相信我这很重要。


祝你好运


hsn
a tip to save time. in the i loop and the j loop you are calling the length of the array in every time it loops. it is better to store the size in a variable and use the variable in the loop. you wont feel the difference. but trust me it matters.

good luck

hsn


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

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