1加3如何设置软件密码:进行选择排序,哪里错了?急!!!!(java)

来源:百度文库 编辑:高考问答 时间:2024/04/30 14:25:24
import java.io.*;
public class zs
{ public static int Score[]=new int[10];
public static void main(String args[]) throws IOException
{ int i;
int Index;
String s;
System.out.println("请输入成绩 (Exit For 0)");
Index=0;
BufferedReader in=new BufferedReader
(new InputStreamReader(System.in));
do{
System.out.print("成绩 "+Index+" :");
s=in.readLine();
Score[Index]=Integer.parseInt(s);
Index++;
}
while(Score[Index-1]!=0);
System.out.println("");
System.out.print("Before Sorting:");
for(i=0;i<Index-1;i++)
System.out.print(" "+Score[i]+" " );
System.out.println("");
System.out.print("通过选择排序:");
System.out.println("");
SelectSort(Index-1);
for(i=0;i<Index-1;i++)
System.out.print(" " +Score[i]+" ");
System.out.println("");
}
/*------------------选择排序--------------------*/
public static void SelectSort(int Index)
{
int i,j,k;
int MinValue;
int IndexMin;
int Temp;

for(i=0;i<Index-1;i++)
{
MinValue=9999;
IndexMin=0;

for(j=i;j<Index;j++)
{
if(Score[j]<MinValue)
{
MinValue=Score[j];
IndexMin=j;
}
Temp=Score[i];
Score[i]=Score[IndexMin];
Score[IndexMin]=Temp;
}
System.out.print("Current Sorting result: ");
for (k=0;k<Index;k++)
System.out.print(" "+Score[k]+" ");
System.out.println("");
}
}
}
感觉没错,但是测试数据时就是不对
输入:80 92 60 73 76 43
排序后竟然是:43 60 73 92 76 80
Current Sorting result:43 92 80 73 76 60
43 60 92 73 76 80
43 60 73 92 76 80
43 60 73 92 76 80
43 60 73 92 76 80
哥哥姐姐们帮下忙,谢谢

算法是没问题的 也许是你的其他地方写不对

class ArraySel
{
private long [] a;
private int nElems;

public ArraySel(int max)
{
a = new long[max];
nElems = 0;
}

public void insert(long value)
{
a[nElems] = value;
nElems++;
}

public void display()
{
for(int j = 0; j < nElems; j++)
System.out.println (a[j] + " ");
System.out.println ("");
}

public void selectionSort()
{
int out, in, min;
for(out = 0; out < nElems - 1; out++)
{
min = out;
for(in = out + 1; in < nElems; in++)
{
if(a[in] < a[min])
min = in;
}
swap(out, min);
}
}

private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
}

class SelectSortApp
{
public static void main(String[] args)
{
int maxSize = 100;
ArraySel arr;
arr = new ArraySel(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(0);
arr.insert(66);
arr.insert(33);

arr.display();
arr.selectionSort();
arr.display();
}
}

//试试这个 以前跟着一书上做的练习