링크
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년 전 | 405 | ||
| 8029 | 9년 전 | 329 | ||
| 8028 | 9년 전 | 288 | ||
| 8027 | 9년 전 | 299 | ||
| 8026 | 9년 전 | 372 | ||
| 8025 | 9년 전 | 405 | ||
| 8024 | 9년 전 | 383 | ||
| 8023 | 9년 전 | 417 | ||
| 8022 | 9년 전 | 333 | ||
| 8021 | 9년 전 | 353 | ||
| 8020 | 9년 전 | 348 | ||
| 8019 | 9년 전 | 364 | ||
| 8018 | 9년 전 | 471 | ||
| 8017 | 9년 전 | 554 | ||
| 8016 | 9년 전 | 367 | ||
| 8015 | 9년 전 | 411 | ||
| 8014 | 9년 전 | 342 | ||
| 8013 | 9년 전 | 262 | ||
| 8012 | 9년 전 | 265 | ||
| 8011 | 9년 전 | 469 | ||
| 8010 | 9년 전 | 326 | ||
| 8009 | 9년 전 | 337 | ||
| 8008 | 9년 전 | 304 | ||
| 8007 | 9년 전 | 454 | ||
| 8006 | 9년 전 | 492 | ||
| 8005 |
|
9년 전 | 987 | |
| 8004 | 9년 전 | 375 | ||
| 8003 | 9년 전 | 447 | ||
| 8002 | 9년 전 | 343 | ||
| 8001 |
|
9년 전 | 685 | |
| 8000 | 9년 전 | 445 | ||
| 7999 | 9년 전 | 400 | ||
| 7998 | 9년 전 | 459 | ||
| 7997 | 9년 전 | 327 | ||
| 7996 | 9년 전 | 559 | ||
| 7995 | 9년 전 | 498 | ||
| 7994 | 9년 전 | 383 | ||
| 7993 | 9년 전 | 446 | ||
| 7992 | 9년 전 | 537 | ||
| 7991 | 9년 전 | 280 | ||
| 7990 | 9년 전 | 313 | ||
| 7989 | 9년 전 | 324 | ||
| 7988 | 9년 전 | 753 | ||
| 7987 | 9년 전 | 454 | ||
| 7986 | 9년 전 | 453 | ||
| 7985 | 9년 전 | 532 | ||
| 7984 | 9년 전 | 449 | ||
| 7983 | 9년 전 | 693 | ||
| 7982 | 9년 전 | 551 | ||
| 7981 | 9년 전 | 508 | ||
| 7980 | 9년 전 | 531 | ||
| 7979 | 9년 전 | 520 | ||
| 7978 | 9년 전 | 486 | ||
| 7977 | 9년 전 | 425 | ||
| 7976 | 9년 전 | 883 | ||
| 7975 | 9년 전 | 397 | ||
| 7974 | 9년 전 | 432 | ||
| 7973 | 9년 전 | 628 | ||
| 7972 | 9년 전 | 409 | ||
| 7971 | 9년 전 | 483 | ||
| 7970 | 9년 전 | 328 | ||
| 7969 | 9년 전 | 569 | ||
| 7968 | 9년 전 | 414 | ||
| 7967 | 9년 전 | 399 | ||
| 7966 | 9년 전 | 411 | ||
| 7965 |
|
9년 전 | 1041 | |
| 7964 | 9년 전 | 426 | ||
| 7963 | 9년 전 | 437 | ||
| 7962 | 9년 전 | 428 | ||
| 7961 |
전갈자리남자
|
9년 전 | 524 | |
| 7960 | 9년 전 | 993 | ||
| 7959 | 9년 전 | 577 | ||
| 7958 | 9년 전 | 430 | ||
| 7957 | 9년 전 | 381 | ||
| 7956 | 9년 전 | 386 | ||
| 7955 | 9년 전 | 485 | ||
| 7954 | 9년 전 | 419 | ||
| 7953 | 9년 전 | 464 | ||
| 7952 | 9년 전 | 388 | ||
| 7951 | 9년 전 | 520 | ||
| 7950 | 9년 전 | 415 | ||
| 7949 | 9년 전 | 411 | ||
| 7948 | 9년 전 | 350 | ||
| 7947 | 9년 전 | 959 | ||
| 7946 | 9년 전 | 475 | ||
| 7945 | 9년 전 | 421 | ||
| 7944 | 9년 전 | 472 | ||
| 7943 | 9년 전 | 411 | ||
| 7942 | 9년 전 | 429 | ||
| 7941 | 9년 전 | 419 | ||
| 7940 | 9년 전 | 922 | ||
| 7939 | 9년 전 | 397 | ||
| 7938 | 9년 전 | 428 | ||
| 7937 | 9년 전 | 313 | ||
| 7936 | 9년 전 | 904 | ||
| 7935 | 9년 전 | 489 | ||
| 7934 | 9년 전 | 463 | ||
| 7933 | 9년 전 | 576 | ||
| 7932 | 9년 전 | 531 | ||
| 7931 | 9년 전 | 585 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기