j4ggi

C# sort

Jan 25th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.20 KB | None | 0 0
  1. // Plik 1 - Program.cs
  2. using System;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6.  
  7. namespace ConsoleApplication3
  8. {
  9.     class Program
  10.     {
  11.         static Form1 Okno;
  12.         static int[] tab0, tab1, tab2, tab3, tab4;
  13.         static void bubblesort()
  14.         {
  15.             Okno.Invoke(new Action(delegate()
  16.             {
  17.                 Okno.TxtBx2();
  18.             }));
  19.             DateTime start = DateTime.Now;
  20.             sortuj.Bubble(tab1);
  21.             DateTime stop = DateTime.Now;
  22.             TimeSpan czas = stop - start;
  23.             string a = sortuj.tostr(tab1);
  24.             Okno.Invoke(new Action(delegate()
  25.             {
  26.                 Okno.s1.Text = a;
  27.                 Okno.czas(1, czas);
  28.                 Okno.Update();
  29.             }));
  30.         }
  31.         static void selectionsort()
  32.         {
  33.             Okno.Invoke(new Action(delegate()
  34.             {
  35.                 Okno.TxtBx3();
  36.             }));
  37.             DateTime start = DateTime.Now;
  38.             sortuj.Selection(tab2);
  39.             DateTime stop = DateTime.Now;
  40.             TimeSpan czas = stop - start;
  41.             string a = sortuj.tostr(tab2);
  42.             Okno.Invoke(new Action(delegate()
  43.             {
  44.                 Okno.s2.Text = a;
  45.                 Okno.czas(2, czas);
  46.                 Okno.Update();
  47.             }));
  48.         }
  49.         static void insertionsort()
  50.         {
  51.             Okno.Invoke(new Action(delegate()
  52.             {
  53.                 Okno.TxtBx4();
  54.             }));
  55.             DateTime start = DateTime.Now;
  56.             sortuj.Insertion(tab3);
  57.             DateTime stop = DateTime.Now;
  58.             TimeSpan czas = stop - start;
  59.             string a = sortuj.tostr(tab3);
  60.             Okno.Invoke(new Action(delegate()
  61.             {
  62.                 Okno.s3.Text = a;
  63.                 Okno.czas(3, czas);
  64.                 Okno.Update();
  65.             }));
  66.         }
  67.         static void quicksort()
  68.         {
  69.             Okno.Invoke(new Action(delegate()
  70.             {
  71.                 Okno.TxtBx5();
  72.             }));
  73.             DateTime start = DateTime.Now;
  74.             sortuj.Quick(tab4, 0, tab4.Length - 1);
  75.             DateTime stop = DateTime.Now;
  76.             TimeSpan czas = stop - start;
  77.             string a = sortuj.tostr(tab4);
  78.             Okno.Invoke(new Action(delegate()
  79.             {
  80.                 Okno.czas(4, czas);
  81.                 Okno.s4.Text = a;
  82.                 Okno.Update();
  83.             }));
  84.         }
  85.         static void StworzOkno()
  86.         {
  87.             Application.Run(Okno);
  88.         }
  89.  
  90.         static void Main()
  91.         {
  92.             Console.WriteLine("Wprowadź liczbę elementów do posortowania:");
  93.             int r = int.Parse(Console.ReadLine());
  94.             Console.WriteLine("Wprowadź dolną granicę:");
  95.             int dol = int.Parse(Console.ReadLine());
  96.             Console.WriteLine("Wprowadź górną granicę:");
  97.             int gora = int.Parse(Console.ReadLine());
  98.             int j=0;
  99.             tab0 = new int[r];
  100.             Console.WriteLine("Czekaj, trwa tworzenie tablicy losowych liczb");
  101.             sortuj.tab(r, dol, gora, tab0);
  102.             tab1 = new int[r];
  103.             tab2 = new int[r];
  104.             tab3 = new int[r];
  105.             tab4 = new int[r];
  106.             tab0.CopyTo(tab1, 0);
  107.             tab0.CopyTo(tab2, 0);
  108.             tab0.CopyTo(tab3, 0);
  109.             tab0.CopyTo(tab4, 0);
  110.             Okno = new Form1(sortuj.tostr(tab0));
  111.             Thread[] ThreadTable = new Thread[5];
  112.             ThreadTable[0] = new Thread(StworzOkno);
  113.             ThreadTable[1] = new Thread(bubblesort);
  114.             ThreadTable[2] = new Thread(insertionsort);
  115.             ThreadTable[3] = new Thread(selectionsort);
  116.             ThreadTable[4] = new Thread(quicksort);
  117.             foreach (Thread i in ThreadTable)
  118.             {
  119.                 i.Start();
  120.                 Console.WriteLine("Uruchamiam Wątek {0} - " + (j == 0 ? "Tworzę okno" : "Rozpoczynam sortowanie metodą nr {1}"), ++j, j - 1);
  121.             }
  122.         }
  123.     }
  124. }
  125.  
  126.  
  127. // PLIK 2 - CLASS1.cs
  128.  
  129. using System;
  130. namespace ConsoleApplication3
  131. {
  132.     public class sortuj
  133.     {
  134.         public static void tab(int ilosc, int dol, int gora, int[] tab)
  135.         {
  136.             Random a = new Random();
  137.             for (int i = 0; i < ilosc; i++)
  138.             {
  139.                 int x = a.Next(dol, gora);
  140.                 tab[i] = x;
  141.             }
  142.         }
  143.         public static string tostr(int[] tab1)
  144.         {
  145.             string str1 = "";
  146.             for (int i = 1; i <= tab1.Length; i++)
  147.                 str1 += tab1[i - 1] + " ";
  148.  
  149.             return str1;
  150.         }
  151.         public static void Bubble(int[] tab)
  152.         {
  153.             for (int i = 1; i < tab.Length; ++i)
  154.             {
  155.                 for (int j = tab.Length - 1; j >= i; j--)
  156.                     if (tab[j - 1] > tab[j])
  157.                     {
  158.                         int x = tab[j - 1];
  159.                         tab[j - 1] = tab[j];
  160.                         tab[j] = x;
  161.                     }
  162.             }
  163.         }
  164.         public static void Selection(int[] tab)
  165.         {
  166.             for (int i = 0; i < tab.Length - 1; i++)
  167.             {
  168.                 for (int j = i; j < tab.Length; j++)
  169.                     if (tab[j] < tab[i])
  170.                     {
  171.                         int a = tab[j];
  172.                         tab[j] = tab[i];
  173.                         tab[i] = a;
  174.                     }
  175.             }
  176.         }
  177.         public static void Insertion(int[] tab)
  178.         {
  179.             for (int i = 1; i < tab.Length; i++)
  180.                 for (int j = i; j - 1 >= 0 && tab[j] < tab[j - 1]; j--)
  181.                 {
  182.                     int a = tab[j];
  183.                     tab[j] = tab[j - 1];
  184.                     tab[j - 1] = a;
  185.                 }
  186.         }
  187.         public static void Quick(int[] tab, int left, int right)
  188.         {
  189.             int i=left;
  190.             int j=right;
  191.             int k=(left+right)/2;
  192.             int x=tab[k];
  193.             do{
  194.                 while(tab[i]<x) i++;
  195.                 while(tab[j]>x) j--;
  196.                 if(i<=j)
  197.                 {    
  198.                     int a=tab[j];
  199.                     tab[j]=tab[i];
  200.                     tab[i]=a;
  201.                     i++;
  202.                     j--;
  203.                 }
  204.             }while(i<=j);
  205.             if(left<j) Quick(tab,left,j);
  206.             if(right>i) Quick(tab,i,right);  
  207.         }
  208.     }
  209. }
  210.  
  211.  
  212. //PLIK 3 - Form1.cs
  213.  
  214. using System;
  215. using System.Drawing;
  216. using System.Windows.Forms;
  217.  
  218. namespace ConsoleApplication3
  219. {
  220.     public partial class Form1 : Form
  221.     {
  222.         private Button ExitBtn;
  223.         private Label[] e = new Label[5];
  224.         public TextBox start, s1, s2, s3, s4;
  225.  
  226.         private void ExitWindow(object sender, EventArgs e)
  227.         {
  228.             Close();
  229.         }
  230.         public void EWBtn()
  231.         {
  232.             ExitBtn = new Button();
  233.             ExitBtn.Parent = this;
  234.             ExitBtn.AutoSize = true;
  235.             ExitBtn.Top = 530;
  236.             ExitBtn.Left = 20 + Width / 2 - ExitBtn.Width;
  237.             ExitBtn.Text = "Zamknij Okno";
  238.             ExitBtn.Click += new System.EventHandler(ExitWindow);
  239.         }
  240.  
  241.         public void tekst(int n, string txt, int top, int left)
  242.         {
  243.             n -= 1;
  244.             e[n] = new Label();
  245.             e[n].Parent = this;
  246.             e[n].AutoSize = true;
  247.             e[n].Top = top;
  248.             e[n].Left = left;
  249.             e[n].Text = txt;
  250.         }
  251.         public void czas(int n, TimeSpan t)
  252.         {
  253.             tekst(n, Convert.ToDecimal(t.TotalMilliseconds)/1000 + " sekund", 490, 370-(n-1)*120);
  254.         }
  255.  
  256.         public void TxtBx(string ss)
  257.         {
  258.             tekst(1, "Liczby przed sortowaniem:", 10, 10);
  259.             start = new System.Windows.Forms.TextBox();
  260.             start.Parent = this;
  261.             start.Top = 30;
  262.             start.Left = 10;
  263.             start.Size = new Size(500, 130);
  264.             start.AcceptsReturn = true;
  265.             start.Multiline = true;
  266.             start.Text = ss;
  267.             start.ScrollBars = ScrollBars.Vertical;
  268.         }
  269.  
  270.         public void TxtBx2()
  271.         {
  272.             tekst(2, "BubbleSort:", 170, 370);
  273.             s1 = new System.Windows.Forms.TextBox();
  274.             s1.Parent = this;
  275.             s1.Top = 190;
  276.             s1.Left = 370;
  277.             s1.Size = new Size(120, 300);
  278.             s1.AcceptsReturn = true;
  279.             s1.Multiline = true;
  280.             s1.ScrollBars = ScrollBars.Vertical;
  281.         }
  282.  
  283.         public void TxtBx3()
  284.         {
  285.             tekst(3, "SelectionSort:", 170, 250);
  286.             s2 = new System.Windows.Forms.TextBox();
  287.             s2.Parent = this;
  288.             s2.Top = 190;
  289.             s2.Left = 250;
  290.             s2.Size = new Size(120, 300);
  291.             s2.AcceptsReturn = true;
  292.             s2.Multiline = true;
  293.             s2.ScrollBars = ScrollBars.Vertical;
  294.         }
  295.  
  296.         public void TxtBx4()
  297.         {
  298.             tekst(4, "InsertionSort:", 170, 130);
  299.             s3 = new System.Windows.Forms.TextBox();
  300.             s3.Parent = this;
  301.             s3.Top = 190;
  302.             s3.Left = 130;
  303.             s3.Size = new Size(120, 300);
  304.             s3.AcceptsReturn = true;
  305.             s3.Multiline = true;
  306.             s3.ScrollBars = ScrollBars.Vertical;
  307.         }
  308.  
  309.         public void TxtBx5()
  310.         {
  311.             tekst(5, "QuickSort:", 170, 10);
  312.             s4 = new System.Windows.Forms.TextBox();
  313.             s4.Parent = this;
  314.             s4.Top = 190;
  315.             s4.Left = 10;
  316.             s4.Size = new Size(120, 300);
  317.             s4.AcceptsReturn = true;
  318.             s4.Multiline = true;
  319.             s4.ScrollBars = ScrollBars.Vertical;
  320.         }
  321.  
  322.         public Form1(string txt)
  323.         {
  324.             this.Text = "Sortowanie";
  325.             this.AutoSize = true;
  326.             TxtBx(txt);
  327.             EWBtn();
  328.         }
  329.     }
  330.  
  331. }
  332.  
  333. //INSTRUKCJA
  334.  
  335. //zapisać te trzy pliki
  336.  
  337. //stworzyć nowe rozwiązanie czy tam solucje
  338. //aplikacje konsoli
  339. //potem
  340. //po lewej
  341. //w oknie projekty
  342. //na nazwie (pogrubionej) kliknąć prawym
  343. //i dodaj->istniejący element
  344. //dodać te 3 pliki
  345. //i dodać referencje
  346. //system.drawing
  347. //system.windows.forms
  348. //system.threading
Advertisement
Add Comment
Please, Sign In to add comment