링크
http://terms.naver.com/entry.nhn?docId=2270444&cid=51173&categoryId=51173 (14) https://ko.wikipedia.org/wiki/%ED%80%B5_%EC%A0%95%EB%A0%AC (14)<?
// 퀵 정렬(Quick Sort)
function Swap(&$arr, $idx1, $idx2) {
$temp = $arr[$idx1];
$arr[$idx1] = $arr[$idx2];
$arr[$idx2] = $temp;
}
// 중간값 찾기
function MedianOfThree($arr, $left, $right) {
$samples = array($left, floor(($left+$right)/2), $right);
if ($arr[$samples[0]] > $arr[$samples[1]])
Swap($samples, 0, 1);
if ($arr[$samples[1]] > $arr[$samples[2]])
Swap($samples, 1, 2);
if ($arr[$samples[0]] > $arr[$samples[1]])
Swap($samples, 0, 1);
return $samples[1];
}
function Partition(&$arr, $left, $right) {
$pIdx = MedianOfThree($arr, $left, $right); // 중간값으로 피벗 선택
$pivot = $arr[$pIdx];
$low = $left + 1;
$high = $right;
Swap($arr, $left, $pIdx); // 피벗을 가장 왼쪽으로 이동
while ($low <= $high) { // 교차되지 않을 때까지 반복
// 피벗보다 큰 값을 찾는 과정
while ($low <= $right && $pivot >= $arr[$low])
$low++;
// 피벗보다 작은 값을 찾는 과정
while ($high >= ($left+1) && $pivot <= $arr[$high])
$high--;
// 교차되지 않는 상태라면 Swap 실행
if ($low <= $high)
Swap($arr, $low, $high);
}
Swap($arr, $left, $high); // 피벗과 high 가 가리키는 대상 교환
return $high; // 옮겨진 피벗의 위치정보 교환
}
function QuickSort(&$arr, $left, $right) {
if ($left < $right) {
$pivot = Partition($arr, $left, $right);
QuickSort($arr, $left, $pivot-1);
QuickSort($arr, $pivot+1, $right);
}
}
$arr = array(3, 2, 4, 1, 7, 6, 5);
$len = count($arr);
QuickSort($arr, 0, $len-1);
print_r($arr);
?>
게시판 목록
팁게시판
질문은 상단의 QA에서 해주시기 바랍니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6077 | 9년 전 | 161 | ||
| 6076 |
PlayPixel
|
9년 전 | 151 | |
| 6075 | 9년 전 | 164 | ||
| 6074 | 9년 전 | 221 | ||
| 6073 |
|
9년 전 | 180 | |
| 6072 |
|
9년 전 | 324 | |
| 6071 | 9년 전 | 231 | ||
| 6070 |
|
9년 전 | 273 | |
| 6069 | 9년 전 | 238 | ||
| 6068 |
|
9년 전 | 191 | |
| 6067 | 9년 전 | 207 | ||
| 6066 |
|
9년 전 | 321 | |
| 6065 |
PASKRAN
|
9년 전 | 214 | |
| 6064 | 9년 전 | 233 | ||
| 6063 |
kiplayer
|
9년 전 | 345 | |
| 6062 |
|
9년 전 | 275 | |
| 6061 | 9년 전 | 217 | ||
| 6060 |
|
9년 전 | 279 | |
| 6059 |
|
9년 전 | 217 | |
| 6058 |
|
9년 전 | 210 | |
| 6057 | 9년 전 | 257 | ||
| 6056 |
|
9년 전 | 208 | |
| 6055 |
|
9년 전 | 255 | |
| 6054 |
|
9년 전 | 232 | |
| 6053 |
snshero
|
9년 전 | 580 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기