关于选择排序的方法
我的类程序:
package com.sodi.test;
public class Ssort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []intArray={12,11,45,6,8,43,40,57,3,20};
int keyValue;
int index;
int temp;
System.out.println("排序前的数组");
for (int i = 0; i < intArray.length; i++)
System.out.println(intArray[i]+"");
System.out.println();
for (int i = 0; i < intArray.length; i++) {
index = i;
keyValue = intArray[i];
for (int j = i; j < intArray.length; j++) {
if (intArray[j]< keyValue) {
index = j;
keyValue=intArray[j];
}
temp =intArray[i];
intArray[i]=intArray[index];
intArray[index]=temp;
}
}
System.out.println("排序后的数组");
for (int i = 0; i < intArray.length; i++)
System.out.println(intArray[i]+" ");
}
}
运行后:数组排序不对,请问有什么问题吗?
排序前的数组
12
11
45
6
8
43
40
57
3
20
排序后的数组
6
11
12
45
8
43
40
57
3
20
------解决方案--------------------[code=Java][/code] public static void main(String[] args) {
int[] intArray = { 12, 11, 45, 6, 8, 43, 40, 57, 3, 20 };
int keyValue;
int index;
int temp;
System.out.println("排序前的数组");
for (int i = 0; i < intArray.length; i++)
System.out.print(intArray[i] + " ");
System.out.println();
for (int i = 0; i < intArray.length; i++) {
index = i;
keyValue = intArray[i];
for (int j = i; j < intArray.length; j++) {
if (intArray[j]< keyValue) {
index = j;
keyValue = intArray[j];
}
}
temp = intArray[index];
intArray[index] = intArray[i];
intArray[i] = temp;
}
System.out.println("排序后的数组");
for (int i = 0; i < intArray.length; i++)
System.out.print(intArray[i] + " ");
}
------解决方案--------------------for (int i = 0; i < intArray.length; i++) {
index = i;
keyValue = intArray[i];
for (int j = i; j < intArray.length; j++) {
if (intArray[j]< keyValue) {
index = j;
keyValue = intArray[j];
}
}
temp = intArray[index];
intArray[index] = intArray[i];
intArray[i] = temp;
}
这段循环中,循环j的大括号放错地方了~
------解决方案--------------------Java code
int [] intArray={12,11,45,6,8,43,40,57,3,20};
Arrays.sort(intArray);
System.out.println(Arrays.toString(intArray));
------解决方案--------------------
http://javagp.group.iteye.com/group/topic/12478
参考下
------解决方案--------------------
真是无语啊,你在J循环里面干嘛要用到I,这样子你每次都是跟固定的值在比较了,这明显是错误的啊。
/**
* 排序
*/
public class Compositor
{
private static int[] ints = {12, 11, 45, 6, 8, 43, 40, 57, 3, 20};
private static int param;
/**
* main函数
* @param args
*/
public static void main(String[] args)
{
for (int i = 0; i < ints.length; i++)
{
for (int j = 0; j < ints.length; j++)
{
if (j != ints.length - 1 && ints[j] > ints[j + 1])
{