슈

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

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

<?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-06-03 조회 3,001
15-07-30 조회 3,399
15-06-08 조회 5,544
15-05-27 조회 4,403
15-01-08 조회 6,695
14-12-30 조회 5,428
14-10-10 조회 5,420
14-08-13 조회 6,332
14-08-11 조회 5,192
14-08-06 조회 6,527
14-03-19 조회 7,422
13-04-26 조회 9,582
12-12-15 조회 5,101
12-12-15 조회 7,337
12-12-11 조회 5,468
12-11-21 조회 6,000
12-10-11 조회 5,082
12-10-03 조회 4,776
12-09-20 조회 6,078
12-09-02 조회 4,255
12-08-29 조회 5,254
12-08-29 조회 4,140
12-08-24 조회 4,240
12-08-18 조회 4,469
12-08-17 조회 4,508
12-08-16 조회 3,764
12-08-06 조회 5,540
12-08-05 조회 4,829
12-08-03 조회 4,545
12-08-03 조회 4,686
12-07-29 조회 3,546
12-07-24 조회 4,132
12-07-22 조회 3,603
12-07-20 조회 4,356
12-07-19 조회 4,705
12-07-19 조회 3,938
12-07-19 조회 3,883
12-07-19 조회 5,548
12-07-19 조회 3,492
12-07-18 조회 5,174
12-07-15 조회 4,145
12-07-14 조회 3,825
12-07-12 조회 4,501
12-07-11 조회 3,774
12-07-11 조회 3,496
12-07-11 조회 3,462
12-07-10 조회 4,643
12-07-09 조회 3,396
12-07-09 조회 4,223
12-07-09 조회 4,638
12-07-08 조회 4,558
12-07-07 조회 5,480
12-07-06 조회 4,737
12-07-06 조회 4,007
12-07-06 조회 3,891
12-07-06 조회 3,819
12-07-02 조회 5,499
12-07-02 조회 4,199
12-06-29 조회 3,723
12-06-28 조회 4,349
12-06-26 조회 3,842
12-06-26 조회 4,963
12-06-21 조회 3,938
12-06-20 조회 4,643
12-06-20 조회 4,270
12-06-15 조회 4,625
12-06-15 조회 4,360
12-06-13 조회 7,378
12-06-05 조회 7,353
12-06-04 조회 3,933
12-06-03 조회 3,777
12-05-31 조회 3,779
12-05-28 조회 6,621
12-05-27 조회 3,855
12-05-25 조회 3,991
12-05-23 조회 4,401
12-05-21 조회 5,108
12-05-17 조회 4,047
12-05-17 조회 3,970
12-05-09 조회 4,042
12-05-07 조회 4,293
12-05-02 조회 4,487
12-05-01 조회 4,661
12-04-30 조회 3,407
12-04-30 조회 4,352
12-04-25 조회 4,437
12-04-25 조회 4,866
12-04-24 조회 3,832
12-04-24 조회 4,599
12-04-23 조회 3,603
12-04-20 조회 3,242
12-04-17 조회 5,431
12-04-16 조회 4,372
12-04-14 조회 3,655
12-04-14 조회 3,683
12-04-14 조회 3,735
12-04-13 조회 3,356
12-04-13 조회 4,542
12-04-13 조회 6,180
12-04-12 조회 3,345