Fle2x

Untitled

May 5th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2.  
  3. public class Example
  4. {
  5.     public static void Main()
  6.     {
  7.         int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  8.         Console.WriteLine("До перемешивания: ");
  9.         BeforeShuffle(array);
  10.         Console.WriteLine("\nПосле перемешивания: ");
  11.         Shuffle(array);
  12.         Console.ReadKey();
  13.     }
  14.  
  15.     static void BeforeShuffle(int[] array)
  16.     {
  17.         for (int i = 0; i < array.Length; i++)
  18.         {
  19.             Console.Write(array[i] + " ");
  20.         }
  21.     }
  22.  
  23.     static void Shuffle(int[] array)
  24.     {
  25.         Random random = new Random();
  26.         for (int i = 0; i < array.Length; i++)
  27.         {
  28.             int currenIndex = array[i];
  29.             int randomIndex = random.Next(i, array.Length);
  30.             array[i] = array[randomIndex];
  31.             array[randomIndex] = currenIndex;
  32.             Console.Write(array[i] + " ");
  33.         }
  34.     }
  35. }
Add Comment
Please, Sign In to add comment