Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void swap(int v1, int v2)
  11. {
  12. v1 = (v1 + v2);
  13. v2 = (v1 - v2);
  14. v1 = (v1 - v2);
  15. }
  16. static void simple_sort(int n, byte[] t)
  17. {
  18. for (byte i = 0; i <= n - 2; i++)
  19. {
  20. for (byte z = (byte)(i + 1); z <= n - 1; z++)
  21. {
  22. if (t[i] > t[z])
  23. {
  24. byte swap;
  25. swap = t[i];
  26. t[i] = t[z];
  27. t[z] = swap;
  28. }
  29. }
  30. }
  31. }
  32. static void minimumselect_sort(int n, byte[] t)
  33. {
  34. for (byte i = 0; i <= n - 2; i++)
  35. {
  36. byte min = i;
  37. for (byte z = (byte)(i + 1); z <= n - 1; z++)
  38. {
  39. if (t[min] > t[z]) min = z;
  40. }
  41. byte swap;
  42. swap = t[i];
  43. t[i] = t[min];
  44. t[min] = swap;
  45. }
  46. }
  47. static void bubble_sort(int n, byte[] t)
  48. {
  49. for (byte i = (byte)n; i > 1; i--)
  50. {
  51. for (byte z = 0; z < i - 1; z++)
  52. {
  53. if (t[z] > t[z + 1])
  54. {
  55. byte swap;
  56. swap = t[z];
  57. t[z] = t[z + 1];
  58. t[z + 1] = swap;
  59. }
  60. }
  61. }
  62. }
  63. static void fixed_bubble_sort(int n, byte[] t)
  64. {
  65. byte i = (byte)(n-1);
  66. //for (; i >= 1; )
  67. while (i >= 1)
  68. {
  69. byte sn = 0;
  70. for (byte z = 0; z < i; z++)
  71. {
  72. if (t[z] > t[z + 1])
  73. {
  74. byte swap; swap = t[z]; t[z] = t[z + 1]; t[z + 1] = swap;
  75. sn = z;
  76. }
  77. }
  78. i = sn;
  79. }
  80. }
  81. static void fixed_beill(int n, byte[] t)
  82. {
  83. for (byte i = 1; i < n; i++)
  84. {
  85. sbyte j = (sbyte)(i - 1);
  86. byte y = t[i];
  87. while (j >= 0 && t[j] > y)
  88. {
  89. t[j + 1] = t[j];
  90. j--;
  91. }
  92. t[j + 1] = y;
  93. }
  94. }
  95. static void sort_beszuras(int n, byte[] t)
  96. {
  97. for (int i = 1; i < n; i++)
  98. {
  99. int kulcs = t[i];
  100. int j = i - 1;
  101. while (j >= 0 && t[j] > kulcs)
  102. {
  103. t[j + 1] = t[j];
  104. j--;
  105. }
  106. t[j + 1] = (byte)kulcs;
  107. }
  108. }
  109.  
  110. static void quick_sort(int l, int r, byte[] t)
  111. {
  112. int i = l, j = r;
  113. byte tmp;
  114. int pivot = t[(l + (r)) / 2];
  115.  
  116. /* partition */
  117.  
  118. while (i <= j)
  119. {
  120. while (t[i] < pivot) i++;
  121. while (t[j] > pivot) j--;
  122.  
  123. if (i <= j)
  124. {
  125. tmp = t[i];
  126. t[i] = t[j];
  127. t[j] = tmp;
  128. i++;
  129. j--;
  130. }
  131. };
  132.  
  133. /* recursion */
  134.  
  135. if (l < j)
  136. quick_sort(l, j, t);
  137. if (i < r)
  138. quick_sort(i, r, t);
  139. }
  140. static void quick_sort_string(int l, int r, string[] t)
  141. {
  142. int i = l, j = r;
  143. string tmp;
  144. string pivot = t[(l + (r)) / 2];
  145.  
  146. /* partition */
  147.  
  148. while (i <= j)
  149. {
  150. while (t[i].CompareTo(pivot) < 0) i++;
  151. while (t[j].CompareTo(pivot) > 0) j--;
  152.  
  153. if (i <= j)
  154. {
  155. tmp = t[i];
  156. t[i] = t[j];
  157. t[j] = tmp;
  158. i++;
  159. j--;
  160. }
  161. };
  162.  
  163. /* recursion */
  164.  
  165. if (l < j)
  166. quick_sort_string(l, j, t);
  167. if (i < r)
  168. quick_sort_string(i, r, t);
  169. }
  170. static void Main(string[] args)
  171. {
  172. //byte[] array = new byte[15]{6,5,4,3,2,1,32,63,94,1,2,3,46,157,35};
  173. string[] array = new string[] { "álmos", "előd", "ond", "kond", "tas", "huba", "töhötöm" };
  174. //simple_sort(array.Length, array);
  175. //minimumselect_sort(array.Length, array);
  176. //bubble_sort(array.Length, array);
  177. //fixed_bubble_sort(array.Length, array);
  178. //fixed_beill(array.Length, array);
  179. //sort_beszuras(array.Length, array);
  180. //quick_sort(0, array.Length-1, array);
  181. quick_sort_string(0, array.Length - 1, array);
  182. Console.Write("\n");
  183. for (int i = 0; i < array.Length; i++)
  184. {
  185. Console.Write(array[i] + " ");
  186. }
  187. Console.Read();
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement