black_friday/Assets/Scripts/PriorityQueue.cs
/// <summary>
/// Sorts the inner heap of the priority queue
/// </summary>
Sorts the inner heap of the priority queue
Sorts the inner heap of the priority queue
public void HeapSort()
        {
            int index = 0;
            while (true)
            {
                int leftChildIndex = (index << 1) + 1;
                int rightChildIndex = (index << 1) + 2;
                if (leftChildIndex > innerHeap.Count - 2)
                {
                    break;
                }
                bool greaterThanLeft = false;
                if (innerHeap[index].CompareTo(innerHeap[leftChildIndex]) > 0)
                {
                    greaterThanLeft = true;
                }
                if ((!greaterThanLeft && innerHeap[index].CompareTo(innerHeap[rightChildIndex]) > 0)
                    || (greaterThanLeft && innerHeap[leftChildIndex].CompareTo(innerHeap[rightChildIndex]) > 0))
                {
                    SwapFromIndices(index, rightChildIndex);
                    greaterThanLeft = false;
                    index = rightChildIndex;
                }
                else if (greaterThanLeft)
                {
                    SwapFromIndices(index, leftChildIndex);
                    index = leftChildIndex;
                }
                else
                {
                    break;
                }
            }
        }
public void HeapSort ( ) { int index = 0 ; while ( true ) { int leftChildIndex = ( index << 1 ) + 1 ; int rightChildIndex = ( index << 1 ) + 2 ; if ( leftChildIndex > innerHeap . Count - 2 ) { break ; } bool greaterThanLeft = false ; if ( innerHeap [ index ] . CompareTo ( innerHeap [ leftChildIndex ] ) > 0 ) { greaterThanLeft = true ; } if ( ( ! greaterThanLeft && innerHeap [ index ] . CompareTo ( innerHeap [ rightChildIndex ] ) > 0 ) || ( greaterThanLeft && innerHeap [ leftChildIndex ] . CompareTo ( innerHeap [ rightChildIndex ] ) > 0 ) ) { SwapFromIndices ( index , rightChildIndex ) ; greaterThanLeft = false ; index = rightChildIndex ; } else if ( greaterThanLeft ) { SwapFromIndices ( index , leftChildIndex ) ; index = leftChildIndex ; } else { break ; } } }
Sorts the inner heap of the priority queue
Sorts the inner heap of the priority queue
//Get the indicies of the left and right child of the current node //If the left child index is outside of range, then sorting is done //Check whether or not the current node has a value greater than the left child //If the left child is greater than the current node AND the right child is less than the current node //OR the left child is less than the current node and the left child is greater than the right child //If the left child is greater than the current node