슈

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

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

<?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-07-13 조회 2,159
16-07-12 조회 2,323
16-07-12 조회 2,352
16-07-11 조회 2,184
16-06-05 조회 2,891
16-06-03 조회 3,000
16-05-26 조회 2,920
15-09-22 조회 4,969
15-06-10 조회 4,811
14-10-25 조회 6,342
14-10-22 조회 8,970
14-09-21 조회 5,773
14-09-16 조회 5,052
14-08-22 조회 1.1만
14-08-13 조회 5,885
14-08-12 조회 5,424
14-08-12 조회 6,277
14-08-11 조회 6,912
14-06-12 조회 6,187
14-06-11 조회 5,090
14-05-09 조회 5,403
14-05-09 조회 6,363
14-05-09 조회 6,391
14-05-08 조회 5,726
14-04-17 조회 4,728
14-04-16 조회 4,557
14-04-15 조회 4,566
14-04-15 조회 4,519
14-04-15 조회 4,285
14-04-14 조회 4,263
14-04-14 조회 4,230
14-04-14 조회 4,614
14-04-14 조회 4,760
14-04-14 조회 4,746
14-04-13 조회 4,643
14-04-12 조회 4,208
14-04-12 조회 4,355
14-04-12 조회 4,464
14-04-11 조회 4,993
14-04-10 조회 4,721
14-04-10 조회 4,762
14-04-09 조회 4,913
14-04-09 조회 5,279
14-04-08 조회 6,943
13-10-30 조회 7,645
13-07-11 조회 5,106
13-03-28 조회 1.1만
13-02-27 조회 6,765
13-02-14 조회 5,580
13-01-10 조회 4,501
12-12-31 조회 5,074
12-12-20 조회 6,095
12-12-12 조회 5,255
12-12-07 조회 4,476
12-11-28 조회 4,828
12-11-22 조회 5,261
12-11-12 조회 5,630
12-09-22 조회 4,496
12-09-18 조회 4,689