vojta249

Maturita_20 - bubláček

May 5th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Program
  4. {
  5.     class Program
  6.     {
  7.         static void bubbleSort(int[] pole)
  8.         {
  9.             //prvni cyklus pole length krat
  10.             for (int j = 0; j < pole.Length; j++)
  11.             {
  12.                 for (int i = 0; i < pole.Length - 1; i++)
  13.                 {
  14.                     //porovnani sousedicich cisel
  15.                     if (pole[i] > pole[i + 1])
  16.                     {
  17.                         //pokud je cislo vlevo vetsi, vymeni se se sousedicim cislem vpravo
  18.                         //takto se postupne dostavaji nejvetsi cisla na konec
  19.                         int t = pole[i];
  20.                         pole[i] = pole[i + 1];
  21.                         pole[i + 1] = t;
  22.  
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         static void vypisPole(int[] pole)
  28.         {
  29.             for (int i = 0; i < pole.Length; i++)
  30.                 Console.Write(pole[i] + " ");
  31.         }
  32.         static void Main(string[] args)
  33.         {
  34.             //pole 100 nahodnych cisel
  35.             int[] pole = new int[100];
  36.             Random random = new Random();
  37.             for (int i = 0; i < pole.Length; i++)
  38.                 pole[i] = random.Next(0, 100); //rozsah cisel
  39.             //vypsani cisel z pole
  40.             Console.WriteLine("Původní pole: ");
  41.             for (int i = 0; i < pole.Length; i++)
  42.                 Console.Write(pole[i] + " ");
  43.             Console.WriteLine("\n");
  44.             bubbleSort(pole);
  45.             Console.WriteLine("\nSetřízené pole: ");
  46.             vypisPole(pole);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment