Topic: 数组排序问题 ?

  Print this page

1.数组排序问题 ? Copy to clipboard
Posted by: heartache
Posted on: 2006-03-22 20:40

我的代码 (用记事本编辑的):
public class sort
{
public static void main(String[] args)
{
int[] a={12,8,47,51,91,82,100,84,72,19};
System.out.println(java.util.Arrays.sort(a[]));
}
}
运行报错, 请问是怎么回事 ? : { Please don't use BMP, 100KB BMP --> 3KB GIF --why }

2.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: zcjl
Posted on: 2006-03-23 00:47

import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

public class TestSort {

public static void main(String[] args) {
int[] a = { 12, 8, 47, 51, 91, 82, 100, 84, 72, 19 };
System.out.println(ArrayUtils.toString(a));
Arrays.sort(a);
System.out.println(ArrayUtils.toString(a));
}

}

代码需要jakarta-commons-lang-2.0.jar

3.Re:数组排序问题 ? 新问题来了! [Re: heartache] Copy to clipboard
Posted by: heartache
Posted on: 2006-03-23 09:36

结果又有更多错误!

{ converted 422KB BMP to this 6KB GIF with the simple Paint program; unnecessary stuff at the bottom right had been removed.

Actually, in this case, you should just cut-n-paste the contents from the Command Prompt window, instead of using an image -- why }


4.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: heartache
Posted on: 2006-03-23 09:40

jakarta-commons-lang-2.0.jar
 JDK里面好象没有看到这个东西呢 ?

5.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: jackyangf
Posted on: 2006-03-23 10:11

........ sort 没有返回值, 你怎么print ?
用一个方法最少的看看说明吧?

6.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: zcjl
Posted on: 2006-03-23 11:13

heartache wrote:
jakarta-commons-lang-2.0.jar
 JDK里面好象没有看到这个东西呢 ?

JDK里面本来就没有啊
不过你google一下就会找到了
或者去这里:
http://www.apache.org/dist/jakarta/commons/lang/binaries/

7.Re:数组排序问题 ? [Re: zcjl] Copy to clipboard
Posted by: why
Posted on: 2006-03-23 18:30

zcjl wrote:
import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

public class TestSort {

public static void main(String[] args) {
int[] a = { 12, 8, 47, 51, 91, 82, 100, 84, 72, 19 };
System.out.println(ArrayUtils.toString(a));
Arrays.sort(a);
System.out.println(ArrayUtils.toString(a));
}

}

代码需要jakarta-commons-lang-2.0.jar

heartache: as you may noticed, ArrayUtils.toString(a) is used for printing the array.
java.util.Arrays.sort(a[])) is wrong, should be java.util.Arrays.sort(a)), as in zcjl's code.

8.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: phoenix451
Posted on: 2006-03-24 08:45

出错的原因是java.util.Arrays.sort(a)返回的是ivoid类型数据,
把程序改成如下就可以了:
public class sort
{
public static void main(String[] args)
{
int[] a={12,8,47,51,91,82,100,84,72,19};
java.util.Arrays.sort(a);
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}


check "Disable Smileys"

9.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: phoenix451
Posted on: 2006-03-24 09:04

不知道怎么回事,()内部加a全部变成了一个脸,
出错的原因是java.util.Arrays.sort(a[])) , 它应该是java.util.Arrays.sortAngel

10.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: phoenix451
Posted on: 2006-03-24 09:05

Post is deleted

11.Re:数组排序问题 ? [Re: phoenix451] Copy to clipboard
Posted by: zcjl
Posted on: 2006-03-24 09:05

phoenix451 wrote:
java.util.Arrays.sort(a))


12.Re:数组排序问题 ? [Re: heartache] Copy to clipboard
Posted by: suntao19830709
Posted on: 2006-03-24 09:57

给你一个我写的快速排序的算法

/**
* 快速排序
* @param num
* @param begin
* @param end
*/
public void doQuickOrder(int[] num, int begin, int end) {
// 计算传入的无序列的长度
int length = end + 1 - begin;
if (length > 1) {
// 设置无序列的第一个元素为中间参考元素
int middle = num[begin];

// ----将最终结果按照 小序列+中间元素+大序列(包括等于) 的顺序合并begin//
int minNum = 0;
int temp = 0;
// 将小于中间元素的元素往靠左的位置排,中间元素位置不变
for (int i = begin + 1; i < end + 1; i++) {
if (num[i] < middle) {
minNum++;
if (i != begin + minNum) {
temp = num[begin + minNum];
num[begin + minNum] = num[i];
num[i] = temp;
}
}
}
// 交换中间元素位置
if (minNum != 0) {
num[begin] = num[begin + minNum];
num[begin + minNum] = middle;
}
// ----将最终结果按照 小序列+中间元素+大序列(包括等于) 的顺序合并end//

// ----对小序列和大序列分别递归执行快速排序begin//
doQuickOrder(num, begin, begin + minNum - 1);
doQuickOrder(num, begin + minNum + 1, end);
// ----对小序列和大序列分别递归执行快速排序end//
}
}

/**
* 快速排序
* @param num
*/
public void doQuickOrder(int[] num) {
doQuickOrder(num, 0, num.length - 1);
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923