Advertisement
Fle2x

Untitled

May 5th, 2020
95
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.  
  4. public class Example
  5. {
  6.     public static void Main()
  7.     {
  8.         int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  9.         Console.WriteLine("До перемешивания: ");
  10.         BeforeShuffle(array);
  11.         Console.WriteLine("\nПосле перемешивания: ");
  12.         Shuffle(array);
  13.         Console.ReadKey();
  14.     }
  15.  
  16.     static void BeforeShuffle(int[] array)
  17.     {
  18.  
  19.         for (int i = 0; i < array.Length; i++)
  20.         {
  21.             Console.Write(array[i] + " ");
  22.         }
  23.     }
  24.  
  25.     static void Shuffle(int[] array)
  26.     {
  27.         for (int i = 0; i < array.Length; i++)
  28.         {
  29.             Random random = new Random();
  30.             int currenValue = array[i];
  31.             int randomIndex = random.Next(i, array.Length);
  32.             array[i] = array[randomIndex];
  33.             array[randomIndex] = currenValue;
  34.             Console.Write(array[i] + " ");
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement