슈

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

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

<?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-11 조회 3,054
16-02-15 조회 3,286
16-02-02 조회 2,951
16-01-25 조회 2,575
16-01-18 조회 2,672
16-01-08 조회 2,597
16-01-07 조회 2,608
16-01-05 조회 2,646
16-01-05 조회 2,941
16-01-05 조회 2,768
16-01-05 조회 2,794
16-01-04 조회 2,656
16-01-04 조회 2,660
16-01-04 조회 2,615
16-01-04 조회 2,525
15-12-30 조회 2,878
15-12-24 조회 2,693
15-12-24 조회 2,687
15-12-24 조회 2,713
15-12-24 조회 2,673
15-12-23 조회 2,657
15-12-16 조회 2,747
15-12-15 조회 2,814
15-12-11 조회 2,939
15-12-10 조회 2,798
15-11-09 조회 2,785
15-11-09 조회 3,008
15-10-06 조회 3,339
15-10-02 조회 3,118
15-05-29 조회 4,450
15-02-02 조회 4,431
15-01-18 조회 4,407
14-10-25 조회 8,372
14-10-25 조회 5,147
14-10-22 조회 7,150
14-10-22 조회 4,854
14-10-22 조회 4,809
14-10-18 조회 5,681
14-10-17 조회 6,053
14-10-17 조회 5,433
14-10-17 조회 8,924
14-10-17 조회 6,445
14-10-13 조회 4,365
14-10-13 조회 4,466
14-10-07 조회 6,148
14-09-24 조회 3,914
14-09-23 조회 3,982
14-09-22 조회 4,884
14-09-22 조회 6,113
14-09-22 조회 4,085
14-09-22 조회 3,758
14-09-21 조회 4,365
14-09-21 조회 3,845
14-09-20 조회 3,848
14-09-20 조회 3,756
14-09-20 조회 3,932
14-09-19 조회 3,867
14-09-19 조회 4,023
14-09-19 조회 3,916
14-09-19 조회 4,301
14-09-19 조회 3,934
14-09-19 조회 3,864
14-09-19 조회 4,147
14-09-19 조회 3,884
14-09-19 조회 3,933
14-09-19 조회 3,914
14-09-18 조회 3,995
14-09-18 조회 3,950
14-09-18 조회 4,221
14-09-18 조회 4,186
14-09-18 조회 4,198
14-09-18 조회 4,899
14-09-18 조회 5,449
14-09-18 조회 5,337
14-09-18 조회 4,134
14-09-18 조회 4,629
14-09-18 조회 4,257
14-08-30 조회 5,483
14-08-06 조회 6,007
14-08-06 조회 5,619
14-08-03 조회 5,268
14-06-12 조회 5,538
14-05-11 조회 3,716
14-05-11 조회 4,480
14-05-11 조회 4,588
14-05-09 조회 1.3만
14-05-01 조회 5,173
14-03-30 조회 4,388
14-03-23 조회 8,008
14-02-12 조회 3,886
13-10-29 조회 7,851
13-10-28 조회 8,680
13-09-29 조회 6,644
13-09-29 조회 4,273
13-07-23 조회 5,145
13-07-23 조회 1.2만
13-07-14 조회 4,682
13-07-14 조회 5,070
13-07-14 조회 6,026
13-07-14 조회 5,305