j4ggi

bubblesort

Jan 12th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. //program.cs
  2. using System;
  3. using System.Windows.Forms;
  4.  
  5. namespace sort
  6. {
  7.     class Program
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Podaj ilość elementów do posortowania:");
  12.             int ilosc=int.Parse(Console.ReadLine());
  13.            
  14.             int[] tab=new int[ilosc];
  15.             Console.WriteLine("Podaj dolną granicę przedziału:");            
  16.             int dol=int.Parse(Console.ReadLine());
  17.             Console.WriteLine("Podaj górną granicę przedziału:");
  18.             int gora=int.Parse(Console.ReadLine());
  19.             sortuj.tab(ilosc, dol, gora, tab);
  20.             string tekst1=sortuj.tostr(ilosc, tab);
  21.             tab=sortuj.Bubble(ilosc, tab);
  22.             string tekst2=sortuj.tostr(ilosc, tab);
  23.             Application.Run( new Form1(tekst1) );
  24.             Application.Run( new Form1(tekst2) );
  25.            
  26.         }
  27.     }
  28. }
  29.  
  30. //form.cs
  31. using System;
  32. using System.Windows.Forms;
  33. namespace sort
  34. {
  35.     public partial class Form1 : Form
  36.     {
  37.         private Button ExitBtn;
  38.         private Label etykieta;
  39.    
  40.         private void ExitWindow(object sender, EventArgs e)
  41.            {
  42.             Close();
  43.            }
  44.            public void EWBtn()
  45.            {
  46.             ExitBtn = new Button();
  47.             ExitBtn.Parent = this;
  48.             ExitBtn.AutoSize = true;
  49. //określam położenie przycisku zależnie od rozmiaru okna (na dole na środku)
  50.             ExitBtn.Top = Height - ExitBtn.Height;
  51.             ExitBtn.Left = 20+ Width/2 - ExitBtn.Width;
  52.  
  53.             ExitBtn.Text = "Zamknij Okno";
  54. //przechwycenie zdarzenia (kliknięcia przycisku) oraz wywołanie metody odpowiedzialnej za zamknięcie okna
  55.             ExitBtn.Click += new System.EventHandler(ExitWindow);    
  56.            }
  57.            public void tekst(string txt)
  58.            {
  59.                etykieta = new Label();
  60.                etykieta.Parent = this;
  61.                etykieta.AutoSize = true;
  62.                etykieta.Top = 20;
  63.                etykieta.Left = 20;
  64.                etykieta.Text = txt;
  65.            }
  66.  
  67.            public Form1(string txt)
  68.         {
  69.             this.Text="Liczby";
  70.             this.AutoSize = true;
  71.             tekst(txt);
  72.             EWBtn();
  73.         }
  74.     }
  75. }
  76.  
  77. //sort.cs
  78. using System;
  79. namespace sort
  80. {
  81.         public  class sortuj
  82.         {  
  83.             public  static void tab(int ilosc, int dol, int gora, int[] tab)
  84.             {
  85.                 Random a = new Random();
  86.                 for (int i=0; i<ilosc; i++)
  87.                 {
  88.                     int x = a.Next(dol, gora);
  89.                     tab[i]= x;
  90.                 }
  91.             }
  92.             public static string tostr(int ilosc, int[] tab1)
  93.             {
  94.                 string str1="";
  95.                 for (int i=1; i<=ilosc; i++)
  96.                     str1+=tab1[i-1] + (i%((int) Math.Sqrt(ilosc))!=0 ? " ":"\n");
  97.  
  98.                 return str1;
  99.             }
  100.             public static int[] Bubble(int ilosc, int[] tab1)
  101.             {
  102.                 for(int i = 1; i < ilosc; ++i)
  103.                 {
  104.                     for(int j = ilosc - 1; j >= i; j--)
  105.                         if(tab1[j - 1] > tab1[j])
  106.                          {
  107.                             int x = tab1[j - 1];
  108.                             tab1[j - 1] = tab1[j];
  109.                             tab1[j] = x;
  110.                          }
  111.                 }
  112.                 return tab1;                       
  113.             }
  114.         }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment