2014-10-21 2 views
0

사용자의 입력을 포함하는 배열이 있습니다. 이 프로그램은 버블 정렬, 선택 정렬 및 삽입 정렬에 관한 것입니다. 첫 번째 버블, 두 번째 선택 및 삽입 정렬이 제공됩니다.이미 정렬 된 배열

문제를 해결할 수 없었습니다. 코드가 선택 정렬로 실행되면 배열은 이미 버블 정렬에 의해 정렬됩니다.

우선 선택 및 삽입 정렬시 "소스 배열"을 사용하기 위해 임시 배열을 2 개 만들었지 만 그 배열은 버블 정렬을 통해 다시 정렬됩니다. (나는 왜 이해가 안되는가)

배열을 별도로 정렬 할 수있는 방법이 있습니까? 나는 스왑과 비교도 계산하고있다. 감사 !

System.out.println("• Please enter the number of elements in the Sorting Bag:"); 
    length = input.nextInt(); 
    System.out.println("• The number of elements: " + length); 

    int[] SorBag = new int[length]; 
    int[] SorBag2 = new int[length]; 
    int[] SorBag3 = new int[length]; 

    System.out.println("• Please enter the elements of Sorting Bag:"); 
    for (int i = 0; i < SorBag.length ; i++) { 
     SorBag[i] = input.nextInt(); 
    } 

    SorBag2 = SorBag; 
    SorBag3 = SorBag; 

    System.out.print("• Elements in the Sorting Bag are:"); 
    for (int j = 0; j < SorBag.length; j++) { 
     System.out.print(" " + SorBag[j]); 
    } 
    System.out.println(""); 
    System.out.println(""); 

    //Bubble Sort  
    for (int i = 1; i < SorBag.length; i++) { 
     for (int j = 0; j < SorBag.length - i; j++) { 
      BComparison++; 
      if (SorBag[j] > SorBag[j + 1]) { 
       BSwaps++; 
       temp1 = SorBag[j + 1]; 
       SorBag[j + 1] = SorBag[j]; 
       SorBag[j] = temp1; 
      } 
     } 
    } 
    System.out.print("• Bubble Sort:"); 
    for (int k = 0; k < SorBag.length; k++) { 
     System.out.print(" " + SorBag[k] + " "); 
    } 
    System.out.print("Comparisons: " + BComparison + " Swaps: " + BSwaps); 
    System.out.println(" "); 

    //Selection Sort 
    for (int i = 0; i < SorBag2.length; i++) { 
     min = i; 

     for (int j = i + 1; j < SorBag2.length; j++) { 
      SComparison++; 

      if (SorBag2[j] < SorBag2[min]) { 
       min = j; 
      } 

      if (min != i) { 

       temp2 = SorBag2[i]; 
       SorBag2[i] = SorBag2[min]; 
       SorBag2[min] = temp2; 
       SSwaps++; 
      } 
     } 
    } 

    System.out.print("• Selection Sort:"); 
    for (int k = 0; k < SorBag2.length; k++) { 
     System.out.print(" " + SorBag2[k] + " "); 
    } 
    System.out.print("Comparisons: " + SComparison + " Swaps: " + SSwaps); 
    System.out.println(" "); 

    //Insertion Sort 
    for (int i = 1; i < SorBag3.length; i++) { 

     int j = 0; 

     while (j > i && SorBag3[j] < SorBag3[j - 1]) { 

      temp3 = SorBag3[j]; 
      SorBag3[j] = SorBag3[j - 1]; 
      SorBag3[j - 1] = temp3; 

      ISwaps++; 

      j--; 
     } 

     IComparison++; 
    } 
    System.out.print("• Insertion Sort:"); 
    for (int k = 0; k < SorBag3.length; k++) { 
     System.out.print(" " + SorBag3[k] + " "); 
    } 
    System.out.print("Comparisons: " + IComparison + " Swaps: " + ISwaps); 
    System.out.println(" "); 

} 
} 
+2

: 그래서 대신 :

System.out.println("• Please enter the elements of Sorting Bag:"); for (int i = 0; i < SorBag.length ; i++) { SorBag[i] = input.nextInt(); } SorBag2 = SorBag; SorBag3 = SorBag; 

이 시도? – reto

+0

코드는 어디에 있습니까? –

+0

어떻게 소스 배열을 잡고 여전히 루프/메서드에서 정렬 할 수 있는지 이해할 수 없습니다. – Tetramputechture

답변

4

SorBag2 = SorBagSorBag3 = SorBag 복사 다른 두 어레이 SorBag의 기준 대신 데이터 만 복사. 당신이 바로, 코드의 관련 부분을 추가하려고

System.out.println("• Please enter the elements of Sorting Bag:"); 
for (int i = 0; i < SorBag.length ; i++) { 
    int nextInt = intput.nextInt(); 
    SorBag[i] = nextInt; 
    SorBag2[i] = nextInt; 
    SorBag3[i] = nextInt;   
} 
+0

예! 마침내. 고맙습니다. 작동 중입니다. – HalilM

+1

문제 없습니다. 이 질문에 대한 답변이 있으면 [답변으로 수락하십시오] (http://meta.stackexchange.com/a/5235). 또한 추천 :'SorBag' 대신'sorBag'을 사용하십시오. 꼭 필요한 것은 아니지만 대부분의 프로그래밍 언어에서 변수/필드 이름은 소문자로 시작합니다. –