2014년 3월 11일 화요일

Java - 2차원 배열, 정렬 알고리즘(선택, 삽입, 버블)

# 2차원 배열의 첫번째 인자를 그룹으로 정렬하고, 두번째 인자를 그룹인자에 맞춰서 정렬
- 정렬 알고리즘 : 선택정렬, 삽입정렬, 버블정렬, 퀵정렬(Arrays.sort())









# TwoDimArraySort.java
                     import java.util.Arrays;
                      
                     public class TwoDimArraySort{
                      
                         /* 2차원 배열 초기화 */
                         private static final int[][] ARRAY_NUMBER   = new int[][]{
                                 {100, 10}, {101,6}, {102,9}, {103,3}, {110,1}
                               , {105,6}, {110,5}, {101,8}, {107,3}, {103,2}
                               , {100,3}, {105,5}, {102,5}, {110,7}, {107,1}
                               , {100,7}, {105,9}, {102,2}, {110,3}, {107,8}
                               };
                      
                         /**
                         * 숫자 정렬
                         */
                         public static void sortInteger(String sortType){
                      
                             int[] dupArray = new int[ARRAY_NUMBER.length]; // 중복 값 제거후 저장할 배열
                             int index = 0;                                   // 중복 값 배렬 순서 지정
                      
                             /**
                              * 중복값 추출및 1차 배열 순서 정렬
                              */
                             for(int i=0; i<ARRAY_NUMBER.length ; i++){
                                 if (i == 0){
                                     dupArray[index] = ARRAY_NUMBER[i][0];
                                     i++;
                                 }else{
                                     boolean isDuplication = false;
                                     for(int j=i+1; j<ARRAY_NUMBER.length; j++){
                                         if(ARRAY_NUMBER[i][0] == ARRAY_NUMBER[j][0]){
                                             isDuplication = true;
                                             break;
                                         }
                                         if(ARRAY_NUMBER[i][0] > ARRAY_NUMBER[j][0]){
                                             int temp = ARRAY_NUMBER[j][0];
                                             ARRAY_NUMBER[j][0] = ARRAY_NUMBER[i][0];
                                             ARRAY_NUMBER[i][0] = temp;
                      
                                             int tempOption = ARRAY_NUMBER[i][1];
                                             ARRAY_NUMBER[i][1] = ARRAY_NUMBER[j][1];
                                             ARRAY_NUMBER[j][1] = tempOption;
                                         }
                                     }
                      
                                     if(!isDuplication){ // 중복이 아니면 dupArray 배열에 순서대로 저장
                                         dupArray[index] = ARRAY_NUMBER[i][0];
                                         index++;
                                     }
                                 }
                             } // 중복값 추출및 1차 배열 순서 정렬 for문 종료
                      
                             /**
                              *  정렬된 그룹번호 갯수 만큼 for문 실행
                              */
                             for(int j=0; j<dupArray.length; j++){
                                 if(dupArray[j] == 0) break;         // 값이 0(중복에서 제거)일경우 중지
                      
                                 System.out.print("#"+dupArray[j]+"("); // 그룹 번호 출력
                      
                                 /* 각 그룹별 건수 확인 및 저장 */
                                 int cnt = 0;
                                 for(int s=0; s<ARRAY_NUMBER.length; s++){
                                     if(dupArray[j] == ARRAY_NUMBER[s][0]){
                                         cnt++;
                                     }
                                 }
                      
                                 /* 그룹 건수만큼 for문 수행 */
                                 for(int i=0; i<cnt; i++){
                                     int[] tmpOption = new int[cnt];
                                     int c = 0;
                      
                                     /* 그룹별 값을 tmpOption 배열에 값 저장 */
                                     for(int s=0; s<ARRAY_NUMBER.length; s++){
                                         if(dupArray[j] == ARRAY_NUMBER[s][0]){
                                             tmpOption[c] = ARRAY_NUMBER[s][1];
                                             c++;
                                             if(cnt == c) break; // 그룹별 건수와 같으면 for문 중지
                                         }
                                     }
                      
                                     /**
                                      * # 정렬 알고리즘에 따른 그룹별 값을 정렬 처리
                                      */
                                     if(sortType.equals("selectionSort")){    /* 선택 정렬 */
                                         int minIndex;
                                         int temp;
                      
                                         for (int a = 0; a<tmpOption.length-1; a++) {
                                             minIndex = a;
                                             for (int b=a+1; b<tmpOption.length; b++) {
                                                 if (tmpOption[b]<tmpOption[minIndex]) {
                                                     minIndex = b;
                                                 }
                                             }
                                             temp = tmpOption[minIndex];
                                             tmpOption[minIndex] = tmpOption[a];
                                             tmpOption[a] = temp;                //
                                         } // 선택 정렬 for문 종료
                                     }else if(sortType.equals("insertionSort")){ /* 삽입 정렬 */
                                         int minIndex;
                                         int a;
                                         int b;
                      
                                         for(a=0; a<tmpOption.length; a++){
                                             minIndex = tmpOption[a];
                                             for(b=a; b>0; b--){
                                                 if(tmpOption[b-1] > minIndex){
                                                     tmpOption[b] = tmpOption[b-1];
                                                 }else{
                                                     break;
                                                 }
                                             }
                                             tmpOption[b] = minIndex;
                                         } // 삽입 정렬 for문 종료
                                     }else if(sortType.equals("bubbleSort")){   /* 버블 정렬 */
                                         int temp;
                                         
                                         for(int a=0; a<cnt; a++){
                                             for(int b=a+1; b<cnt; b++){
                                                 if(tmpOption[a] > tmpOption[b]){
                                                     temp = tmpOption[b];
                                                     tmpOption[b] = tmpOption[a];
                                                     tmpOption[a] = temp;
                                                 }
                                             }
                                         } // 버블 정렬 for문 종료
                                     }else{
                                         /**
                                          * 2) Arrays.sort() 메서드를 이용한 정렬
                                          * # The sorting algorithm is a Dual-Pivot Quicksort
                                          */
                                         Arrays.sort(tmpOption);
                     // Arrays 정보 출력 : 100 >[3, 7, 10]
                     // System.out.println(Arrays.toString(tmpOption));
                                     }
                      
                                     /* 그룹별 정렬된 결과 값 출력 */
                                     for(int t=0; t<cnt; t++){
                                         System.out.print(tmpOption[t]);
                                         if(t+1<cnt) System.out.print(",");
                                     }
                                     System.out.print(")");
                                     if(i == 0) break; // 메인 for문 반복 중지
                                 } // 그룹별 건수만큼 for문 수행 종료
                                 System.out.println();
                             } // 정렬된 그룹번호 갯수 for문 종료      
                         }
                      
                         public static void main(String[] arr){
                             sortInteger("selectionSort");  // 선택 정렬
//                           sortInteger("insertionSort");  // 삽입 정렬
//                           sortInteger("bubbleSort");     // 버블 정렬
//                           sortInteger("");               // Arrays.sort() 퀵 정렬
                         }
                     }
                      
                     /* # 출력 결과 */
                     /**
                     #100(3,7,10)
                     #101(6,8)
                     #102(2,5,9)
                     #103(2,3)
                     #105(5,6,9)
                     #107(1,3,8)
                     #110(1,3,5,7)
                     */

댓글 없음:

댓글 쓰기