Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WpfApplicationSort
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- Random rand = new Random();
- int numOfIntegers = 1000;
- int[] array;
- double executionTime = 0;
- InitializeComponent();
- array = new int[numOfIntegers];
- }
- //selection sort, algorithm 1
- public void SelectionSort(int[] array)
- {
- int minIndex;
- int pass, n = array.Length;
- int j = 0;
- int temp;
- for (pass = 0; pass < n-1; pass++)
- {
- minIndex = pass;
- {
- if (array[j] < array[minIndex])
- minIndex = j;
- }
- if (minIndex!= pass)
- {
- temp = array[pass];
- array[pass] = array[minIndex];
- array[minIndex] = temp;
- }
- }
- }
- // insertion sort, algorithm 2
- public void InsertionSort(int[] array)
- {
- int i, j, n = array.Length;
- int target;
- for (i = 1; i < n; i++)
- {
- j = i;
- target = array[i];
- while (j > 0 && target < array[j - 1])
- {
- array[j] = array[j - 1];
- j--;
- }
- array[j] = target;
- }
- }
- // exchange sort algorthim 3
- public void ExchangeSort(int[] array)
- {
- int pass, i, n = array.Length;
- int temp;
- for (pass = 0; pass < n - 1; pass++)
- {
- for (i = pass + 1; i < n; i++)
- {
- if (array[i] < array[pass])
- {
- temp = array[pass];
- array[pass] = array[i];
- array[i] = temp;
- }
- }
- }
- }
- // bubble sort no 4
- public void BubbleSort(int[] array)
- {
- int i, j, n = array.Length;
- bool exchangeOccurs = true;
- int temp;
- i = n - 1;
- while (i > 0 && exchangeOccurs)
- {
- exchangeOccurs = false;
- for (j = 0; j < i; j++)
- {
- if (array[j + 1] < array[j])
- {
- temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- exchangeOccurs = true;
- }
- }
- i--;
- }
- }
- // shell sort no 5
- public static void ShellSort(int[] array)
- {
- int i, j, k, n = array.Length;
- int target;
- for (k = 1; k <= n / 9; k = 3 * k + 1) ;
- while (k >= 1)
- {
- for (i = k; i < n; i++)
- {
- target = array[i];
- j = i - k;
- while (j >= 0 && target < array[j])
- {
- array[j + k] = array[j];
- j = j - k;
- }
- array[j + k] = target;
- }
- k /= 3;
- }
- }
- }
- public static void GenIntergers_Click(object sender, RoutedEventArgs e)
- {
- for (int i = 0; i < numOfIntegers; i++)
- {
- array[i] = rand.Next();
- MessageBox.Show("Done!");
- Rectangle rect = new Rectangle();
- rect.Width = 50;
- rect.Height = 150;
- rect.Fill = Brushes.White;
- rect.Stroke = Brushes.Blue;
- Canvas.SetLeft(rect, 100);
- Canvas.SetBottom(rect, 50);
- Canvas.Children.Add(rect);
- }
- private void Sort_Click(object sender, RoutedEventArgs e)
- {
- DateTime startTime = DateTime.Now;
- SelectionSort(array);
- TimeSpan diff = DateTime.Now.Subtract(startTime);
- executionTime = diff.TotalMilliseconds;
- MessageBox.Show(executionTime.ToString());
- }
- }
Add Comment
Please, Sign In to add comment