Guest User

Untitled

a guest
Jul 15th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace WpfApplicationSort
  16. {
  17.     /// <summary>
  18.     /// Interaction logic for MainWindow.xaml
  19.     /// </summary>
  20.     public partial class MainWindow : Window
  21.     {
  22.         public MainWindow()
  23.         {
  24.  
  25.             Random rand = new Random();
  26.             int numOfIntegers = 1000;
  27.             int[] array;
  28.             double executionTime = 0;
  29.  
  30.             InitializeComponent();
  31.  
  32.             array = new int[numOfIntegers];
  33.         }
  34.  
  35.     //selection sort, algorithm 1        
  36. public void SelectionSort(int[] array)  
  37. {
  38. int minIndex;
  39.  
  40. int pass,  n = array.Length;
  41. int j = 0;
  42. int temp;
  43.  
  44. for (pass = 0; pass < n-1; pass++)
  45.  {
  46.      minIndex = pass;
  47.     {
  48.  
  49.    
  50.     if (array[j] < array[minIndex])
  51.     minIndex = j;
  52.   }
  53.    
  54. if (minIndex!= pass)
  55.   {
  56.    temp = array[pass];
  57.    array[pass] = array[minIndex];
  58.    array[minIndex] = temp;
  59.   }
  60.  }
  61. }
  62.     //  insertion sort, algorithm 2
  63. public void InsertionSort(int[] array)
  64. {
  65. int i, j, n = array.Length;
  66. int target;
  67.  
  68. for (i = 1; i < n; i++)
  69. {
  70. j = i;
  71. target = array[i];
  72. while (j > 0 && target < array[j - 1])
  73. {
  74. array[j] = array[j - 1];
  75. j--;
  76. }
  77.  
  78. array[j] = target;
  79. }
  80. }
  81.  
  82. //  exchange sort algorthim 3
  83. public void ExchangeSort(int[] array)
  84. {
  85. int pass, i, n = array.Length;
  86. int temp;
  87.  
  88. for (pass = 0; pass < n - 1; pass++)
  89. {
  90. for (i = pass + 1; i < n; i++)
  91. {
  92. if (array[i] < array[pass])
  93. {
  94. temp = array[pass];
  95. array[pass] = array[i];
  96. array[i] = temp;
  97. }
  98.     }
  99.         }
  100. }
  101.  
  102.         //  bubble sort no 4
  103. public void BubbleSort(int[] array)
  104. {
  105. int i, j, n = array.Length;
  106.  
  107. bool exchangeOccurs = true;
  108. int temp;
  109. i = n - 1;
  110. while (i > 0 && exchangeOccurs)
  111. {
  112. exchangeOccurs = false;
  113.  
  114. for (j = 0; j < i; j++)
  115. {
  116.  
  117. if (array[j + 1] < array[j])
  118. {
  119. temp = array[j];
  120. array[j] = array[j + 1];
  121. array[j + 1] = temp;
  122. exchangeOccurs = true;
  123. }
  124. }
  125. i--;
  126. }
  127. }
  128.         // shell sort no 5
  129. public static void ShellSort(int[] array)
  130. {
  131. int i, j, k, n = array.Length;
  132. int target;
  133.  
  134. for (k = 1; k <= n / 9; k = 3 * k + 1) ;
  135. while (k >= 1)
  136. {
  137.  
  138. for (i = k; i < n; i++)
  139. {
  140. target = array[i];
  141.  
  142. j = i - k;
  143.  
  144. while (j >= 0 && target < array[j])
  145. {
  146. array[j + k] = array[j];
  147. j = j - k;
  148. }
  149. array[j + k] = target;
  150. }
  151. k /= 3;
  152. }
  153. }
  154.  
  155.  
  156.         }
  157.  
  158. public static void GenIntergers_Click(object sender, RoutedEventArgs e)
  159. {
  160.  
  161.     for (int i = 0; i < numOfIntegers; i++)
  162.     {
  163.         array[i] = rand.Next();
  164.    
  165.     MessageBox.Show("Done!");
  166.  
  167.     Rectangle rect = new Rectangle();
  168.     rect.Width = 50;
  169.     rect.Height = 150;
  170.     rect.Fill = Brushes.White;
  171.     rect.Stroke = Brushes.Blue;
  172.     Canvas.SetLeft(rect, 100);
  173.     Canvas.SetBottom(rect, 50);
  174.     Canvas.Children.Add(rect);
  175.  
  176.  
  177.         }
  178.  
  179. private void Sort_Click(object sender, RoutedEventArgs e)
  180. {
  181.     DateTime startTime = DateTime.Now;
  182.     SelectionSort(array);
  183.     TimeSpan diff = DateTime.Now.Subtract(startTime);
  184.     executionTime = diff.TotalMilliseconds;
  185.     MessageBox.Show(executionTime.ToString());
  186.  
  187. }
  188.    
  189. }
Add Comment
Please, Sign In to add comment