mathiaspl20

Untitled

Feb 24th, 2017
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.Foundation;
  7. using Windows.Foundation.Collections;
  8. using Windows.UI.Xaml;
  9. using Windows.UI.Xaml.Controls;
  10. using Windows.UI.Xaml.Controls.Primitives;
  11. using Windows.UI.Xaml.Data;
  12. using Windows.UI.Xaml.Input;
  13. using Windows.UI.Xaml.Media;
  14. using Windows.UI.Xaml.Navigation;
  15.  
  16. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
  17.  
  18. namespace Bubblesort
  19. {
  20. /// <summary>
  21. /// An empty page that can be used on its own or navigated to within a Frame.
  22. /// </summary>
  23. public sealed partial class MainPage : Page
  24. {
  25. int[] arr1 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
  26. int[] arr2 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
  27. int[] arr3 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
  28. int[] arr4 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
  29. String bubbleout = "";
  30. String mergeout = "";
  31. String quickout = "";
  32. String insertout = "";
  33.  
  34. Random randNum = new Random();
  35.  
  36.  
  37. public MainPage()
  38. {
  39. this.InitializeComponent();
  40. //Array wird mit ganzen Zahlen gefüllt
  41. for (int i = 0; i < arr1.Length; i++)
  42. {
  43. arr1[i] = randNum.Next(1, 100);
  44. arr2 = arr1;
  45. arr3 = arr1;
  46. arr4 = arr1;
  47. }
  48.  
  49. //Umwandeln des Arrays in einen String zur Ausgabe in einem Textfeld
  50. String inputstring = "";
  51.  
  52. for (int i = 0; i < arr1.Length; i++)
  53. {
  54. inputstring += arr1[i] + " | ";
  55. }
  56.  
  57. tbx_input.Text = inputstring;
  58.  
  59. }
  60.  
  61. //###QUICKSORT###//
  62. public int[] bubblesort(int[] a)
  63. {
  64. bool flag = true;
  65. int temp;
  66. int numLength = a.Length;
  67.  
  68. for (int i = 1;
  69. (i <= (numLength - 1)) && flag; i++)
  70. {
  71. flag = false;
  72. for (int j = 0; j < (numLength - 1); j++)
  73. {
  74. if (a[j + 1] < a[j])
  75. {
  76. temp = a[j];
  77. a[j] = a[j + 1];
  78. a[j + 1] = temp;
  79. flag = true;
  80. }
  81.  
  82. for (int z = 0; z < a.Length; z++)
  83. {
  84. bubbleout += a[z] + " | ";
  85.  
  86. }
  87. bubbleout += "\r\n";
  88. }
  89.  
  90.  
  91.  
  92.  
  93. }
  94. tbx_output_bubble.Text = bubbleout;
  95. return a;
  96.  
  97. }
  98.  
  99. //###MERGESORT###//
  100. public int[] mergesort(int[] numbers, int left, int right)
  101. {
  102. int mid;
  103.  
  104. if (right > left)
  105. {
  106. mid = (right + left) / 2;
  107. mergesort(numbers, left, mid);
  108. mergesort(numbers, (mid + 1), right);
  109.  
  110. DoMerge(numbers, left, (mid + 1), right);
  111.  
  112.  
  113. }
  114.  
  115.  
  116. for (int z = 0; z < numbers.Length; z++)
  117. {
  118. mergeout += numbers[z] + " | ";
  119.  
  120. }
  121. mergeout += "\r\n";
  122. tbx_output_merge.Text = mergeout;
  123. return numbers;
  124.  
  125.  
  126. }
  127.  
  128. static public void DoMerge(int[] numbers, int left, int mid, int right)
  129. {
  130. int[] temp = new int[10];
  131. int i, left_end, num_elements, tmp_pos;
  132.  
  133. left_end = (mid - 1);
  134. tmp_pos = left;
  135. num_elements = (right - left + 1);
  136.  
  137. while ((left <= left_end) && (mid <= right))
  138. {
  139. if (numbers[left] <= numbers[mid])
  140. temp[tmp_pos++] = numbers[left++];
  141. else
  142. temp[tmp_pos++] = numbers[mid++];
  143. }
  144.  
  145. while (left <= left_end)
  146. temp[tmp_pos++] = numbers[left++];
  147.  
  148. while (mid <= right)
  149. temp[tmp_pos++] = numbers[mid++];
  150.  
  151. for (i = 0; i < num_elements; i++)
  152. {
  153. numbers[right] = temp[right];
  154. right--;
  155. }
  156.  
  157.  
  158. }
  159.  
  160. //###QUICKSORT###//
  161. public void quicksort(int[] elements, int left, int right)
  162. {
  163. int i = left, j = right;
  164. int pivot = elements[(left + right) / 2];
  165.  
  166. while (i <= j)
  167. {
  168. while (elements[i].CompareTo(pivot) < 0)
  169. {
  170. i++;
  171. }
  172.  
  173. while (elements[j].CompareTo(pivot) > 0)
  174. {
  175. j--;
  176. }
  177.  
  178. if (i <= j)
  179. {
  180. // Swap
  181. int tmp = elements[i];
  182. elements[i] = elements[j];
  183. elements[j] = tmp;
  184.  
  185. i++;
  186. j--;
  187. }
  188.  
  189. for (int z = 0; z < elements.Length; z++)
  190. {
  191. quickout += elements[z] + " | ";
  192.  
  193. }
  194. quickout += "\r\n";
  195. tbx_output_quick.Text = quickout;
  196. }
  197.  
  198. // Recursive calls
  199. if (left < j)
  200. {
  201. quicksort(elements, left, j);
  202. }
  203.  
  204. if (i < right)
  205. {
  206. quicksort(elements, i, right);
  207. }
  208. }
  209. //###INSERTIONSORT###//
  210. static int[] insertionsort(int[] arr3)
  211. {
  212. for (int i = 0; i < arr3.Length - 1; i++)
  213. {
  214. int j = i + 1;
  215.  
  216. while (j > 0)
  217. {
  218. if (arr3[j - 1] > arr3[j])
  219. {
  220. int temp = arr3[j - 1];
  221. arr3[j - 1] = arr3[j];
  222. arr3[j] = temp;
  223.  
  224. }
  225. j--;
  226. }
  227. }
  228. return arr3;
  229. insertout += "\r\n";
  230. tbx_output_insert.Text = insertout;
  231. }
  232.  
  233. //###B_CLICK###//
  234. private void b_bubblesort_Click(object sender, RoutedEventArgs e)
  235. {
  236.  
  237. int[] z = bubblesort(arr1);
  238.  
  239.  
  240.  
  241.  
  242. }
  243. private void b_quicksort_Click(object sender, RoutedEventArgs e)
  244. {
  245.  
  246. quicksort(arr2, 0, arr3.Length-1);
  247.  
  248.  
  249.  
  250.  
  251. }
  252. private void b_insertionsort_Click(object sender, RoutedEventArgs e)
  253. {
  254.  
  255. int[] z = insertionsort(arr3, 9);
  256.  
  257.  
  258.  
  259.  
  260. }
  261. private void b_mergesort_Click(object sender, RoutedEventArgs e)
  262. {
  263.  
  264. int[] z = mergesort(arr4, 0, 9);
  265.  
  266.  
  267. }
  268.  
  269. }
  270. }
  271.  
  272. <Page
  273. x:Class="Bubblesort.MainPage"
  274. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  275. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  276. xmlns:local="using:Bubblesort"
  277. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  278. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  279. mc:Ignorable="d" FontFamily="Century Gothic" Foreground="{x:Null}">
  280.  
  281. <Grid Background="Gray" Margin="0,0,-357,0" HorizontalAlignment="Left" Width="1637">
  282. <TextBlock x:Name="tbl_head" TextWrapping="Wrap" FontSize="80" Text="Sort" FontFamily="Century Gothic" FontWeight="Bold" VerticalAlignment="Top" Foreground="White" Height="128" Margin="1294,-18,-198,0"/>
  283. <TextBox x:Name="tbx_input" Margin="10,10,870,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" FontSize="16" FontFamily="Century Gothic" Foreground="White" Height="50"/>
  284. <TextBox x:Name="tbx_output_insert" HorizontalAlignment="Left" Margin="415,65,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="540" Width="400" FontSize="10" Foreground="White"/>
  285. <Button x:Name="b_insertionsort" Content="Insertionsort" HorizontalAlignment="Left" VerticalAlignment="Top" Height="60" Width="400" Click="b_insertionsort_Click" FontFamily="Century Gothic" FontSize="30" Foreground="White" Margin="415,610,0,0"/>
  286. <Button x:Name="b_quicksort" Content="Quicksort" HorizontalAlignment="Left" Margin="10,610,0,0" VerticalAlignment="Top" Height="60" Width="400" Click="b_quicksort_Click" FontFamily="Century Gothic" FontSize="30" Foreground="White"/>
  287. <Button x:Name="b_mergesort" Content="Mergesort" HorizontalAlignment="Left" Margin="1225,610,0,0" VerticalAlignment="Top" Height="60" Width="400" Click="b_mergesort_Click" FontFamily="Century Gothic" FontSize="30" Foreground="White"/>
  288. <Button x:Name="b_bubblesort" Content="Bubblesort" HorizontalAlignment="Left" Margin="820,610,0,0" VerticalAlignment="Top" Height="60" Width="400" Click="b_bubblesort_Click" FontFamily="Century Gothic" FontSize="30" Foreground="White"/>
  289. <TextBox x:Name="tbx_output_quick" HorizontalAlignment="Left" Margin="10,65,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="540" Width="400" FontSize="10" Foreground="White"/>
  290. <TextBox x:Name="tbx_output_bubble" HorizontalAlignment="Left" Margin="820,65,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="540" Width="400" FontSize="10" Foreground="White"/>
  291. <TextBox x:Name="tbx_output_merge" HorizontalAlignment="Left" Margin="1225,65,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="540" Width="400" FontSize="10" Foreground="White"/>
  292. <TextBlock x:Name="tbx_quick_cnt" HorizontalAlignment="Left" Margin="10,670,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="40" Width="400" FontSize="30" Foreground="White"/>
  293. <TextBlock x:Name="tbx_insert_cnt" HorizontalAlignment="Left" Margin="415,675,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="40" Width="400" FontSize="30" Foreground="White"/>
  294. <TextBlock x:Name="tbx_bubble_cnt" HorizontalAlignment="Left" Margin="820,670,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="40" Width="400" FontSize="30" Foreground="White"/>
  295. <TextBlock x:Name="tbx_merge_cnt" HorizontalAlignment="Left" Margin="1225,670,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="40" Width="400" FontSize="30" Foreground="White"/>
  296.  
  297. </Grid>
  298. </Page>
RAW Paste Data