2013-12-09 70 views
0

我有這個代碼,我不知道爲什麼選擇排序不排序一路有誰知道在哪裏修復程序。我相信選擇排序代碼是正確的,我只是不知道什麼是錯的。該代碼正在運行Java排序陣列

import java.util.Scanner; 

public class selectionSort 
{ 
    public static void main(String[] args) 
{ 
    Scanner scanner = new Scanner(System.in); 

    int temp; 

    int i,j,first; 



    System.out.println("How many numbers do you want to enter?"); 
    int ammount = scanner.nextInt(); 
    int[]array = new int[ammount]; 

    for (i = 0 ; i < array.length; i++) 
    { 

    System.out.println("Enter the numbers now."); 
    array[i] = scanner.nextInt(); 

    } 

    System.out.println("\nThe array is:"); 
    for(i = 0; i < array.length; i++){ 
    System.out.print(array[i] + " "); 

    } 




    for (i=array.length - 1; i>0;i--) 
    { 
    first=0; 
    for(j=1;j<=1;j++) 
    { 
     if(array[j]<array[first]) 
      first = j; 

    } 

    temp = array[first]; 

    array[first] = array[i]; 
    array[i]=temp; 

    } 


    System.out.println("\nThe sorted array is:"); 
    for(i = 0; i < array.length; i++){ 
    System.out.print(array[i] + " "); 

    } 

} 

} 

回答

4

您似乎有一個錯字。這條線:

for(j=1;j<=1;j++) 

也許應該是:

for(j=1;j<=i;j++) 

(循環終止測試應該是j<=i,不j<=1。)