2013-12-09 3 views
0

나는이 코드를 가지고 왜 선택 정렬이 모든 방법으로 정렬되지 않는지 모르겠다. 누구든지 프로그램을 고칠 곳을 알고 있는가? 내가 믿는 선택 정렬 코드는 옳지 않다. 나는 무엇이 잘못되었는지를 모른다. 코드가 작동 중입니다자바 정렬 배열

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해야합니다.)