MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Heapsort
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
void up(int []t, int L)
{
int temp;
int index = L;
while (index > 0)
{
int parent = (index - 1) / 2;
if ( t[parent] > t[index] )
{
temp = t[parent];
t[parent] = t[index];
t[index] = temp;
index = parent;
}
else break;
}
}
void makeInput(int x, int []t, ref int L)
{
t[L] = x;
up(t, L);
L++;
}
void down(int []t, int L)
{
int i = 0;
while(true)
{
int p = 2 * i + 1; // left descendant of the root i
if ( p > L )
break;
if ( p + 1 <= L )
if ( t[p] > t[p+1] )
p++;
if ( t[i] < t[p] )
break;
int temp = t[p];
t[p] = t[i];
t[i] = temp;
i = p;
}
}
int takeRoot(int []t, ref int L)
{
int x = t[0];
L--;
t[0] = t[L];
down(t, L);
return x;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
int i;
string[] stringArray = string1.Text.Split(' ');
int lenghtArray = stringArray.Length;
resultstring.Text = Convert.ToString(lenghtArray);
int[] numberArray;
numberArray = new int[lenghtArray];
for (i = 0; i < lenghtArray; i++)
{
numberArray[i] = Convert.ToInt32(stringArray[i]);
}
int L = 0;
int[] t = new int[lenghtArray];
for (i = 0; i < lenghtArray; i++)
makeInput(numberArray[i], t, ref L);
for (i = 0; i < lenghtArray; i++)
numberArray[i] = takeRoot(t, ref L);
string result = "";
for (i = 0; i < lenghtArray; i++)
{
result += (Convert.ToString(numberArray[i])) + " ";
}
resultstring.Text = result;
}
}
}