링크
http://terms.naver.com/entry.nhn?docId=2270444&cid=51173&categoryId=51173 (15) https://ko.wikipedia.org/wiki/%ED%80%B5_%EC%A0%95%EB%A0%AC (15)<?
// 퀵 정렬(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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7930 | 9년 전 | 397 | ||
| 7929 | 9년 전 | 314 | ||
| 7928 | 9년 전 | 406 | ||
| 7927 | 9년 전 | 326 | ||
| 7926 | 9년 전 | 657 | ||
| 7925 | 9년 전 | 340 | ||
| 7924 | 9년 전 | 328 | ||
| 7923 | 9년 전 | 333 | ||
| 7922 | 9년 전 | 368 | ||
| 7921 | 9년 전 | 394 | ||
| 7920 | 9년 전 | 312 | ||
| 7919 | 9년 전 | 330 | ||
| 7918 | 9년 전 | 485 | ||
| 7917 | 9년 전 | 323 | ||
| 7916 | 9년 전 | 398 | ||
| 7915 | 9년 전 | 396 | ||
| 7914 | 9년 전 | 403 | ||
| 7913 | 9년 전 | 558 | ||
| 7912 | 9년 전 | 406 | ||
| 7911 | 9년 전 | 354 | ||
| 7910 | 9년 전 | 397 | ||
| 7909 | 9년 전 | 498 | ||
| 7908 | 9년 전 | 411 | ||
| 7907 | 9년 전 | 356 | ||
| 7906 | 9년 전 | 377 | ||
| 7905 | 9년 전 | 354 | ||
| 7904 | 9년 전 | 341 | ||
| 7903 | 9년 전 | 341 | ||
| 7902 | 9년 전 | 547 | ||
| 7901 |
|
9년 전 | 726 | |
| 7900 | 9년 전 | 568 | ||
| 7899 | 9년 전 | 366 | ||
| 7898 | 9년 전 | 377 | ||
| 7897 | 9년 전 | 329 | ||
| 7896 | 9년 전 | 349 | ||
| 7895 | 9년 전 | 459 | ||
| 7894 | 9년 전 | 374 | ||
| 7893 | 9년 전 | 323 | ||
| 7892 | 9년 전 | 372 | ||
| 7891 | 9년 전 | 751 | ||
| 7890 | 9년 전 | 1187 | ||
| 7889 | 9년 전 | 741 | ||
| 7888 |
limsy1987
|
9년 전 | 540 | |
| 7887 | 9년 전 | 534 | ||
| 7886 | 9년 전 | 422 | ||
| 7885 | 9년 전 | 394 | ||
| 7884 | 9년 전 | 398 | ||
| 7883 | 9년 전 | 393 | ||
| 7882 | 9년 전 | 435 | ||
| 7881 | 9년 전 | 425 | ||
| 7880 | 9년 전 | 547 | ||
| 7879 | 9년 전 | 447 | ||
| 7878 | 9년 전 | 1205 | ||
| 7877 | 9년 전 | 738 | ||
| 7876 | 9년 전 | 470 | ||
| 7875 | 9년 전 | 548 | ||
| 7874 |
|
9년 전 | 797 | |
| 7873 | 9년 전 | 509 | ||
| 7872 | 9년 전 | 662 | ||
| 7871 | 9년 전 | 471 | ||
| 7870 | 9년 전 | 597 | ||
| 7869 | 9년 전 | 420 | ||
| 7868 | 9년 전 | 429 | ||
| 7867 | 9년 전 | 408 | ||
| 7866 | 9년 전 | 476 | ||
| 7865 | 9년 전 | 439 | ||
| 7864 | 9년 전 | 503 | ||
| 7863 | 9년 전 | 498 | ||
| 7862 | 9년 전 | 459 | ||
| 7861 | 9년 전 | 616 | ||
| 7860 | 9년 전 | 608 | ||
| 7859 | 9년 전 | 398 | ||
| 7858 | 9년 전 | 693 | ||
| 7857 | 9년 전 | 1059 | ||
| 7856 | 9년 전 | 507 | ||
| 7855 | 9년 전 | 730 | ||
| 7854 | 9년 전 | 720 | ||
| 7853 | 9년 전 | 570 | ||
| 7852 | 9년 전 | 499 | ||
| 7851 | 9년 전 | 491 | ||
| 7850 | 9년 전 | 576 | ||
| 7849 | 9년 전 | 351 | ||
| 7848 | 9년 전 | 402 | ||
| 7847 | 9년 전 | 638 | ||
| 7846 | 9년 전 | 445 | ||
| 7845 | 9년 전 | 401 | ||
| 7844 | 9년 전 | 385 | ||
| 7843 | 9년 전 | 401 | ||
| 7842 | 9년 전 | 389 | ||
| 7841 | 9년 전 | 374 | ||
| 7840 | 9년 전 | 390 | ||
| 7839 | 9년 전 | 425 | ||
| 7838 | 9년 전 | 507 | ||
| 7837 | 9년 전 | 344 | ||
| 7836 | 9년 전 | 390 | ||
| 7835 | 9년 전 | 461 | ||
| 7834 |
|
9년 전 | 1184 | |
| 7833 | 9년 전 | 412 | ||
| 7832 | 9년 전 | 405 | ||
| 7831 | 9년 전 | 544 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기