테스트 사이트 - 개발 중인 베타 버전입니다

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

· 9년 전 · 310

<?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);

?>


댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

게시글 목록

번호 제목
8265
20403
20402
20401
20400
8259
20399
20398
8255
8249
8246
8242
20396
8240
20395
20394
31033
28385
20393
28380
20392
28377
20391
20390
20389
20388
20387
20386
20385
20384
20383
20382
8238
20380
20379
28376
28372
20378
20377
8235
20375
20374
20373
20372
20371
20370
28370
20369
28366
20368
20367
20366
20365
20364
20363
20362
20361
20360
20359
8231
20358
20357
20356
20355
20354
20353
20352
20351
20350
20349
20348
20347
20346
20345
20344
20343
20342
20341
20340
20339
20338
20337
20336
20335
20334
20333
20332
20331
20330
20329
20328
20327
20326
20325
20324
20323
20322
20321
20320
20319