using
System;
using
System.Collections.Generic;
class
Program {
static
List<
int
>
SortArrayInDescendingOrder(List<
int
> arr)
{
PriorityQueue<
int
> minHeap
=
new
PriorityQueue<
int
>();
foreach
(
int
num
in
arr) { minHeap.Push(num); }
List<
int
> result =
new
List<
int
>();
while
(minHeap.Count > 0) {
int
top = minHeap.Top;
minHeap.Pop();
result.Insert(0, top);
}
return
result;
}
static
void
Main(
string
[] args)
{
List<
int
> arr =
new
List<
int
>{ 4, 6, 3, 2, 9 };
List<
int
> result = SortArrayInDescendingOrder(arr);
foreach
(
int
num
in
result)
{
Console.Write(num +
" "
);
}
Console.WriteLine();
}
}
class
PriorityQueue<T>
where
T : IComparable<T> {
private
List<T> _heap;
public
int
Count
{
get
{
return
_heap.Count; }
}
public
T Top
{
get
{
return
_heap[0]; }
}
public
PriorityQueue() { _heap =
new
List<T>(); }
public
void
Push(T item)
{
_heap.Add(item);
int
i = _heap.Count - 1;
while
(i > 0
&& _heap[(i - 1) / 2].CompareTo(_heap[i])
> 0) {
Swap(i, (i - 1) / 2);
i = (i - 1) / 2;
}
}
public
void
Pop()
{
if
(_heap.Count == 0)
throw
new
InvalidOperationException(
"PriorityQueue is empty"
);
int
lastIndex = _heap.Count - 1;
_heap[0] = _heap[lastIndex];
_heap.RemoveAt(lastIndex);
lastIndex--;
int
i = 0;
while
(
true
) {
int
leftChild = 2 * i + 1;
int
rightChild = 2 * i + 2;
if
(leftChild > lastIndex)
break
;
int
j = leftChild;
if
(rightChild <= lastIndex
&& _heap[rightChild].CompareTo(
_heap[leftChild])
< 0) {
j = rightChild;
}
if
(_heap[i].CompareTo(_heap[j]) <= 0)
break
;
Swap(i, j);
i = j;
}
}
private
void
Swap(
int
i,
int
j)
{
T temp = _heap[i];
_heap[i] = _heap[j];
_heap[j] = temp;
}
}