fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. int [] data = GenerateData(3);
  8.  
  9. Console.Write("Before:");
  10. PrintArray(data);
  11.  
  12. AnonSort(ref data);
  13.  
  14. Console.Write("After:");
  15. PrintArray(data);
  16. }
  17. private static int[] GenerateData(int length)
  18. {
  19. if (length > 3) throw new Exception("Are you fckn out of mind?");
  20. int [] data = new int[length];
  21. System.Random rand = new System.Random();
  22. for(int i = 0; i < data.Length; i++)
  23. data[i] = rand.Next(10);
  24.  
  25. return data;
  26. }
  27. private static void PrintArray(int[] data)
  28. {
  29. Console.Write("{");
  30. for(int i = 0; i < data.Length; i++)
  31. {
  32. Console.Write(data[i]);
  33. if (i != data.Length - 1) Console.Write(", ");
  34. }
  35.  
  36. Console.WriteLine("}");
  37. }
  38.  
  39. private static void AnonSort(ref int[] data)
  40. {
  41. Console.WriteLine($"Welcome to anon sort.\nWe have an array of {data.Length} elements. Let's sort this shit hard way!\n...");
  42.  
  43. //O (n*n)
  44. BubbleSort(ref data);
  45.  
  46. //O (power(n!, n! + 1) + 2n + O(Math.Pow(n!, n! + 1))) = O (power(n!, n! + 1))
  47. long counter = AlgorithmOptimization(data.Length);
  48.  
  49. Console.WriteLine($"Anon sort complete.\nAlgorithm takes {counter}+ arithmetic operations.");
  50. }
  51. private static void BubbleSort(ref int[] data)
  52. {
  53. for(int i = 0; i < data.Length - 1; i++)
  54. for(int j = i + 1; j < data.Length; j++)
  55. {
  56. if(data[i] > data[j])
  57. {
  58. int temp = data[i];
  59. data[i] = data[j];
  60. data[j] = temp;
  61. }
  62. }
  63. }
  64. private static long Factorial(long n)
  65. {
  66. long temp = 1;
  67. for (int i = 1; i <= n; i++)
  68. temp *= i;
  69.  
  70. return temp;
  71. }
  72. private static long AlgorithmOptimization(int n)
  73. {
  74. long counter = new long();
  75. long temp = (long)Math.Pow(Factorial(n), Factorial(n) + 1);
  76. for (long l = 0; l < temp; l++)
  77. counter++;
  78.  
  79. return counter;
  80. }
  81. }
Success #stdin #stdout 0.04s 24032KB
stdin
Standard input is empty
stdout
Before:{8, 9, 6}
Welcome to anon sort.
We have an array of 3 elements. Let's sort this shit hard way!
...
Anon sort complete.
Algorithm takes 279936+ arithmetic operations.
After:{6, 8, 9}