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

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

<?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,660
16-05-11 조회 2,868
16-03-21 조회 3,010
16-03-19 조회 2,945
16-03-17 조회 2,782
16-03-17 조회 2,832
16-03-14 조회 2,872
16-03-13 조회 2,760
16-03-09 조회 2,875
16-03-08 조회 2,815
16-03-08 조회 2,616
16-03-08 조회 2,656
16-03-08 조회 2,815
16-03-07 조회 2,622
16-03-07 조회 2,671
16-02-29 조회 2,997
16-02-25 조회 3,021
16-02-22 조회 2,850
16-02-12 조회 3,068
16-02-04 조회 3,014
16-02-04 조회 3,244
16-02-04 조회 2,828
16-02-03 조회 3,086
16-02-03 조회 2,850
16-02-02 조회 2,772
16-02-02 조회 2,725
16-02-01 조회 2,697
16-01-30 조회 2,556
16-01-30 조회 2,732
16-01-30 조회 2,627
16-01-30 조회 2,606
16-01-29 조회 2,530
16-01-28 조회 2,581
16-01-28 조회 2,424
16-01-27 조회 2,457
16-01-26 조회 2,395
16-01-26 조회 2,468
16-01-25 조회 2,463
16-01-22 조회 2,389
16-01-22 조회 2,502
16-01-21 조회 2,451
16-01-19 조회 2,309
16-01-19 조회 2,408
16-01-18 조회 2,376
16-01-17 조회 2,512
16-01-14 조회 2,493
16-01-13 조회 2,667
16-01-12 조회 2,615
16-01-12 조회 2,559
16-01-11 조회 2,253
15-12-30 조회 3,038
15-12-29 조회 2,477
15-12-28 조회 2,526
15-12-09 조회 2,597
15-12-08 조회 2,813
15-12-08 조회 2,572
15-12-08 조회 2,851
15-12-07 조회 2,635
15-12-07 조회 3,011
15-12-07 조회 2,678
15-12-06 조회 2,809
15-12-06 조회 2,513
15-12-06 조회 2,727
15-12-05 조회 2,632
15-12-05 조회 2,897
15-12-05 조회 2,712
15-12-04 조회 2,585
15-12-04 조회 2,658
15-12-04 조회 2,729
15-12-03 조회 2,694
15-12-03 조회 2,734
15-12-03 조회 2,721
15-12-02 조회 2,831
15-12-02 조회 2,763
15-12-02 조회 2,886
15-12-02 조회 2,904
15-12-01 조회 2,666
15-12-01 조회 2,461
15-12-01 조회 2,535
15-11-30 조회 2,729
15-11-30 조회 2,851
15-11-30 조회 2,703
15-11-29 조회 2,550
15-11-29 조회 2,764
15-11-29 조회 2,617
15-11-28 조회 2,550
15-11-28 조회 2,575
15-11-28 조회 2,611
15-11-27 조회 2,710
15-11-27 조회 2,552
15-11-27 조회 2,565
15-11-25 조회 2,602
15-11-25 조회 2,627
15-11-25 조회 2,467
15-11-23 조회 2,714
15-11-23 조회 2,840
15-11-23 조회 2,710
15-11-21 조회 2,640
15-11-21 조회 2,532
15-11-21 조회 2,478