Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PHP 7 Data Structures and Algorithms

You're reading from   PHP 7 Data Structures and Algorithms Implement linked lists, stacks, and queues using PHP

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781786463890
Length 340 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Mizanur Rahman Mizanur Rahman
Author Profile Icon Mizanur Rahman
Mizanur Rahman
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Introduction to Data Structures and Algorithms FREE CHAPTER 2. Understanding PHP Arrays 3. Using Linked Lists 4. Constructing Stacks and Queues 5. Applying Recursive Algorithms - Recursion 6. Understanding and Implementing Trees 7. Using Sorting Algorithms 8. Exploring Search Options 9. Putting Graphs into Action 10. Understanding and Using Heaps 11. Solving Problems with Advanced Techniques 12. PHP Built-In Support for Data Structures and Algorithms 13. Functional Data Structures with PHP

Using heap sort

Heap sort requires us to build a heap from a given list of elements and then continuously checks the heap property so that the whole heap remains sorted all the time. Unlike a regular heap where we stop checking the heap property once the newly inserted value satisfies the conditions, we continue to do so for the next elements during the heap sort implementation. The pseudocode of the heap sort looks like this:

Heapsort(A as array) 
BuildHeap(A)
for i = n-1 to 0
swap(A[0], A[i])
n = n - 1
Heapify(A, 0)

BuildHeap(A as array)
n = elements_in(A)
for i = floor(n/2) to 0
Heapify(A,i)

Heapify(A as array, i as int)
left = 2i+1
right = 2i+2
max = i

if (left <= n) and (A[left] > A[i])
max = left

if (right<=n) and (A[right] > A[max])
max = right

if (max != i)
swap(A...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime