Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
- // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
- namespace Bubblesort
- {
- /// <summary>
- /// An empty page that can be used on its own or navigated to within a Frame.
- /// </summary>
- public sealed partial class MainPage : Page
- {
- int[] arr1 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
- int[] arr2 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
- int[] arr3 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
- int[] arr4 = new int[10]; //Legt einen int-Array mit der Länge 10 an.
- String bubbleout = "";
- String mergeout = "";
- String quickout = "";
- String insertout = "";
- Random randNum = new Random();
- public MainPage()
- {
- this.InitializeComponent();
- //Array wird mit ganzen Zahlen gefüllt
- for (int i = 0; i < arr1.Length; i++)
- {
- arr1[i] = randNum.Next(1, 100);
- arr2 = arr1;
- arr3 = arr1;
- arr4 = arr1;
- }
- //Umwandeln des Arrays in einen String zur Ausgabe in einem Textfeld
- String inputstring = "";
- for (int i = 0; i < arr1.Length; i++)
- {
- inputstring += arr1[i] + " | ";
- }
- tbx_input.Text = inputstring;
- }
- //###QUICKSORT###//
- public int[] bubblesort(int[] a)
- {
- bool flag = true;
- int temp;
- int numLength = a.Length;
- for (int i = 1;
- (i <= (numLength - 1)) && flag; i++)
- {
- flag = false;
- for (int j = 0; j < (numLength - 1); j++)
- {
- if (a[j + 1] < a[j])
- {
- temp = a[j];
- a[j] = a[j + 1];
- a[j + 1] = temp;
- flag = true;
- }
- for (int z = 0; z < a.Length; z++)
- {
- bubbleout += a[z] + " | ";
- }
- bubbleout += "\r\n";
- }
- }
- tbx_output_bubble.Text = bubbleout;
- return a;
- }
- //###MERGESORT###//
- public int[] mergesort(int[] numbers, int left, int right)
- {
- int mid;
- if (right > left)
- {
- mid = (right + left) / 2;
- mergesort(numbers, left, mid);
- mergesort(numbers, (mid + 1), right);
- DoMerge(numbers, left, (mid + 1), right);
- }
- for (int z = 0; z < numbers.Length; z++)
- {
- mergeout += numbers[z] + " | ";
- }
- mergeout += "\r\n";
- tbx_output_merge.Text = mergeout;
- return numbers;
- }
- static public void DoMerge(int[] numbers, int left, int mid, int right)
- {
- int[] temp = new int[10];
- int i, left_end, num_elements, tmp_pos;
- left_end = (mid - 1);
- tmp_pos = left;
- num_elements = (right - left + 1);
- while ((left <= left_end) && (mid <= right))
- {
- if (numbers[left] <= numbers[mid])
- temp[tmp_pos++] = numbers[left++];
- else
- temp[tmp_pos++] = numbers[mid++];
- }
- while (left <= left_end)
- temp[tmp_pos++] = numbers[left++];
- while (mid <= right)
- temp[tmp_pos++] = numbers[mid++];
- for (i = 0; i < num_elements; i++)
- {
- numbers[right] = temp[right];
- right--;
- }
- }
- //###QUICKSORT###//
- public void quicksort(int[] elements, int left, int right)
- {
- int i = left, j = right;
- int pivot = elements[(left + right) / 2];
- while (i <= j)
- {
- while (elements[i].CompareTo(pivot) < 0)
- {
- i++;
- }
- while (elements[j].CompareTo(pivot) > 0)
- {
- j--;
- }
- if (i <= j)
- {
- // Swap
- int tmp = elements[i];
- elements[i] = elements[j];
- elements[j] = tmp;
- i++;
- j--;
- }
- for (int z = 0; z < elements.Length; z++)
- {
- quickout += elements[z] + " | ";
- }
- quickout += "\r\n";
- tbx_output_quick.Text = quickout;
- }
- // Recursive calls
- if (left < j)
- {
- quicksort(elements, left, j);
- }
- if (i < right)
- {
- quicksort(elements, i, right);
- }
- }
- //###INSERTIONSORT###//
- static int[] insertionsort(int[] arr3)
- {
- for (int i = 0; i < arr3.Length - 1; i++)
- {
- int j = i + 1;
- while (j > 0)
- {
- if (arr3[j - 1] > arr3[j])
- {
- int temp = arr3[j - 1];
- arr3[j - 1] = arr3[j];
- arr3[j] = temp;
- }
- j--;
- }
- }
- return arr3;
- insertout += "\r\n";
- tbx_output_insert.Text = insertout;
- }
- //###B_CLICK###//
- private void b_bubblesort_Click(object sender, RoutedEventArgs e)
- {
- int[] z = bubblesort(arr1);
- }
- private void b_quicksort_Click(object sender, RoutedEventArgs e)
- {
- quicksort(arr2, 0, arr3.Length-1);
- }
- private void b_insertionsort_Click(object sender, RoutedEventArgs e)
- {
- int[] z = insertionsort(arr3, 9);
- }
- private void b_mergesort_Click(object sender, RoutedEventArgs e)
- {
- int[] z = mergesort(arr4, 0, 9);
- }
- }
- }
- <Page
- x:Class="Bubblesort.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:Bubblesort"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" FontFamily="Century Gothic" Foreground="{x:Null}">
- <Grid Background="Gray" Margin="0,0,-357,0" HorizontalAlignment="Left" Width="1637">
- <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"/>
- <TextBox x:Name="tbx_input" Margin="10,10,870,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" FontSize="16" FontFamily="Century Gothic" Foreground="White" Height="50"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- <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"/>
- </Grid>
- </Page>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement