vojta249

Maturita_18 - select

May 4th, 2022 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Program
  4. {
  5.     class Program
  6.     {
  7.         static void selectSort(int[] pole)
  8.         {
  9.             //projede cele pole, zjisti index iMin nejmensiho cisla v poli
  10.             for (int j = 0; j <= pole.Length - 1; j++)
  11.             {
  12.                 int iMin = j;
  13.                 for (int i = j + 1; i <= pole.Length - 1; i++)
  14.                 {
  15.                     if (pole[i] < pole[iMin])
  16.                         iMin = i;
  17.                 }
  18.                 //prohozeni cisel pole[j] a pole[iMin]
  19.                 /*takto se postupne dostavaji nejmensi cisla doleva na index j
  20.                 (nejmensi cislo na index 0, 2. nejmensi na index 1...)*/
  21.                 int t = pole[j];
  22.                 pole[j] = pole[iMin];
  23.                 pole[iMin] = t;
  24.             }
  25.         }
  26.         static void vypisPole(int[] pole)
  27.         {
  28.             for (int i = 0; i < pole.Length; i++)
  29.                 Console.Write(pole[i] + " ");
  30.             Console.WriteLine();
  31.         }
  32.         static void Main(string[] args)
  33.         {
  34.  
  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);
  39.  
  40.  
  41.             Console.WriteLine("Původní pole:");
  42.             vypisPole(pole);
  43.             Console.WriteLine("\n");
  44.             selectSort(pole);
  45.             Console.WriteLine("\nSetřízené pole");
  46.             vypisPole(pole);
  47.             Console.ReadKey();
  48.         }
  49.     }
  50. }
Add Comment
Please, Sign In to add comment