Advertisement
xxeell

Stalin_sort

Nov 4th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace StalinArray
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Random r = new Random();
  13.             int min_count = 10;
  14.             int max_count = 21;
  15.             int l = r.Next(min_count, max_count);
  16.             int[] a = new int[l];
  17.  
  18.             for (int i = 0; i < l; i++) a[i] = i + 1;
  19.  
  20.             foreach (int i in a) Console.Write("{0} ", i);
  21.             Console.WriteLine();
  22.  
  23.             Console.ForegroundColor = ConsoleColor.Green;
  24.             Console.WriteLine("Перемешивание...");
  25.             int magic_count = r.Next(min_count, max_count);
  26.             Console.WriteLine("{0} замен...", magic_count);
  27.             Console.ForegroundColor = ConsoleColor.Gray;
  28.            
  29.             int t, i1, i2;
  30.             for (int i = 0; i < magic_count; i++)
  31.             {
  32.                 i1 = r.Next(0, l);
  33.                 i2 = r.Next(0, l);
  34.  
  35.                 t = a[i1];
  36.                 a[i1] = a[i2];
  37.                 a[i2] = t;
  38.             }
  39.  
  40.             foreach (int i in a) Console.Write("{0} ", i);
  41.             Console.WriteLine();
  42.  
  43.             Console.ForegroundColor = ConsoleColor.Green;
  44.             Console.WriteLine("Сдвиги...");
  45.             Console.ForegroundColor = ConsoleColor.Gray;
  46.  
  47.             List<List<int>> tarr = new List<List<int>>();
  48.             tarr.Add(new List<int>(a));
  49.  
  50.             for (int i = 0; i < tarr.Count; i++)
  51.             {
  52.                 for (int q = 1; q < tarr[i].Count; q++)
  53.                 {
  54.                     if (tarr[i][q] < tarr[i][q - 1])
  55.                     {
  56.                         if (i + 1 >= tarr.Count) tarr.Add(new List<int>());
  57.  
  58.                         tarr[i + 1].Add(tarr[i][q]);
  59.                         tarr[i].RemoveAt(q);
  60.                         q--;
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             for (int i = 0; i < tarr.Count; i++)
  66.             {
  67.                 for (int q = 0; q < tarr[i].Count; q++)
  68.                 {
  69.                     Console.Write("{0} ", tarr[i][q]);
  70.                 }
  71.                 Console.WriteLine();
  72.             }
  73.  
  74.             Console.ForegroundColor = ConsoleColor.Green;
  75.             Console.WriteLine("Волшебная магия...");
  76.             Console.ForegroundColor = ConsoleColor.Gray;
  77.  
  78.             List<int> res = new List<int>();
  79.             int ti;
  80.  
  81.             while (tarr.Count > 0)
  82.             {
  83.                 if (tarr[0].Count < 1)
  84.                 {
  85.                     tarr.RemoveAt(0);
  86.                     continue;
  87.                 }
  88.  
  89.                 ti = 0;
  90.                 for (int i = 1; i < tarr.Count; i++)
  91.                 {
  92.                     if(tarr[i].Count < 1)
  93.                     {
  94.                         tarr.RemoveAt(i);
  95.                         i--;
  96.                         continue;
  97.                     }
  98.  
  99.                     if (tarr[ti][0] > tarr[i][0]) ti = i;
  100.                 }
  101.  
  102.                 res.Add(tarr[ti][0]);
  103.                 tarr[ti].RemoveAt(0);
  104.             }
  105.  
  106.             foreach (int i in res) Console.Write("{0} ", i);
  107.             Console.WriteLine();
  108.  
  109.             Console.ReadKey();
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement