class QuickSorting{

  static int[] elements = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};

  public static void main(String[] args) throws Exception {
    quickSort(0, elements.length - 1);
    printElement();
   }

   static void quickSort(int startIndex, int endIndex) {
     if ((endIndex - startIndex) <= 0) {
       return;
     }
    int pivotIndex = startIndex;
    int storeIndex = pivotIndex + 1;

    for (int i = pivotIndex + 1; i <= endIndex; i++) {
      if (elements[i] < elements[pivotIndex]) {
        swapIndex(i, storeIndex);
        storeIndex++;
      }
    }
    swapIndex(pivotIndex, storeIndex - 1);
    quickSort(startIndex, storeIndex - 2);
    quickSort(storeIndex, endIndex);
   }

   static void swapIndex(int foreIndex, int backIndex) {
     if (foreIndex == backIndex) {
       return;
     }
     int tmpValue = elements[foreIndex];
     elements[foreIndex] = elements[backIndex];
     elements[backIndex] = tmpValue;
   }

   static void printElement() {
     for (int element : elements) {
       System.out.printf("%d ", element);
     }
     System.out.println();
   }

}

Posted by 안낭우훗

블로그 이미지
좋은싸이트 공유, 재해석 , 공부 정리, 틀린거 알려 주세요~
안낭우훗

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.5
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

글 보관함