2014-03-14 4 views
0

내 프로그램은 최대 길이의 단어 인 최상위 숫자, 두 번째 숫자는 거기에있는 단어의 수, 나머지는 숫자 인 파일을 현재 형식으로 가져옵니다. 정렬 할 수 있습니다.정렬 프로그램이 올바르게 정렬되지 않음

4 
10 
437 1807 3218 1791 9058 9322 766 9977 16 7143 

그런 다음 가장 낮은 것부터 가장 높은 것까지 정렬하십시오. 그러나 내가 그것을 얻으려고 할 때마다 나는 일반적으로 첫 번째 자리에서 가장 높은 숫자를 얻고 나머지는 뒤죽박죽이다. 이 의사 코드와 유사한 마지막 세 루프는 다음과 같습니다.

the first power of ten = 10; 
for each i up to the maximum number of digits+1 
    1. for each value v in the array, starting at position 0: 
      a. isolate the “ith” digit from v (call this digit d) – use the current power 
                   of ten to do this 
      b. add v to bin[d] 
    2. for each bin (that is, each bin[j], starting with j=0) 
      a. while bin[j] is not empty 
       i. remove the first value from bin[j] and copy it back into the next 
       position in the array 
    3. Increase the power of ten to the next power of ten 

도움이 되셨을 것입니다.

import java.io.*; 
import java.util.*; 
public class BinSort { 

public static void main(String[] args) throws FileNotFoundException { 
    Scanner sc = new Scanner(new File(args[0])); 
    int length = sc.nextInt(); 
    int size = sc.nextInt(); 
    int[] temp = new int[size]; 
    sc.nextLine(); 
    for(int i = 0;i < temp.length;i++){ 
     temp[i] = sc.nextInt(); 
    } 

    sort(temp,size,length); 
} 

public static void sort(int[] input, int length,int size){ 
    ArrayList<Integer>[]bin=(ArrayList<Integer>[]) new ArrayList[10]; 
    for(int i = 0;i<length;i++){ 
     bin[i] = new ArrayList<Integer>(); 
    } 

    int power = 10; 
    for(int i = 0;i<size+1;i++){ 
     for(int v = 0;v<input.length;v++){ 
      int d = input[v] % power; 
      if(power>10) 
       d = d/ (power/10); 
      bin[d].add(input[v]); 
     } 
     for(int j = 0;j<10;j++){ 
      while(!bin[j].isEmpty()){ 
       int temp = bin[j].get(0); 
       bin[j].remove(0); 
       input[j] = temp; 
      } 
     } 
     power = power*10; 

    } 
    System.out.println("Final result"); 
    System.out.println(Arrays.toString(input)); 
} 
} 

답변

1
 input[j] = temp; 

읽어야

 input[pos] = temp; 
관련 문제