슈

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

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

<?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-02-06 조회 3,498
16-02-04 조회 3,048
16-02-04 조회 3,319
16-02-04 조회 2,874
16-02-03 조회 3,129
16-02-03 조회 2,764
16-02-03 조회 2,913
16-02-03 조회 2,836
16-02-03 조회 2,938
16-02-02 조회 2,981
16-02-02 조회 2,847
16-02-02 조회 2,877
16-02-02 조회 2,774
16-02-02 조회 2,686
16-02-01 조회 2,776
16-02-01 조회 2,486
16-02-01 조회 2,571
16-01-31 조회 2,571
16-01-31 조회 2,627
16-01-31 조회 2,624
16-01-30 조회 3,030
16-01-30 조회 2,616
16-01-30 조회 2,594
16-01-30 조회 2,789
16-01-30 조회 2,681
16-01-30 조회 2,658
16-01-29 조회 2,605
16-01-29 조회 2,598
16-01-29 조회 2,476
16-01-29 조회 2,424
16-01-28 조회 2,684
16-01-28 조회 2,512
16-01-28 조회 2,431
16-01-28 조회 2,347
16-01-28 조회 2,506
16-01-27 조회 2,487
16-01-27 조회 2,486
16-01-27 조회 2,498
16-01-26 조회 2,473
16-01-26 조회 2,438
16-01-26 조회 2,453
16-01-26 조회 2,552
16-01-25 조회 2,461
16-01-25 조회 2,526
16-01-25 조회 2,527
16-01-25 조회 2,398
16-01-24 조회 2,398
16-01-24 조회 2,414
16-01-24 조회 2,333
16-01-23 조회 2,385
16-01-23 조회 2,413
16-01-23 조회 2,376
16-01-22 조회 2,482
16-01-22 조회 2,344
16-01-22 조회 2,487
16-01-22 조회 2,586
16-01-21 조회 2,605
16-01-21 조회 2,499
16-01-21 조회 2,404
16-01-21 조회 2,489
16-01-21 조회 2,446
16-01-20 조회 2,471
16-01-20 조회 2,381
16-01-20 조회 2,362
16-01-20 조회 2,335
16-01-19 조회 2,396
16-01-19 조회 2,450
16-01-19 조회 2,398
16-01-19 조회 2,460
16-01-19 조회 2,457
16-01-18 조회 2,426
16-01-18 조회 2,422
16-01-18 조회 2,436
16-01-18 조회 2,555
16-01-17 조회 2,597
16-01-17 조회 2,431
16-01-17 조회 2,532
16-01-16 조회 2,316
16-01-16 조회 2,307
16-01-16 조회 2,331
16-01-15 조회 2,467
16-01-15 조회 2,347
16-01-15 조회 2,464
16-01-15 조회 2,409
16-01-14 조회 2,539
16-01-14 조회 2,629
16-01-14 조회 3,097
16-01-14 조회 2,428
16-01-14 조회 2,664
16-01-14 조회 2,477
16-01-13 조회 2,717
16-01-13 조회 2,480
16-01-13 조회 2,453
16-01-13 조회 2,401
16-01-13 조회 2,388
16-01-12 조회 2,662
16-01-12 조회 2,582
16-01-12 조회 2,372
16-01-12 조회 2,492
16-01-11 조회 2,644