Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Chalenge
  8. {
  9. class Program
  10. {
  11. public static void RollMinMax(int[] arr)
  12. {
  13. Console.WriteLine(new String('-', 45));
  14. Console.WriteLine("before: [{0}]", string.Join(", ", arr));
  15.  
  16. int min = 0, max = 0, tmp, offset = 1;
  17. bool maxInPlace = false, minInPlace = false;
  18. int N = arr.Length;
  19.  
  20. for (int i = 0; i < N; i++)
  21. {
  22. if (i < N / 2)
  23. {
  24. if (arr[min] > arr[N - 1 - i])
  25. min = N - 1 - i;
  26. if (arr[min] > arr[i])
  27. min = i;
  28. if (arr[max] < arr[i])
  29. max = i;
  30. if (arr[max] < arr[N - 1 - i])
  31. max = N - 1 - i;
  32. }
  33. else
  34. {
  35. if (i == N / 2 && max > min)
  36. { // only once. locate max left to min
  37. Switch(arr, min, max);
  38. tmp = min;
  39. min = max;
  40. max = tmp;
  41. }
  42.  
  43. if (max - offset >= 0)
  44. Switch(arr, max - offset + 1, max - offset);
  45. else
  46. maxInPlace = true;
  47.  
  48. if (min + offset <= N - 1)
  49. Switch(arr, min + offset - 1, min + offset);
  50. else
  51. minInPlace = true;
  52.  
  53. offset += 1;
  54.  
  55. // if min or max in place then do another shift to the other
  56. if (maxInPlace && min + offset <= N - 1)
  57. {
  58. Switch(arr, min + offset - 1, min + offset);
  59. offset += 1;
  60. }
  61. else if (minInPlace && max - offset >= 0)
  62. {
  63. Switch(arr, max - offset + 1, max - offset);
  64. offset += 1;
  65. }
  66. }
  67. }
  68. Console.WriteLine("after : [{0}]", string.Join(", ", arr));
  69. }
  70.  
  71. public static void Switch(int[] arr, int index1, int index2)
  72. {
  73. int tmp = arr[index1];
  74. arr[index1] = arr[index2];
  75. arr[index2] = tmp;
  76. }
  77.  
  78. static void Main(string[] args)
  79. {
  80. int[] arr1 = { 2, 4, 56, 6, 7, 45, 23, 9, 1, 8, 10, 3 };
  81. int[] arr1exp = { 56, 2, 4, 6, 7, 45, 23, 9, 8, 10, 3, 1 };
  82. RollMinMax(arr1);
  83. if (!arr1.SequenceEqual(arr1exp))
  84. Console.WriteLine("TEST FAILED 1");
  85.  
  86. int[] arr2 = { 2, 4, 56, 6, 7, 45, 23, 9, 1, 8, 10 };
  87. int[] arr2exp = { 56, 2, 4, 6, 7, 45, 23, 9, 8, 10, 1 };
  88. RollMinMax(arr2);
  89. if (!arr2.SequenceEqual(arr2exp))
  90. Console.WriteLine("TEST FAILED 2");
  91.  
  92. int[] arr3 = { 56, 4, 2, 6, 7, 45, 23, 9, 10, 8, 1 };
  93. int[] arr3exp = { 56, 4, 2, 6, 7, 45, 23, 9, 10, 8, 1 };
  94. RollMinMax(arr3);
  95. if (!arr3.SequenceEqual(arr3exp))
  96. Console.WriteLine("TEST FAILED 3");
  97.  
  98. int[] arr4 = { 4, 2, 6, 7, 45, 23, 9, 10, 8, 1, 56 };
  99. int[] arr4exp = { 56, 4, 2, 6, 7, 45, 23, 9, 10, 8, 1 };
  100. RollMinMax(arr4);
  101. if (!arr4.SequenceEqual(arr4exp))
  102. Console.WriteLine("TEST FAILED 4");
  103.  
  104. int[] arr5 = { 1, 56, 6, 7, 45, 23, 9, 10, 8, 4, 2 };
  105. int[] arr5exp = { 56, 6, 7, 45, 23, 9, 10, 8, 4, 2, 1 };
  106. RollMinMax(arr5);
  107. if (!arr5.SequenceEqual(arr5exp))
  108. Console.WriteLine("TEST FAILED 5");
  109.  
  110. int[] arr6 = { 2, 4, 1, 6, 7, 45, 23, 9, 56, 8, 10 };
  111. int[] arr6exp = { 56, 2, 4, 6, 7, 45, 23, 9, 8, 10, 1 };
  112. RollMinMax(arr6);
  113. if (!arr6.SequenceEqual(arr6exp))
  114. Console.WriteLine("TEST FAILED 6");
  115.  
  116. int[] arr7 = { 2, 4, 1, 6, 7, 45, 11, 23, 9, 56, 8, 10 };
  117. int[] arr7exp = { 56, 2, 4, 6, 7, 45, 11, 23, 9, 8, 10, 1 };
  118. RollMinMax(arr7);
  119. if (!arr7.SequenceEqual(arr7exp))
  120. Console.WriteLine("TEST FAILED 7");
  121.  
  122. // max, min at end
  123. int[] a31 = { 14, 13, 9, 9, 20, 12, 4, 6, 37, 1, 9, 25 };
  124. int[] a31exp = { 37, 14, 13, 9, 9, 20, 12, 4, 6, 9, 25, 1 };
  125. RollMinMax(a31);
  126. if (!a31.SequenceEqual(a31exp))
  127. Console.WriteLine("TEST FAILED\n");
  128.  
  129. // max, min at end
  130. int[] a43 = { 14, 13, 9, 9, 20, 12, 4, 6, 9, 25, 37, 1 };
  131. int[] a43exp = { 37, 14, 13, 9, 9, 20, 12, 4, 6, 9, 25, 1 };
  132. RollMinMax(a43);
  133. if (!a43.SequenceEqual(a43exp))
  134. Console.WriteLine("TEST FAILED\n");
  135.  
  136. // min, max at end
  137. int[] a42 = { 3, 34, 9, 9, 20, 12, 4, 6, 9, 25, 1, 37 };
  138. int[] a42exp = { 37, 3, 34, 9, 9, 20, 12, 4, 6, 9, 25, 1 };
  139. RollMinMax(a42);
  140. if (!a42.SequenceEqual(a42exp))
  141. Console.WriteLine("TEST FAILED");
  142.  
  143. int[] a41 = { 3, 34, 9, 9, 20, 12, 1, 6, 9, 25, 10, 37 };
  144. int[] a41exp = { 37, 3, 34, 9, 9, 20, 12, 6, 9, 25, 10, 1 };
  145. RollMinMax(a41);
  146. if (!a41.SequenceEqual(a41exp))
  147. Console.WriteLine("TEST FAILED");
  148.  
  149. // max, min at begining
  150. int[] a51 = { 34, 3, 9, 9, 20, 12, 4, 6, 9, 25, 7, 7 };
  151. int[] a51exp = { 34, 9, 9, 20, 12, 4, 6, 9, 25, 7, 7, 3 };
  152. RollMinMax(a51);
  153. if (!a51.SequenceEqual(a51exp))
  154. Console.WriteLine("TEST FAILED\n");
  155.  
  156. // min, max at begining
  157. int[] a2 = { 3, 34, 9, 9, 20, 12, 4, 6, 9, 25, 7, 7 };
  158. int[] a2exp = { 34, 9, 9, 20, 12, 4, 6, 9, 25, 7, 7, 3 };
  159. RollMinMax(a2);
  160. if (!a2.SequenceEqual(a2exp))
  161. Console.WriteLine("TEST FAILED\n");
  162.  
  163. // max, min at middle
  164. int[] a5 = { 3, 34, 9, 9, 20, 42, 1, 6, 9, 25, 2, 37 };
  165. int[] a5exp = { 42, 3, 34, 9, 9, 20, 6, 9, 25, 2, 37, 1 };
  166. RollMinMax(a5);
  167. if (!a5.SequenceEqual(a5exp))
  168. Console.WriteLine("TEST FAILED");
  169.  
  170. // min, max at middle
  171. int[] a6 = { 3, 34, 9, 9, 20, 1, 42, 6, 9, 25, 2, 37 };
  172. int[] a6exp = { 42, 3, 34, 9, 9, 20, 6, 9, 25, 2, 37, 1 };
  173. RollMinMax(a6);
  174. if (!a6.SequenceEqual(a6exp))
  175. Console.WriteLine("TEST FAILED");
  176.  
  177. int[] a7 = { 16, 12, 19, 19, 13, 17, 11, 1, 100 };
  178. int[] a7exp = { 100, 16, 12, 19, 19, 13, 17, 11, 1 };
  179. RollMinMax(a7);
  180. if (!a7.SequenceEqual(a7exp))
  181. Console.WriteLine("TEST FAILED");
  182.  
  183. Console.ReadLine();
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement