Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Basic Heap Sort

Basic Heap Sort

I. Concept and Introduction

Heap sort (Heapsort) refers to a sorting algorithm designed using the heap data structure.

A heap is a structure approximating a complete binary tree, and it also satisfies the heap property: the key or index of a child node is always smaller (or larger) than that of its parent.

II. Applicability Notes

Our previous process of building a heap called the insert method for each data item, inserting them one by one using shift up. The time complexity of this algorithm is O(nlogn). This section introduces a process of building a heap for sorting, called Heapify, with algorithm time complexity of O(n)。

III. Process diagram

A complete binary tree has an important property: the index of the first non-leaf node is n/2 the index value obtained by taking the integer part, where n is the number of elements (provided the array index starts at 1).

Position index 5 is the first non-leaf node. Starting from it and moving forward one by one, we treat each element as the root node and perform a shift down operation to satisfy the maximum heap property.

After performing the shift down operation at position index 5, 22 and 62 swap positions.

Perform shift down operation on the element at index 4

Perform shift down operation on the element at index 3

Perform shift down operation on the element at index 2

Finally, perform a shift down operation on the root node, and the entire heap sort process is complete.

IV. Java Example Code

Source code package download:Download

src/runoob/heap/Heapify.java file code:

package

runoob.heap

;

import

runoob.sort.SortTestHelper

;

/**
 * Heap sort using heapify
 */

public

class

Heapify

<

T

extends

Comparable

{

protected

T

[

]

data

;

   

protected

int

count

;

   

protected

int

capacity

;

   

// Constructor, create a maximum heap from a given array

   

// The process of building this heap has time complexity O(n)

   

public

Heapify

(

T arr

[

]

)

{

       

int

n

=

arr.

length

;

        data

=

(

T

[

]

)

new

Comparable

[

n

1

]

;

        capacity

=

n

;

       

for

(

int

i

=

0

;

i

<

n

;

i

++

)

            data

[

i

1

]

=

arr

[

i

]

;

        count

=

n

;

       

// Start from the first element that is not a leaf node

       

for

(

int

i

=

count

/

2

;

i

=

1

;

i

--

)

            shiftDown

(

i

)

;

   

}

   

// Return the number of elements in the heap

   

public

int

size

(

)

{

       

return

count

;

   

}

   

// Return a boolean value indicating whether the heap is empty

   

public

boolean

isEmpty

(

)

{

       

return

count

==

0

;

   

}

   

// Insert a new element item into the maximum heap

   

public

void

insert

(

T item

)

{

       

assert

count

1

<=

capacity

;

        data

[

count

1

]

=

item

;

        count

++;

        shiftUp

(

count

)

;

   

}

   

// Take the top element from the maximum heap, i.e., the maximum data stored in the heap

   

public

T extractMax

(

)

{

       

assert

count

0

;

        T ret

=

data

[

1

]

;

        swap

(

1

, count

)

;

        count

--;

        shiftDown

(

1

)

;

       

return

ret

;

   

}

   

// Get the top element of the maximum heap

   

public

T getMax

(

)

{

       

assert

(

count

0

)

;

       

return

data

[

1

]

;

   

}

   

// Swap the two elements with indices i and j in the heap

   

private

void

swap

(

int

i,

int

j

)

{

        T t

=

data

[

i

]

;

        data

[

i

]

=

data

[

j

]

;

        data

[

j

]

=

t

;

   

}

   

//********************

   

//* Core helper function of the maximum heap

   

//********************

   

private

void

shiftUp

(

int

k

)

{

       

while

(

k

1

&&

data

[

k

/

2

]

.

compareTo

(

data

[

k

]

)

<

0

)

{

swap

(

k, k

/

2

)

;

            k

/=

2

;

       

}

   

}

   

private

void

shiftDown

(

int

k

)

{

       

while

(

2

k

<=

count

)

{

           

int

j

=

2

k

;

// In this loop iteration, data[k] and data[j] swap positions

           

if

(

j

1

<=

count

&&

data

[

j

1

]

.

compareTo

(

data

[

j

]

)

0

)

                j

++;

           

// data[j] is the maximum of data[2*k] and data[2*k+1]

           

if

(

data

[

k

]

.

compareTo

(

data

[

j

]

)

=

0

)

break

;

            swap

(

k, j

)

;

            k

=

j

;

       

}

   

}

   

// Test heapify

   

public

static

void

main

(

String

[

]

args

)

{

       

int

N

=

100

;

       

Integer

[

]

arr

=

SortTestHelper.

generateRandomArray

(

N,

0

,

100000

)

;

        Heapify

<

Integer

heapify

=

new

Heapify

<

Integer

(

arr

)

;

       

// Gradually take the data out of heapify using extractMax

       

// The order of taking out should be from largest to smallest

       

for

(

int

i

=

0

;

i

<

N

;

i

++

)

{

            arr

[

i

]

=

heapify.

extractMax

(

)

;

           

System

.

out

.

print

(

arr

[

i

]

" "

)

;

       

}

       

// Ensure the arr array is arranged from largest to smallest

       

for

(

int

i

=

1

;

i

<

N

;

i

++

)

           

assert

arr

[

i

1

]

=

arr

[

i

]

;

   

}

}