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

· 10년 전 · 545

<?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년 전 조회 648
9년 전 조회 517
9년 전 조회 713
9년 전 조회 605
9년 전 조회 623
9년 전 조회 671
9년 전 조회 625
9년 전 조회 624
9년 전 조회 608
9년 전 조회 976
9년 전 조회 1,017
9년 전 조회 987
9년 전 조회 951
9년 전 조회 729
9년 전 조회 886
9년 전 조회 1,015
9년 전 조회 717
9년 전 조회 1,002
9년 전 조회 955
9년 전 조회 1,378
10년 전 조회 987
10년 전 조회 1,173
10년 전 조회 817
10년 전 조회 963
10년 전 조회 695
10년 전 조회 701
10년 전 조회 814
10년 전 조회 780
10년 전 조회 965
10년 전 조회 679
10년 전 조회 747
10년 전 조회 729
10년 전 조회 633
10년 전 조회 734
10년 전 조회 588
10년 전 조회 639
10년 전 조회 572
10년 전 조회 665
10년 전 조회 643
10년 전 조회 1,097
10년 전 조회 606
10년 전 조회 536
10년 전 조회 801
10년 전 조회 637
10년 전 조회 602
10년 전 조회 587
10년 전 조회 555
10년 전 조회 495
10년 전 조회 473
10년 전 조회 546
10년 전 조회 560
10년 전 조회 516
10년 전 조회 459
10년 전 조회 545
10년 전 조회 536
10년 전 조회 527
10년 전 조회 597
10년 전 조회 599
10년 전 조회 492
10년 전 조회 529
10년 전 조회 588
10년 전 조회 525
10년 전 조회 610
10년 전 조회 555
10년 전 조회 498
10년 전 조회 472
10년 전 조회 505
10년 전 조회 485
10년 전 조회 496
10년 전 조회 523
10년 전 조회 459
10년 전 조회 509
10년 전 조회 467
10년 전 조회 572
10년 전 조회 671
10년 전 조회 624
10년 전 조회 587
10년 전 조회 502
10년 전 조회 609
10년 전 조회 518
10년 전 조회 549
10년 전 조회 502
10년 전 조회 480
10년 전 조회 471
10년 전 조회 510
10년 전 조회 578
10년 전 조회 527
10년 전 조회 612
10년 전 조회 511
10년 전 조회 543
10년 전 조회 519
10년 전 조회 583
10년 전 조회 656
10년 전 조회 754
10년 전 조회 527
10년 전 조회 598
10년 전 조회 518
10년 전 조회 461
10년 전 조회 454
10년 전 조회 628