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

· 10년 전 · 964

<?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);

?>


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

프로그램

+
제목 글쓴이 날짜 조회
9년 전 조회 1,129
9년 전 조회 976
9년 전 조회 1,177
9년 전 조회 1,029
9년 전 조회 1,046
9년 전 조회 1,112
9년 전 조회 1,076
9년 전 조회 1,081
9년 전 조회 1,020
9년 전 조회 1,406
10년 전 조회 1,485
10년 전 조회 1,435
10년 전 조회 1,367
10년 전 조회 1,171
10년 전 조회 1,319
10년 전 조회 1,456
10년 전 조회 1,126
10년 전 조회 1,493
10년 전 조회 1,425
10년 전 조회 1,790
10년 전 조회 1,402
10년 전 조회 1,608
10년 전 조회 1,244
10년 전 조회 1,405
10년 전 조회 1,103
10년 전 조회 1,151
10년 전 조회 1,234
10년 전 조회 1,220
10년 전 조회 1,349
10년 전 조회 1,178
10년 전 조회 1,191
10년 전 조회 1,153
10년 전 조회 1,094
10년 전 조회 1,173
10년 전 조회 1,015
10년 전 조회 1,060
10년 전 조회 971
10년 전 조회 1,093
10년 전 조회 1,059
10년 전 조회 1,523
10년 전 조회 1,016
10년 전 조회 975
10년 전 조회 1,210
10년 전 조회 1,044
10년 전 조회 1,008
10년 전 조회 1,023
10년 전 조회 988
10년 전 조회 922
10년 전 조회 889
10년 전 조회 965
10년 전 조회 989
10년 전 조회 927
10년 전 조회 855
10년 전 조회 921
10년 전 조회 881
10년 전 조회 918
10년 전 조회 963
10년 전 조회 972
10년 전 조회 899
10년 전 조회 915
10년 전 조회 972
10년 전 조회 977
10년 전 조회 1,026
10년 전 조회 949
10년 전 조회 891
10년 전 조회 877
10년 전 조회 897
10년 전 조회 857
10년 전 조회 887
10년 전 조회 925
10년 전 조회 878
10년 전 조회 922
10년 전 조회 885
10년 전 조회 977
10년 전 조회 1,082
10년 전 조회 1,034
10년 전 조회 984
10년 전 조회 904
10년 전 조회 1,002
10년 전 조회 916
10년 전 조회 941
10년 전 조회 906
10년 전 조회 894
10년 전 조회 880
10년 전 조회 907
10년 전 조회 1,003
10년 전 조회 939
10년 전 조회 1,025
10년 전 조회 906
10년 전 조회 941
10년 전 조회 926
10년 전 조회 980
10년 전 조회 1,123
10년 전 조회 1,145
10년 전 조회 948
10년 전 조회 1,018
10년 전 조회 916
10년 전 조회 851
10년 전 조회 895
10년 전 조회 1,024