vojta249

quick+insert sort

Mar 30th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2.  
  3. namespace quicksort
  4. {
  5.     using System;
  6.  
  7.     public class Program
  8.     {
  9.         static int count = 0;
  10.  
  11.         public static void Main()
  12.         {
  13.             Random rnd = new Random();
  14.             int[] arr = new int[14];
  15.             for (int i = 0; i < 14; i++)
  16.             {
  17.                 int cislo = rnd.Next(0, 500);
  18.                 arr[i] = cislo;
  19.             }
  20.  
  21.             int j, n, l, p;
  22.             n = arr.Length;
  23.  
  24.             Console.Write("Původní pole : ");
  25.             for (j = 0; j < n; j++)
  26.             {
  27.                 Console.Write(arr[j] + " ");
  28.             }
  29.  
  30.             Console.WriteLine("");
  31.             int[] k = new int[1];
  32.             l = 0;
  33.             p = arr.Length - 1;
  34.  
  35.             if (arr.Length < 15 )
  36.             {
  37.                 InsertionSort(arr);
  38.             }
  39.  
  40.             else
  41.             {
  42.                 QuickSort(arr, l, p);
  43.             }
  44.  
  45.             Console.Write("Tolikrát jsem se musel juknout cotoe za číslo : ");
  46.             Console.WriteLine(count);
  47.             Console.Write("Takhle to dopadá : ");
  48.             for (j = 0; j < n; j++)
  49.             {
  50.                 Console.Write(arr[j] + " ");
  51.             }
  52.  
  53.         }
  54.         public static int Partition(int[] arr, int l, int p)
  55.         {
  56.             int pivot = arr[(l + p) / 2];
  57.  
  58.             while (l <= p)
  59.             {
  60.                 while (arr[l] < pivot)
  61.                 {
  62.                     l++;
  63.                     count++;
  64.                 }
  65.  
  66.                 while (arr[p] > pivot)
  67.                 {
  68.                     p--;
  69.                     count++;
  70.                 }
  71.  
  72.                 if (l <= p)
  73.                 {
  74.                     int temp = arr[p];
  75.                     arr[p] = arr[l];
  76.                     arr[l] = temp;
  77.                     l++;
  78.                     p--;
  79.                 }
  80.  
  81.             }
  82.  
  83.             return l;
  84.         }
  85.  
  86.         public static void QuickSort(int[] arr, int l, int p)
  87.         {
  88.             int index;
  89.             if (arr.Length > 1)
  90.             {
  91.                 index = Partition(arr, l, p);
  92.                 if (l < index - 1)
  93.                 {
  94.                     QuickSort(arr, l, index - 1);
  95.                 }
  96.  
  97.                 if (p > index)
  98.                 {
  99.                     QuickSort(arr, index, p);
  100.                 }
  101.             }
  102.         }
  103.  
  104.         public static void InsertionSort(int [] arr)
  105.         {
  106.             int n = arr.Length;
  107.             int t, iMin;
  108.             for (int i = 0; i < n - 1; i++)
  109.             {
  110.                 count++;
  111.                 iMin = i;
  112.                 for (int x = i + 1; x < n; x++)
  113.                 {
  114.                     if (arr[x] < arr[iMin])
  115.                     {
  116.                         iMin = x;
  117.                     }
  118.                 }
  119.  
  120.                 t = arr[iMin];
  121.                 arr[iMin] = arr[i];
  122.                 arr[i] = t;
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment