Skip to main content

Posts

Showing posts with the label heapify

Heap Sort

Heap Sort is interesting because the use of the 'heapify' method that creates a binary tree as a flat array. First, a binary tree, also known as a heap , is created, and then the same function is used to sort the elements. In a heap, the first node in the array at zero (0) is the top node of the binary tree. The next two (2) items are the two (2) subnodes of the top node, and so on.  For each node at a position (positionIndex), its 2 subnodes are in the following positions: int left = 2 * positionIndex + 1; int right = 2 *  positionIndex + 2; The CommonMethods.Swap() function is a reusable class , since swapping values by position is a common action during these example sorts. Class using System; namespace Algorithms {     class HeapSort     {         public int[]  Sort()         {             //creates array             int[] arrayTo...

Algorithms in a Nutshell

The following are some of the the chapters from Algorithm's in a Nutshell, from which I will produce code in .NET, specifically C#, VB.NET or F#, to clarify the principles for myself. Chapter 4. Sorting Algorithms ·        Section 4.1. Overview ·        Section 4.2. Insertion Sort ·        Section 4.3. Median Sort ·        Section 4.4. Quicksort ·        Section 4.5. Selection Sort ·        Section 4.6. Heap Sort ·        Section 4.7. Counting Sort ·        Section 4.8. Bucket Sort ·        Section 4.9. Criteria for Choosing a Sorting Algorithm ·        Section 4.10. References Chapter 5. Searching ·        Section 5.1....