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

· 10년 전 · 616

<?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년 전 조회 728
9년 전 조회 575
9년 전 조회 779
9년 전 조회 669
9년 전 조회 683
9년 전 조회 748
9년 전 조회 697
9년 전 조회 709
9년 전 조회 678
9년 전 조회 1,047
9년 전 조회 1,082
9년 전 조회 1,070
9년 전 조회 1,014
9년 전 조회 805
9년 전 조회 965
9년 전 조회 1,086
9년 전 조회 785
9년 전 조회 1,078
10년 전 조회 1,036
10년 전 조회 1,447
10년 전 조회 1,051
10년 전 조회 1,244
10년 전 조회 877
10년 전 조회 1,036
10년 전 조회 748
10년 전 조회 778
10년 전 조회 897
10년 전 조회 864
10년 전 조회 1,021
10년 전 조회 758
10년 전 조회 817
10년 전 조회 792
10년 전 조회 699
10년 전 조회 809
10년 전 조회 655
10년 전 조회 706
10년 전 조회 636
10년 전 조회 724
10년 전 조회 703
10년 전 조회 1,151
10년 전 조회 675
10년 전 조회 604
10년 전 조회 865
10년 전 조회 688
10년 전 조회 658
10년 전 조회 664
10년 전 조회 616
10년 전 조회 561
10년 전 조회 539
10년 전 조회 617
10년 전 조회 635
10년 전 조회 583
10년 전 조회 526
10년 전 조회 619
10년 전 조회 586
10년 전 조회 595
10년 전 조회 651
10년 전 조회 661
10년 전 조회 551
10년 전 조회 590
10년 전 조회 651
10년 전 조회 588
10년 전 조회 679
10년 전 조회 622
10년 전 조회 569
10년 전 조회 537
10년 전 조회 574
10년 전 조회 530
10년 전 조회 560
10년 전 조회 597
10년 전 조회 535
10년 전 조회 577
10년 전 조회 548
10년 전 조회 636
10년 전 조회 743
10년 전 조회 695
10년 전 조회 672
10년 전 조회 568
10년 전 조회 671
10년 전 조회 578
10년 전 조회 609
10년 전 조회 578
10년 전 조회 556
10년 전 조회 543
10년 전 조회 577
10년 전 조회 658
10년 전 조회 605
10년 전 조회 681
10년 전 조회 582
10년 전 조회 608
10년 전 조회 602
10년 전 조회 645
10년 전 조회 733
10년 전 조회 822
10년 전 조회 596
10년 전 조회 670
10년 전 조회 579
10년 전 조회 536
10년 전 조회 531
10년 전 조회 687