슈

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

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

<?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,047
16-02-04 조회 3,315
16-02-04 조회 2,874
16-02-03 조회 3,126
16-02-03 조회 2,763
16-02-03 조회 2,912
16-02-03 조회 2,836
16-02-03 조회 2,937
16-02-02 조회 2,979
16-02-02 조회 2,846
16-02-02 조회 2,870
16-02-02 조회 2,771
16-02-02 조회 2,681
16-02-01 조회 2,776
16-02-01 조회 2,486
16-02-01 조회 2,569
16-01-31 조회 2,565
16-01-31 조회 2,624
16-01-31 조회 2,624
16-01-30 조회 3,030
16-01-30 조회 2,615
16-01-30 조회 2,590
16-01-30 조회 2,787
16-01-30 조회 2,681
16-01-30 조회 2,653
16-01-29 조회 2,604
16-01-29 조회 2,598
16-01-29 조회 2,472
16-01-29 조회 2,416
16-01-28 조회 2,679
16-01-28 조회 2,509
16-01-28 조회 2,429
16-01-28 조회 2,345
16-01-28 조회 2,503
16-01-27 조회 2,485
16-01-27 조회 2,483
16-01-27 조회 2,496
16-01-26 조회 2,469
16-01-26 조회 2,437
16-01-26 조회 2,453
16-01-26 조회 2,552
16-01-25 조회 2,460
16-01-25 조회 2,524
16-01-25 조회 2,526
16-01-25 조회 2,396
16-01-24 조회 2,396
16-01-24 조회 2,412
16-01-24 조회 2,332
16-01-23 조회 2,383
16-01-23 조회 2,411
16-01-23 조회 2,375
16-01-22 조회 2,480
16-01-22 조회 2,343
16-01-22 조회 2,483
16-01-22 조회 2,583
16-01-21 조회 2,602
16-01-21 조회 2,498
16-01-21 조회 2,403
16-01-21 조회 2,488
16-01-21 조회 2,443
16-01-20 조회 2,468
16-01-20 조회 2,381
16-01-20 조회 2,360
16-01-20 조회 2,332
16-01-19 조회 2,394
16-01-19 조회 2,450
16-01-19 조회 2,395
16-01-19 조회 2,460
16-01-19 조회 2,455
16-01-18 조회 2,423
16-01-18 조회 2,420
16-01-18 조회 2,434
16-01-18 조회 2,552
16-01-17 조회 2,595
16-01-17 조회 2,428
16-01-17 조회 2,530
16-01-16 조회 2,315
16-01-16 조회 2,306
16-01-16 조회 2,326
16-01-15 조회 2,466
16-01-15 조회 2,347
16-01-15 조회 2,462
16-01-15 조회 2,407
16-01-14 조회 2,538
16-01-14 조회 2,625
16-01-14 조회 3,096
16-01-14 조회 2,424
16-01-14 조회 2,661
16-01-14 조회 2,474
16-01-13 조회 2,717
16-01-13 조회 2,479
16-01-13 조회 2,452
16-01-13 조회 2,399
16-01-13 조회 2,385
16-01-12 조회 2,661
16-01-12 조회 2,582
16-01-12 조회 2,370
16-01-12 조회 2,491
16-01-11 조회 2,644