Filip13

ntp test - sve

Oct 29th, 2024
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.95 KB | Source Code | 0 0
  1. namespace sortiranja
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             //int[] array = { -1, 5, 7, 8, 1, -2, 0, 5, -1, 6, -9, 0, 11, -12, 3 };
  8.  
  9.  
  10.             //BubbleSort(array);
  11.             //SelectionSort(array);
  12.             //InsertionSort(array);
  13.             //QuickSort(array, 0, array.Length - 1);
  14.             //MergeSort(array);
  15.  
  16.             //int target = int.Parse(Console.ReadLine());
  17.             //int targetPositon = BinarySearchIterative(array, target, 0, array.Length - 1);
  18.             //int targetPositon = BinarySearchRecursive(array, target, 0, array.Length - 1);
  19.             //Console.WriteLine(targetPositon);
  20.  
  21.  
  22.             //Console.WriteLine(string.Join(" ", array));
  23.  
  24.  
  25.             int a = int.Parse(Console.ReadLine());
  26.             int b = int.Parse(Console.ReadLine());
  27.  
  28.             Console.WriteLine(IsPrimeMaxOptimized(a).ToString());
  29.             Factiorization(a);
  30.  
  31.             int nzd = EuclidAlgorithmRecursive(a, b);
  32.             int nzd1 = EuclidAlgorithmIterative(a, b);
  33.             Console.WriteLine(nzd);
  34.             Console.WriteLine(nzd1);
  35.  
  36.             int nzs = NZS(a, b);
  37.             Console.WriteLine(nzs);
  38.         }
  39.  
  40.         private static int NZS(int a, int b)
  41.         {
  42.             return a * b / EuclidAlgorithmIterative(a, b);
  43.         }
  44.  
  45.         private static int EuclidAlgorithmIterative(int a, int b)
  46.         {
  47.             while (b > 0)
  48.             {
  49.                 int tmp = b;
  50.                 b = a % b;
  51.                 a = tmp;
  52.             }
  53.  
  54.             return a;
  55.         }
  56.  
  57.         private static int EuclidAlgorithmRecursive(int a, int b)
  58.         {
  59.             if (b == 0)
  60.             {
  61.                 return a;
  62.             }
  63.             return EuclidAlgorithmRecursive(b, a % b);
  64.         }
  65.  
  66.  
  67.         private static void Factiorization(int num)
  68.         {
  69.             int root = (int)Math.Ceiling(Math.Sqrt(num));
  70.             int i = 2;
  71.  
  72.             while (i <= root)
  73.             {
  74.                 if (num % i == 0)
  75.                 {
  76.                     Console.WriteLine(i);
  77.                     num /= i;
  78.                     root = (int)Math.Ceiling(Math.Sqrt(num));
  79.                 }
  80.                 else
  81.                 {
  82.                     i++;
  83.                 }
  84.             }
  85.             if (num > 1)
  86.             {
  87.                 Console.WriteLine(num);
  88.             }
  89.         }
  90.  
  91.         private static bool IsPrimeMaxOptimized(int num)
  92.         {
  93.             if (num == 1)
  94.             {
  95.                 return false;
  96.             }
  97.             if (num == 2)
  98.             {
  99.                 return true;
  100.             }
  101.             if (num % 2 == 0)
  102.             {
  103.                 return false;
  104.             }
  105.  
  106.  
  107.             int root = (int)Math.Ceiling(Math.Sqrt(num));
  108.             for (int i = 3; i <= root; i += 2)
  109.             {
  110.                 if (num % i == 0)
  111.                 {
  112.                     return false;
  113.                 }
  114.  
  115.             }
  116.  
  117.             return true;
  118.         }
  119.  
  120.  
  121.  
  122.  
  123.         private static void MergeSort(int[] array)
  124.         {
  125.             if (array.Length <= 1)
  126.             {
  127.                 return;
  128.             }
  129.  
  130.             int mid = array.Length / 2;
  131.  
  132.             int[] left = new int[mid];
  133.             int[] right = new int[array.Length - mid];
  134.  
  135.             for (int i = 0; i < left.Length; i++)
  136.             {
  137.                 left[i] = array[i];
  138.             }
  139.  
  140.             for (int i = mid; i < array.Length; i++)
  141.             {
  142.                 right[i - mid] = array[i];
  143.             }
  144.  
  145.  
  146.             MergeSort(left);
  147.             MergeSort(right);
  148.  
  149.             Merge(array, left, right);
  150.         }
  151.  
  152.         private static void Merge(int[] array, int[] left, int[] right)
  153.         {
  154.             int l = 0;
  155.             int r = 0;
  156.             int pos = 0;
  157.  
  158.             while (l < left.Length && r < right.Length)
  159.             {
  160.                 if (left[l] <= right[r])
  161.                 {
  162.                     array[pos] = left[l];
  163.                     pos++;
  164.                     l++;
  165.                 }
  166.                 else
  167.                 {
  168.                     array[pos] = right[r];
  169.                     pos++;
  170.                     r++;
  171.                 }
  172.             }
  173.  
  174.             while (l < left.Length)
  175.             {
  176.                 array[pos] = left[l];
  177.                 pos++;
  178.                 l++;
  179.             }
  180.  
  181.             while (r < right.Length)
  182.             {
  183.                 array[pos] = right[r];
  184.                 pos++;
  185.                 r++;
  186.             }
  187.  
  188.         }
  189.  
  190.  
  191.         private static void QuickSort(int[] array, int l, int r)
  192.         {
  193.             if (l >= r)
  194.             {
  195.                 return;
  196.             }
  197.  
  198.             int pivotPos = Partition(array, l, r);
  199.  
  200.             QuickSort(array, l, pivotPos - 1);
  201.             QuickSort(array, pivotPos + 1, r);
  202.         }
  203.  
  204.         private static int Partition(int[] array, int l, int r)
  205.         {
  206.             int pivotPos = l;
  207.  
  208.             for (int i = l + 1; i <= r; i++)
  209.             {
  210.                 if (array[l] > array[i])
  211.                 {
  212.                     pivotPos++;
  213.                     Swap(array, i, pivotPos);
  214.                 }
  215.             }
  216.  
  217.             Swap(array, l, pivotPos);
  218.  
  219.  
  220.             return pivotPos;
  221.         }
  222.  
  223.  
  224.         private static void BubbleSort(int[] array)
  225.         {
  226.             for (int i = 0; i < array.Length - 1; i++)
  227.             {
  228.                 for (int j = 0; j < array.Length - 1 - i; j++)
  229.                 {
  230.                     if (array[j] > array[j + 1])
  231.                     {
  232.                         Swap(array, j, j + 1);
  233.                     }
  234.                 }
  235.             }
  236.  
  237.         }
  238.  
  239.         private static void InsertionSort(int[] array)
  240.         {
  241.             for (int i = 1; i < array.Length; i++)
  242.             {
  243.                 int key = array[i];
  244.  
  245.                 int j = i - 1;
  246.                 while (j >= 0 && array[j] > key)
  247.                 {
  248.                     array[j + 1] = array[j];
  249.                     j--;
  250.                 }
  251.  
  252.                 array[j + 1] = key;
  253.             }
  254.  
  255.         }
  256.  
  257.         private static void SelectionSort(int[] array)
  258.         {
  259.             for (int i = 0; i < array.Length - 1; i++)
  260.             {
  261.                 int minIndex = i;
  262.                 for (int j = i + 1; j < array.Length; j++)
  263.                 {
  264.                     if (array[j] < array[minIndex])
  265.                     {
  266.                         minIndex = j;
  267.                     }
  268.                 }
  269.  
  270.                 Swap(array, minIndex, i);
  271.             }
  272.         }
  273.  
  274.  
  275.  
  276.  
  277.         private static int BinarySearchIterative(int[] array, int target, int l, int r)
  278.         {
  279.             while (l <= r)
  280.             {
  281.                 int m = (r + l) / 2;
  282.  
  283.                 if (array[m] == target)
  284.                 {
  285.                     return m;
  286.                 }
  287.                 else if (array[m] > target)
  288.                 {
  289.                     r = m - 1;
  290.                 }
  291.                 else
  292.                 {
  293.                     l = m + 1;
  294.                 }
  295.             }
  296.  
  297.             return -1;
  298.         }
  299.  
  300.         private static int BinarySearchRecursive(int[] array, int target, int l, int r)
  301.         {
  302.             if (l > r)
  303.             {
  304.                 return -1;
  305.             }
  306.  
  307.             int m = (l + r) / 2;
  308.  
  309.             if (array[m] == target)
  310.             {
  311.                 return m;
  312.             }
  313.             else if (array[m] > target)
  314.             {
  315.                 return BinarySearchRecursive(array, target, l, m - 1);
  316.             }
  317.             else /*if (array[m] < target)*/
  318.             {
  319.                 return BinarySearchRecursive(array, target, m + 1, r);
  320.             }
  321.  
  322.         }
  323.  
  324.  
  325.  
  326.         private static void Swap(int[] array, int i, int j)
  327.         {
  328.             int tmp = array[i];
  329.             array[i] = array[j];
  330.             array[j] = tmp;
  331.         }
  332.  
  333.     }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment