一个简单的java数字排序问题!
排序有点问题!!!谁能指点下哪里有问题???//SortAlgorithm.javapublicclassSortAlgorithm{privatevoidbubbleS...
排序有点问题!!!谁能指点下哪里有问题???
//SortAlgorithm.java
public class SortAlgorithm {
private void bubbleSort(int[] numlist){
int out,in;
for(out=numlist.length-1;out>1;out--)
{for(in=0;in<out;in++)
if(numlist[in]>numlist[in+1]){
int temp=numlist[in];
numlist[in]=numlist[out];
numlist[in+1]=temp;
}
}
}
private void insertSort(int[] numlist){
int in,out;
for(out=1;out<numlist.length;out++){
int temp=numlist[out];
in=out;
while(in>0&&numlist[in-1]>=temp)
{numlist[in]=numlist[in-1];
--in;
}
numlist[in]=temp;
}
}
private void selectionSort(int[] numlist){
int in,out,min;
for(out=0;out<numlist.length-1;out++){
min=out;
for(in=out+1;in<numlist.length;in++)
{if(numlist[in]<numlist[min])
min=in;
int temp=numlist[out];
numlist[out]=numlist[min];
numlist[min]=temp;
}
}
}
private void display(int[] numlist){
for(int j=0;j<numlist.length;j++)
System.out.print(numlist[j]+" ");
System.out.println("");
}
public static void main(String[] args){
SortAlgorithm sortAlgorithm=new SortAlgorithm();
int numlist[]=new int[]{1,3,2,9,8,10,21,5};
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
long begin=System.currentTimeMillis();
sortAlgorithm.bubbleSort(numlist);
long end=System.currentTimeMillis();
System.out.println("冒泡排序用时为:"+(end-begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.currentTimeMillis();
sortAlgorithm.insertSort(numlist);
end=System.currentTimeMillis();
System.out.println("插入排序用时为:"+(end-begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.currentTimeMillis();
sortAlgorithm.selectionSort(numlist);
end=System.currentTimeMillis();
System.out.println("选择排序用时为:"+(end-begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
} 展开
//SortAlgorithm.java
public class SortAlgorithm {
private void bubbleSort(int[] numlist){
int out,in;
for(out=numlist.length-1;out>1;out--)
{for(in=0;in<out;in++)
if(numlist[in]>numlist[in+1]){
int temp=numlist[in];
numlist[in]=numlist[out];
numlist[in+1]=temp;
}
}
}
private void insertSort(int[] numlist){
int in,out;
for(out=1;out<numlist.length;out++){
int temp=numlist[out];
in=out;
while(in>0&&numlist[in-1]>=temp)
{numlist[in]=numlist[in-1];
--in;
}
numlist[in]=temp;
}
}
private void selectionSort(int[] numlist){
int in,out,min;
for(out=0;out<numlist.length-1;out++){
min=out;
for(in=out+1;in<numlist.length;in++)
{if(numlist[in]<numlist[min])
min=in;
int temp=numlist[out];
numlist[out]=numlist[min];
numlist[min]=temp;
}
}
}
private void display(int[] numlist){
for(int j=0;j<numlist.length;j++)
System.out.print(numlist[j]+" ");
System.out.println("");
}
public static void main(String[] args){
SortAlgorithm sortAlgorithm=new SortAlgorithm();
int numlist[]=new int[]{1,3,2,9,8,10,21,5};
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
long begin=System.currentTimeMillis();
sortAlgorithm.bubbleSort(numlist);
long end=System.currentTimeMillis();
System.out.println("冒泡排序用时为:"+(end-begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.currentTimeMillis();
sortAlgorithm.insertSort(numlist);
end=System.currentTimeMillis();
System.out.println("插入排序用时为:"+(end-begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.currentTimeMillis();
sortAlgorithm.selectionSort(numlist);
end=System.currentTimeMillis();
System.out.println("选择排序用时为:"+(end-begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
} 展开
2个回答
展开全部
应该好了吧。。
你冒泡写错了,数组改了,把后面的都带错了
public class SortAlgorithm {
private void bubbleSort(int[] numlist) {
int out, in;
for (out = 0; out < numlist.length-1; out++) {
for (in = numlist.length-1; in >= out+1; in--)
if (numlist[in] < numlist[in-1]) {
int temp = numlist[in-1];
numlist[in-1] = numlist[in];
numlist[in] = temp;
}
}
}
private void insertSort(int[] numlist) {
int in, out;
for (out = 1; out < numlist.length; out++) {
int temp = numlist[out];
in = out;
while (in > 0 && numlist[in - 1] >= temp) {
numlist[in] = numlist[in - 1];
--in;
}
numlist[in] = temp;
}
}
private void selectionSort(int[] numlist) {
int in, out, min;
for (out = 0; out < numlist.length - 1; out++) {
min = out;
for (in = out + 1; in < numlist.length; in++) {
if (numlist[in] < numlist[min])
min = in;
int temp = numlist[out];
numlist[out] = numlist[min];
numlist[min] = temp;
}
}
}
private void display(int[] numlist) {
for (int j = 0; j < numlist.length; j++)
System.out.print(numlist[j] + " ");
System.out.println("");
}
public static void main(String[] args) {
SortAlgorithm sortAlgorithm = new SortAlgorithm();
int numlist[] = new int[] { 1, 3, 2, 9, 8, 10, 21, 5 };
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
long begin = System.currentTimeMillis();
sortAlgorithm.bubbleSort(numlist);
long end = System.currentTimeMillis();
System.out.println("冒泡排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.insertSort(numlist);
end = System.currentTimeMillis();
System.out.println("插入排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.selectionSort(numlist);
end = System.currentTimeMillis();
System.out.println("选择排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
}public class SortAlgorithm {
private void bubbleSort(int[] numlist) {
int out, in;
for (out = 0; out < numlist.length-1; out++) {
for (in = numlist.length-1; in >= out+1; in--)
if (numlist[in] < numlist[in-1]) {
int temp = numlist[in-1];
numlist[in-1] = numlist[in];
numlist[in] = temp;
}
}
}
private void insertSort(int[] numlist) {
int in, out;
for (out = 1; out < numlist.length; out++) {
int temp = numlist[out];
in = out;
while (in > 0 && numlist[in - 1] >= temp) {
numlist[in] = numlist[in - 1];
--in;
}
numlist[in] = temp;
}
}
private void selectionSort(int[] numlist) {
int in, out, min;
for (out = 0; out < numlist.length - 1; out++) {
min = out;
for (in = out + 1; in < numlist.length; in++) {
if (numlist[in] < numlist[min])
min = in;
int temp = numlist[out];
numlist[out] = numlist[min];
numlist[min] = temp;
}
}
}
private void display(int[] numlist) {
for (int j = 0; j < numlist.length; j++)
System.out.print(numlist[j] + " ");
System.out.println("");
}
public static void main(String[] args) {
SortAlgorithm sortAlgorithm = new SortAlgorithm();
int numlist[] = new int[] { 1, 3, 2, 9, 8, 10, 21, 5 };
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
long begin = System.currentTimeMillis();
sortAlgorithm.bubbleSort(numlist);
long end = System.currentTimeMillis();
System.out.println("冒泡排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.insertSort(numlist);
end = System.currentTimeMillis();
System.out.println("插入排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.selectionSort(numlist);
end = System.currentTimeMillis();
System.out.println("选择排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
}
你冒泡写错了,数组改了,把后面的都带错了
public class SortAlgorithm {
private void bubbleSort(int[] numlist) {
int out, in;
for (out = 0; out < numlist.length-1; out++) {
for (in = numlist.length-1; in >= out+1; in--)
if (numlist[in] < numlist[in-1]) {
int temp = numlist[in-1];
numlist[in-1] = numlist[in];
numlist[in] = temp;
}
}
}
private void insertSort(int[] numlist) {
int in, out;
for (out = 1; out < numlist.length; out++) {
int temp = numlist[out];
in = out;
while (in > 0 && numlist[in - 1] >= temp) {
numlist[in] = numlist[in - 1];
--in;
}
numlist[in] = temp;
}
}
private void selectionSort(int[] numlist) {
int in, out, min;
for (out = 0; out < numlist.length - 1; out++) {
min = out;
for (in = out + 1; in < numlist.length; in++) {
if (numlist[in] < numlist[min])
min = in;
int temp = numlist[out];
numlist[out] = numlist[min];
numlist[min] = temp;
}
}
}
private void display(int[] numlist) {
for (int j = 0; j < numlist.length; j++)
System.out.print(numlist[j] + " ");
System.out.println("");
}
public static void main(String[] args) {
SortAlgorithm sortAlgorithm = new SortAlgorithm();
int numlist[] = new int[] { 1, 3, 2, 9, 8, 10, 21, 5 };
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
long begin = System.currentTimeMillis();
sortAlgorithm.bubbleSort(numlist);
long end = System.currentTimeMillis();
System.out.println("冒泡排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.insertSort(numlist);
end = System.currentTimeMillis();
System.out.println("插入排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.selectionSort(numlist);
end = System.currentTimeMillis();
System.out.println("选择排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
}public class SortAlgorithm {
private void bubbleSort(int[] numlist) {
int out, in;
for (out = 0; out < numlist.length-1; out++) {
for (in = numlist.length-1; in >= out+1; in--)
if (numlist[in] < numlist[in-1]) {
int temp = numlist[in-1];
numlist[in-1] = numlist[in];
numlist[in] = temp;
}
}
}
private void insertSort(int[] numlist) {
int in, out;
for (out = 1; out < numlist.length; out++) {
int temp = numlist[out];
in = out;
while (in > 0 && numlist[in - 1] >= temp) {
numlist[in] = numlist[in - 1];
--in;
}
numlist[in] = temp;
}
}
private void selectionSort(int[] numlist) {
int in, out, min;
for (out = 0; out < numlist.length - 1; out++) {
min = out;
for (in = out + 1; in < numlist.length; in++) {
if (numlist[in] < numlist[min])
min = in;
int temp = numlist[out];
numlist[out] = numlist[min];
numlist[min] = temp;
}
}
}
private void display(int[] numlist) {
for (int j = 0; j < numlist.length; j++)
System.out.print(numlist[j] + " ");
System.out.println("");
}
public static void main(String[] args) {
SortAlgorithm sortAlgorithm = new SortAlgorithm();
int numlist[] = new int[] { 1, 3, 2, 9, 8, 10, 21, 5 };
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
long begin = System.currentTimeMillis();
sortAlgorithm.bubbleSort(numlist);
long end = System.currentTimeMillis();
System.out.println("冒泡排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.insertSort(numlist);
end = System.currentTimeMillis();
System.out.println("插入排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin = System.currentTimeMillis();
sortAlgorithm.selectionSort(numlist);
end = System.currentTimeMillis();
System.out.println("选择排序用时为:" + (end - begin));
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*冒泡有总是,而且时间获得精度不够,都帮你改了*/
public class SortAlgorithm
{
private void bubbleSort(int[] numlist)
{
int out,in;
for(out=numlist.length-1;out>0;out--)
{
for(in=0;in<out;in++)
{
if(numlist[in]>numlist[in+1])
{
int temp=numlist[in];
numlist[in]=numlist[in+1]; //这儿写错了
numlist[in+1]=temp;
}
}
}
}
private void insertSort(int[] numlist)
{
int in,out;
for(out=1;out<numlist.length;out++)
{
int temp=numlist[out];
in=out;
while(in>0&&numlist[in-1]>=temp)
{
numlist[in]=numlist[in-1];
--in;
}
numlist[in]=temp;
}
}
private void selectionSort(int[] numlist)
{
int in,out,min;
for(out=0;out<numlist.length-1;out++)
{
min=out;
for(in=out+1;in<numlist.length;in++)
{
if(numlist[in]<numlist[min])
min=in;
}
int temp=numlist[out];
numlist[out]=numlist[min];
numlist[min]=temp;
}
}
private void display(int[] numlist)
{
for(int j=0;j<numlist.length;j++)
System.out.print(numlist[j]+" ");
System.out.println("");
}
public static void main(String[] args)
{
SortAlgorithm sortAlgorithm=new SortAlgorithm();
int numlist[]=new int[]{1,3,2,9,8,10,21,5};
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
//long begin=System.currentTimeMillis();
long begin=System.nanoTime();
System.out.println(begin);
sortAlgorithm.bubbleSort(numlist);
//long end=System.currentTimeMillis();
long end=System.nanoTime();
System.out.println(end);
System.out.println("冒泡排序用时为:"+(end-begin) + "毫秒");
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.nanoTime();
sortAlgorithm.insertSort(numlist);
end=System.nanoTime();
System.out.println("插入排序用时为:"+(end-begin) + "毫秒");
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.nanoTime();
sortAlgorithm.selectionSort(numlist);
end=System.nanoTime();
System.out.println("选择排序用时为:"+(end-begin) + "毫秒");
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
}
public class SortAlgorithm
{
private void bubbleSort(int[] numlist)
{
int out,in;
for(out=numlist.length-1;out>0;out--)
{
for(in=0;in<out;in++)
{
if(numlist[in]>numlist[in+1])
{
int temp=numlist[in];
numlist[in]=numlist[in+1]; //这儿写错了
numlist[in+1]=temp;
}
}
}
}
private void insertSort(int[] numlist)
{
int in,out;
for(out=1;out<numlist.length;out++)
{
int temp=numlist[out];
in=out;
while(in>0&&numlist[in-1]>=temp)
{
numlist[in]=numlist[in-1];
--in;
}
numlist[in]=temp;
}
}
private void selectionSort(int[] numlist)
{
int in,out,min;
for(out=0;out<numlist.length-1;out++)
{
min=out;
for(in=out+1;in<numlist.length;in++)
{
if(numlist[in]<numlist[min])
min=in;
}
int temp=numlist[out];
numlist[out]=numlist[min];
numlist[min]=temp;
}
}
private void display(int[] numlist)
{
for(int j=0;j<numlist.length;j++)
System.out.print(numlist[j]+" ");
System.out.println("");
}
public static void main(String[] args)
{
SortAlgorithm sortAlgorithm=new SortAlgorithm();
int numlist[]=new int[]{1,3,2,9,8,10,21,5};
System.out.print("原来顺序为:");
sortAlgorithm.display(numlist);
System.out.println("");
//long begin=System.currentTimeMillis();
long begin=System.nanoTime();
System.out.println(begin);
sortAlgorithm.bubbleSort(numlist);
//long end=System.currentTimeMillis();
long end=System.nanoTime();
System.out.println(end);
System.out.println("冒泡排序用时为:"+(end-begin) + "毫秒");
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.nanoTime();
sortAlgorithm.insertSort(numlist);
end=System.nanoTime();
System.out.println("插入排序用时为:"+(end-begin) + "毫秒");
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
System.out.println("");
begin=System.nanoTime();
sortAlgorithm.selectionSort(numlist);
end=System.nanoTime();
System.out.println("选择排序用时为:"+(end-begin) + "毫秒");
System.out.print("排序之后:");
sortAlgorithm.display(numlist);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询