2017-05-03 5 views
0

기수 정렬에 대한 이해가 안되었습니다. 나는 더 이상 글자가 남지 않을 때까지 오른쪽에서 왼쪽으로 정렬하는 것과 같은 단어의 마지막 글자를 분류해야합니다.Java에서 역순으로 기수 정렬

바 고양이 사과 버그 톱니 케이 퍼 뿔 발목 곰 같은 텍스트 파일보기

발목 뿔,536 같은 내 출력사과 바 곰 버그 케이 퍼 고양이 톱니

하지만이

바 버그 고양이 톱니 곰 발목 사과 케이 퍼 같은 출력을 얻을 수 있겠 달구어

올바른 코드가있는 것 같아 보이지만 방해가되어서 무엇을해야할지 모르겠습니다. 내가 무슨 짓을했는지 코드가 도움이 올바른 방향으로 여기

날 지점 얻을 수 있다면 크게

RadixSort.java

public class RadixSort { 
     public static void main(String[]args) throws FileNotFoundException{ 
    Linkedlist[] allNameLinkedList = new Linkedlist[26]; // create an array 
     of LinkedList for 26 letters in alphabets 
    int count = 0; 
    // initialize all the elements in the array to new LinkedList 
    for (int i = 0; i < allNameLinkedList.length; i++) { 
     allNameLinkedList[i] = new Linkedlist(); 
    } 
    Scanner scan = new Scanner(new File("words.txt")); 

    while(scan.hasNextLine()) 
    { 
     String currentname = scan.nextLine(); 
     for(int i = 0; i < 26; i++){ 
      if(currentname.charAt(2) == (char)(i+97)) 
      { 
       allNameLinkedList[i].addNodeToTheEndOfTheList(currentname); 
      } 
     } 
     count++; 
    } 

    // copy sorted nodes to new LinkedList called container 
    Linkedlist container = new Linkedlist(); 
    for (int i = 0; i < 26; i++) { 
     Node n = allNameLinkedList[i].front; 

     while(n != null){ 
      container.addNodeToTheEndOfTheList(n.name); 
      n = n.next; 
     } 
    } 
    // empty all the elements of array 
    for (int i = 0; i < allNameLinkedList.length; i++) { 
     allNameLinkedList[i] = new Linkedlist(); 
    } 

    Node m = container.front; 
    while(m!=null) 
    { 
     String currentname = m.name; 
     for(int i = 0; i < 26; i++){ 
      if(currentname.charAt(1) == (char)(i+97)) 
      { 
       allNameLinkedList[i].addNodeToTheEndOfTheList(currentname); 
      } 
     } 
     m = m.next; 
     count++; 
    } 
    container = new Linkedlist(); 
    for (int i = 0; i < 26; i++) { 
     m = allNameLinkedList[i].front; 

     while(m!=null){ 
      container.addNodeToTheEndOfTheList(m.name); 
      m = m.next; 
     } 
    } 
    for (int i = 0; i < allNameLinkedList.length; i++) { 
     allNameLinkedList[i] = new Linkedlist(); 
    } 
    m = container.front; 

    while(m!=null) 
    { 
     String currentname = m.name; 

     for(int i = 0; i < 26; i++){ 
      if(currentname.charAt(0) == (char)(i+97)) 
      { 
       allNameLinkedList[i].addNodeToTheEndOfTheList(currentname); 
      } 
     } 
     m = m.next; 
     count++; 
    } 
    container = new Linkedlist(); 
    for (int i = 0; i < 26; i++) { 
     m = allNameLinkedList[i].front; 

     while(m!=null){ 
      System.out.println(m.name); 
      container.addNodeToTheEndOfTheList(m.name); 
      m = m.next; 
     } 
    } 
    scan.close(); 
    System.out.println("The total number of comparisions was :"+count); 
    } 
    } 
+0

나중에 참조 할 수 있도록 잘못 작성된 내용을 적은 양의 텍스트로 보여주는 간단한 자체 포함 된 올바른 예제를 포함하는 것이 좋습니다. 여기에는 연결된 목록 코드가 포함됩니다. 원하는 내용에 대한 자세한 내용은 http://sscce.org/ – dddJewelsbbb

+1

코드가 너무 많아서 질문을 이해하지 못했지만 단어를 정렬했는데 실제로 필요한 것은 무엇입니까? '결과물은 기수 정렬과 관련이 있습니까? – Shadov

답변

0

귀하의 문제 만 정렬되는 것입니다 주시면 감사하겠습니다 첫 번째 문자.

while(m!=null) 
    { 
     String currentname = m.name; 

     for(int i = 0; i < 26; i++){ 
      // charAt(0) is first character in String 
      if(currentname.charAt(0) == (char)(i+97)) 
      { 
       allNameLinkedList[i].addNodeToTheEndOfTheList(currentname); 
      } 
     } 
     m = m.next; 
     count++; 
    } 

첫 번째 문자가 아닌 모든 문자를 반복해야합니다. 이것은 왜 정렬이 알파벳 순서인지 설명합니다. 어떻게 그것이 완벽한 사전 편집 순서에 속하는지는 저를 넘어서 있습니다. 코드 스 니펫은 charAt (0)을 기반으로 연결된 데이터 "버킷"으로만 데이터를 정렬합니다.

입력이 정렬되지 않았고 출력이 실제로 알파벳 기수 정렬에 필요한 결과이기 때문에 입력 및 출력이 더 이상 나타나지 않는 것처럼 보입니다. MSD 기수 정렬.

사전 식 순서의 경우 높은 비교 크기가 첫 문자이기 때문에 최상위 자릿수는 알파벳 비교를 위해 원하는 것입니다. 정수 비교에서와 같이 다른 종류의 LSD (최하위 자리)가있을 수 있지만 다른 길이의 문자열에 대해 걱정해야하기 때문에 LSD 기수 정렬이 짧다고 경고해야합니다.

MSD 기수 정렬을 사용하면 큰 문제가 없습니다. 단어의 범위를 초과하지 말고 다른 단어로 이동하지 않는 한 단어를 배치 한 후에 이동하지 마십시오.LSD의 경우, current word length - current index > 0이 실제 문자로 계속 정렬되기 전에 먼저 charAt(word length - current index) 인 경우 역순으로 색인을 생성해야하지만 최종 결과는 실제로는 사전 순으로 정렬되지 않습니다. LSD 기수 정렬에 거의 목적이 없습니다. 개념 증명 (proof of-concept)은 그러한 방식으로 주문할 수 있습니다. 또는 귀하의 최종 결과가 부분적으로 만 주문되도록 그러한 방식으로 특별히 귀하에게 주어진 과제물.

둘째, 각 반복 후에 정렬 할 논리가 나타나지 않습니다. 모든 반복이 발생할 때마다 각 버킷 내에서 정렬하여 모든 것이 정렬 된 순서인지 확인해야합니다.