Facebook
From counting, 5 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace InsertSort
  8. {
  9.     class Program
  10.     {
  11.         public static void InsertSort(int[] tab)
  12.         {
  13.             int n = tab.Length;
  14.             int obecny;
  15.             int inny;
  16.             for (int i=1;i<n;i++)
  17.             {
  18.                 obecny = tab[i];
  19.                 inny = i;
  20.                 while (inny > 0 && obecny < tab[inny - 1])
  21.                 {
  22.                     tab[inny] = tab[inny - 1];
  23.                     inny--;
  24.                 }
  25.                 tab[inny] = obecny;
  26.             }
  27.         }
  28.  
  29.         public static void Swap(int[] tab, int index1, int index2)
  30.         {
  31.             int bufor = tab[index1];
  32.             tab[index1] = tab[index2];
  33.             tab[index2] = bufor;
  34.         }
  35.  
  36.         public static void Select(int[] tab)
  37.         {
  38.             int min;
  39.             int n = tab.Length;
  40.  
  41.             for (int i=0;i<n - 1;i++)
  42.             {
  43.                 min = i;
  44.                 for (int j = i+1;j<n; j++)
  45.                 {
  46.                     if (tab[min] > tab[j])
  47.                         min = j;
  48.                 }
  49.                 Swap(tab, min, i);
  50.             }
  51.         }
  52.  
  53.         public static void Zliczanie(int[] tab)
  54.         {
  55.             int n = tab.Length;
  56.             int i, k, c;
  57.             int[] pomocnicza = new int[100];
  58.             for (i = 0; i < 100; i++)
  59.                 pomocnicza[i] = 0;
  60.             for (i = 0; i < n; i++)
  61.                 pomocnicza[tab[i]]++;
  62.             c = 0;
  63.             for (i=0;i<100;i++)
  64.             {
  65.                 if (pomocnicza[i] > 0)
  66.                 {
  67.                     for (k=1;k<pomocnicza[i]+1;k++)
  68.                     {
  69.                         tab[c] = i;
  70.                         c++;
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.  
  76.         static void Main(string[] args)
  77.         {
  78.             Random r = new Random();
  79.             int[] tab = new int[20];
  80.             for (int i=0;i<tab.Length;i++)
  81.             {
  82.                 tab[i] = r.Next(100);
  83.                 Console.Write(tab[i] + " ");
  84.             }
  85.             Console.WriteLine();
  86.             Zliczanie(tab);
  87.             for (int i = 0; i < tab.Length; i++)
  88.             {
  89.                 Console.Write(tab[i] + " ");
  90.             }
  91.             Console.ReadKey();
  92.         }
  93.     }
  94. }
  95.