슈

[알고리즘]힙 정렬(Heap Sort)

· 2016-01-28 (목) 23:21:43 · 2973

<?php

class Heap {

    var $numOfData;

    var $heapArr = array();

    var $comp;

 

    function __construct($pc) { // 힙의 초기화

        $this->numOfData = 0;

        $this->comp = $pc;

    }

 

    function HIsEmpty() { // 힙이 비었는지 확인

        if ($this->numOfData == 0)

            return TRUE;

        else

            return FALSE;

    }

 

    function GetParentIDX($idx) { // 부모 노드의 인덱스 값 반환

        return floor($idx/2);

    }

 

    function GetLChildIDX($idx) { // 왼쪽 자식 노드의 인덱스 값 반환

        return $idx*2;

    }

 

    function GetRChildIDX($idx) { // 오른쪽 자식 노드의 인덱스 값 반환

        return $this->GetLChildIDX($idx)+1;

    }

 

// 두 개의 자식 노드중 높은 우선 순위의 자식 노드 인덱스 값 반환

    function GetHiPriChildIDX($idx) {

        if ($this->GetLChildIDX($idx) > $this->numOfData)

            return 0;

 

        else if ($this->GetLChildIDX($idx) == $this->numOfData)

            return $this->GetLChildIDX($idx);

 

        else

        {

            $comp = $this->comp;

            if ($comp($this->heapArr[$this->GetLChildIDX($idx)],

                $this->heapArr[$this->GetRChildIDX($idx)]) < 0)

                return $this->GetRChildIDX($idx);

            else

                return $this->GetLChildIDX($idx);

        }

    }

 

// 힙에 데이터 저장

    function HInsert($data) {

        $idx = $this->numOfData+1;

        $comp = $this->comp;

        while ($idx != 1) {

            if($comp($data, $this->heapArr[$this->GetParentIDX($idx)]) > 0) {

                $this->heapArr[$idx] = $this->heapArr[$this->GetParentIDX($idx)];

                $idx = $this->GetParentIDX($idx);

            }

            else

                break;

        }

        $this->heapArr[$idx] = $data;

        $this->numOfData += 1;

    }

// 힙에서 데이터 삭제

    function HDelete() {

        $retData = $this->heapArr[1];

        $lastElem = $this->heapArr[$this->numOfData];

 

        $parentIdx = 1;

        $childIdx;

        $comp = $this->comp;

        while ($childIdx = $this->GetHiPriChildIDX($parentIdx)) {

            if ($comp($lastElem, $this->heapArr[$childIdx]) >= 0)

                break;

 

            $this->heapArr[$parentIdx] = $this->heapArr[$childIdx];

            $parentIdx = $childIdx;

        }

 

        $this->heapArr[$parentIdx] = $lastElem;

        $this->numOfData -= 1;

        return $retData;

    }

}

 

$arr = array(3, 4, 2, 1);

 

// 오름차순

function PriorityComp($d1, $d2) {

    return $d2 - $d1;

}

 

$heap = new Heap(PriorityComp);

foreach ($arr as $val)

$heap->HInsert($val);

 

foreach ($arr as $k => $v) 

$arr[$k] = $heap->Hdelete();

 

print_r($arr);

?>


|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
16-02-06 조회 3,566
16-02-04 조회 3,117
16-02-04 조회 3,387
16-02-04 조회 3,028
16-02-03 조회 3,185
16-02-03 조회 2,923
16-02-03 조회 2,975
16-02-03 조회 2,887
16-02-03 조회 3,008
16-02-02 조회 3,050
16-02-02 조회 2,932
16-02-02 조회 2,954
16-02-02 조회 2,845
16-02-02 조회 3,256
16-02-01 조회 2,849
16-02-01 조회 2,649
16-02-01 조회 2,631
16-01-31 조회 2,664
16-01-31 조회 2,673
16-01-31 조회 2,682
16-01-30 조회 3,083
16-01-30 조회 2,674
16-01-30 조회 2,673
16-01-30 조회 2,865
16-01-30 조회 2,960
16-01-30 조회 2,781
16-01-29 조회 2,661
16-01-29 조회 2,663
16-01-29 조회 2,554
16-01-29 조회 2,547
16-01-28 조회 2,974
16-01-28 조회 2,702
16-01-28 조회 2,499
16-01-28 조회 2,406
16-01-28 조회 2,643
16-01-27 조회 2,549
16-01-27 조회 2,546
16-01-27 조회 2,567
16-01-26 조회 2,545
16-01-26 조회 2,524
16-01-26 조회 2,539
16-01-26 조회 2,631
16-01-25 조회 2,553
16-01-25 조회 2,593
16-01-25 조회 2,603
16-01-25 조회 2,471
16-01-24 조회 2,471
16-01-24 조회 2,472
16-01-24 조회 2,392
16-01-23 조회 2,441
16-01-23 조회 2,464
16-01-23 조회 2,435
16-01-22 조회 2,570
16-01-22 조회 2,617
16-01-22 조회 2,573
16-01-22 조회 2,660
16-01-21 조회 2,690
16-01-21 조회 2,575
16-01-21 조회 2,549
16-01-21 조회 2,571
16-01-21 조회 2,526
16-01-20 조회 2,552
16-01-20 조회 2,468
16-01-20 조회 2,419
16-01-20 조회 2,426
16-01-19 조회 2,472
16-01-19 조회 2,500
16-01-19 조회 2,481
16-01-19 조회 2,522
16-01-19 조회 2,526
16-01-18 조회 2,499
16-01-18 조회 2,497
16-01-18 조회 2,512
16-01-18 조회 2,642
16-01-17 조회 2,675
16-01-17 조회 2,518
16-01-17 조회 2,610
16-01-16 조회 2,437
16-01-16 조회 2,370
16-01-16 조회 2,434
16-01-15 조회 2,591
16-01-15 조회 2,597
16-01-15 조회 2,517
16-01-15 조회 2,455
16-01-14 조회 2,596
16-01-14 조회 2,727
16-01-14 조회 3,167
16-01-14 조회 2,488
16-01-14 조회 2,727
16-01-14 조회 2,537
16-01-13 조회 2,784
16-01-13 조회 2,586
16-01-13 조회 2,551
16-01-13 조회 2,566
16-01-13 조회 2,459
16-01-12 조회 2,894
16-01-12 조회 2,641
16-01-12 조회 2,655
16-01-12 조회 2,570
16-01-11 조회 2,704