링크
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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8030 | 9년 전 | 408 | ||
| 8029 | 9년 전 | 332 | ||
| 8028 | 9년 전 | 291 | ||
| 8027 | 9년 전 | 299 | ||
| 8026 | 9년 전 | 373 | ||
| 8025 | 9년 전 | 405 | ||
| 8024 | 9년 전 | 388 | ||
| 8023 | 9년 전 | 419 | ||
| 8022 | 9년 전 | 334 | ||
| 8021 | 9년 전 | 356 | ||
| 8020 | 9년 전 | 350 | ||
| 8019 | 9년 전 | 367 | ||
| 8018 | 9년 전 | 471 | ||
| 8017 | 9년 전 | 555 | ||
| 8016 | 9년 전 | 371 | ||
| 8015 | 9년 전 | 411 | ||
| 8014 | 9년 전 | 343 | ||
| 8013 | 9년 전 | 263 | ||
| 8012 | 9년 전 | 266 | ||
| 8011 | 9년 전 | 473 | ||
| 8010 | 9년 전 | 327 | ||
| 8009 | 9년 전 | 339 | ||
| 8008 | 9년 전 | 307 | ||
| 8007 | 9년 전 | 456 | ||
| 8006 | 9년 전 | 493 | ||
| 8005 |
|
9년 전 | 990 | |
| 8004 | 9년 전 | 378 | ||
| 8003 | 9년 전 | 448 | ||
| 8002 | 9년 전 | 346 | ||
| 8001 |
|
9년 전 | 685 | |
| 8000 | 9년 전 | 447 | ||
| 7999 | 9년 전 | 402 | ||
| 7998 | 9년 전 | 461 | ||
| 7997 | 9년 전 | 328 | ||
| 7996 | 9년 전 | 559 | ||
| 7995 | 9년 전 | 503 | ||
| 7994 | 9년 전 | 384 | ||
| 7993 | 9년 전 | 453 | ||
| 7992 | 9년 전 | 539 | ||
| 7991 | 9년 전 | 281 | ||
| 7990 | 9년 전 | 315 | ||
| 7989 | 9년 전 | 326 | ||
| 7988 | 9년 전 | 753 | ||
| 7987 | 9년 전 | 455 | ||
| 7986 | 9년 전 | 456 | ||
| 7985 | 9년 전 | 535 | ||
| 7984 | 9년 전 | 452 | ||
| 7983 | 9년 전 | 694 | ||
| 7982 | 9년 전 | 551 | ||
| 7981 | 9년 전 | 511 | ||
| 7980 | 9년 전 | 532 | ||
| 7979 | 9년 전 | 522 | ||
| 7978 | 9년 전 | 486 | ||
| 7977 | 9년 전 | 425 | ||
| 7976 | 9년 전 | 886 | ||
| 7975 | 9년 전 | 398 | ||
| 7974 | 9년 전 | 440 | ||
| 7973 | 9년 전 | 628 | ||
| 7972 | 9년 전 | 412 | ||
| 7971 | 9년 전 | 490 | ||
| 7970 | 9년 전 | 333 | ||
| 7969 | 9년 전 | 571 | ||
| 7968 | 9년 전 | 419 | ||
| 7967 | 9년 전 | 404 | ||
| 7966 | 9년 전 | 412 | ||
| 7965 |
|
9년 전 | 1043 | |
| 7964 | 9년 전 | 431 | ||
| 7963 | 9년 전 | 437 | ||
| 7962 | 9년 전 | 433 | ||
| 7961 |
전갈자리남자
|
9년 전 | 526 | |
| 7960 | 9년 전 | 996 | ||
| 7959 | 9년 전 | 578 | ||
| 7958 | 9년 전 | 431 | ||
| 7957 | 9년 전 | 384 | ||
| 7956 | 9년 전 | 387 | ||
| 7955 | 9년 전 | 488 | ||
| 7954 | 9년 전 | 422 | ||
| 7953 | 9년 전 | 465 | ||
| 7952 | 9년 전 | 389 | ||
| 7951 | 9년 전 | 522 | ||
| 7950 | 9년 전 | 416 | ||
| 7949 | 9년 전 | 413 | ||
| 7948 | 9년 전 | 350 | ||
| 7947 | 9년 전 | 962 | ||
| 7946 | 9년 전 | 476 | ||
| 7945 | 9년 전 | 425 | ||
| 7944 | 9년 전 | 476 | ||
| 7943 | 9년 전 | 416 | ||
| 7942 | 9년 전 | 432 | ||
| 7941 | 9년 전 | 420 | ||
| 7940 | 9년 전 | 922 | ||
| 7939 | 9년 전 | 399 | ||
| 7938 | 9년 전 | 430 | ||
| 7937 | 9년 전 | 314 | ||
| 7936 | 9년 전 | 904 | ||
| 7935 | 9년 전 | 494 | ||
| 7934 | 9년 전 | 466 | ||
| 7933 | 9년 전 | 579 | ||
| 7932 | 9년 전 | 533 | ||
| 7931 | 9년 전 | 588 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기