马克社区吧 关注:458贴子:423
  • 0回复贴,共1

java中的排序除了冒泡以来, 再给出一种方法, 举例说明

只看楼主收藏回复

9.5 排序: 有一种排序的方法,非常好理解,详见本题的步骤,先找出最大值和最小值,把最小值打印出来后,把它存在另一个数组b当中,再删除此最小值,之后再来一次找出最小值,打印出最小值以后,再把它存在另一个数组b当中,再删除此最小值,这样循环往复,直到做完,你就会发觉,你已经把排了序数放在b数组当中了,而这里的彻底删除最小值的方法就是用比最大值还大一的数来取代最小值。(自己想想为什么?)参考后面的答案你会发觉,按照下面的四步,你已经把一个数组排序了。 i)make a method called getMin to find the minimal value of the array. ii)make a method called getMax to find the maximum value of the array. iii) replace the minimal value with the maximum+1. iiii) sort an array.
public class Test {
static int minPosition=0;//用这个全局变量来记录最小数的位置索引,
public static void main(String[] args) {
int[] a = {6, 12, 7, 23, 4};
int max = getMax(a);
int[] b = new int[a.length];
for (int j = 0; j < a.length; j++) {
int min = getMin(a);
/*把a数组当中最小的值,马克-to-win给替换成max+1,这样就相当于把原来的最小值从这个数组当中彻底清除掉了。整个循环做完,a数组就彻底废了。*/
a[minPosition] = max + 1;
。。。。。。。。。。。。。。。。。。。。。。。
详情请见: http://www.mark-to-win.com/JavaBeginner/JavaBeginner1_web.html#9.5


IP属地:北京1楼2014-06-18 11:41回复