2018. 8. 1. 23:39 ALGORITHM/문제해결
[JAVA] 퀵소팅(QuickSorting)
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();
}
}
'ALGORITHM > 문제해결' 카테고리의 다른 글
[JAVA] 숫자를 입력받아서 홀수 개수와 짝수 개수를 센다 (0) | 2018.08.04 |
---|---|
[JAVA] 홀수, 짝수 구별(oddNum, evenNum) (0) | 2018.08.02 |
[JAVA] 일련의(규칙의) 숫자 더하기, (addNum) (0) | 2018.08.02 |
[JAVA] 버블소팅(BubbleSorting) (0) | 2018.08.01 |
[Algorithm] 알고리즘 이란 (0) | 2018.08.01 |