슈

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

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

<?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-30 조회 2,714
16-05-11 조회 2,944
16-03-21 조회 3,073
16-03-19 조회 3,031
16-03-17 조회 2,854
16-03-17 조회 2,930
16-03-14 조회 2,943
16-03-13 조회 2,884
16-03-09 조회 2,955
16-03-08 조회 2,920
16-03-08 조회 2,693
16-03-08 조회 2,771
16-03-08 조회 2,936
16-03-07 조회 2,747
16-03-07 조회 2,729
16-02-29 조회 3,067
16-02-25 조회 3,129
16-02-22 조회 2,939
16-02-12 조회 3,216
16-02-04 조회 3,053
16-02-04 조회 3,321
16-02-04 조회 2,877
16-02-03 조회 3,134
16-02-03 조회 2,941
16-02-02 조회 2,888
16-02-02 조회 2,777
16-02-01 조회 2,780
16-01-30 조회 2,617
16-01-30 조회 2,797
16-01-30 조회 2,875
16-01-30 조회 2,668
16-01-29 조회 2,610
16-01-28 조회 2,690
16-01-28 조회 2,533
16-01-27 조회 2,501
16-01-26 조회 2,458
16-01-26 조회 2,561
16-01-25 조회 2,533
16-01-22 조회 2,501
16-01-22 조회 2,590
16-01-21 조회 2,496
16-01-19 조회 2,404
16-01-19 조회 2,464
16-01-18 조회 2,431
16-01-17 조회 2,602
16-01-14 조회 2,541
16-01-13 조회 2,721
16-01-12 조회 2,665
16-01-12 조회 2,589
16-01-11 조회 2,348
15-12-30 조회 3,099
15-12-29 조회 2,540
15-12-28 조회 2,578
15-12-09 조회 2,642
15-12-08 조회 2,848
15-12-08 조회 2,605
15-12-08 조회 2,884
15-12-07 조회 2,684
15-12-07 조회 3,043
15-12-07 조회 2,713
15-12-06 조회 2,845
15-12-06 조회 2,541
15-12-06 조회 2,763
15-12-05 조회 2,695
15-12-05 조회 2,946
15-12-05 조회 2,750
15-12-04 조회 2,612
15-12-04 조회 2,721
15-12-04 조회 2,766
15-12-03 조회 2,724
15-12-03 조회 2,759
15-12-03 조회 2,768
15-12-02 조회 2,857
15-12-02 조회 2,803
15-12-02 조회 2,924
15-12-02 조회 2,972
15-12-01 조회 2,699
15-12-01 조회 2,491
15-12-01 조회 2,569
15-11-30 조회 2,761
15-11-30 조회 2,888
15-11-30 조회 2,807
15-11-29 조회 2,587
15-11-29 조회 2,815
15-11-29 조회 2,649
15-11-28 조회 2,586
15-11-28 조회 2,603
15-11-28 조회 2,645
15-11-27 조회 2,743
15-11-27 조회 2,589
15-11-27 조회 2,599
15-11-25 조회 2,651
15-11-25 조회 2,705
15-11-25 조회 2,510
15-11-23 조회 2,796
15-11-23 조회 2,922
15-11-23 조회 2,767
15-11-21 조회 2,669
15-11-21 조회 2,575
15-11-21 조회 2,518