You are on page 1of 4

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

public class Program


{

static void Main(string[] args){

Console.WriteLine("1. Alocarea aleatorie");


Console.WriteLine("2. Alocarea de la tastatura");

int N;
int.TryParse(Console.ReadLine(), out N);
Thread.Sleep(1000);
Console.Clear();
int[] arr = { 0 };

switch (N)
{
case 1:
arr = new int[10];
Random random = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = random.Next(0, 100);
}
break;
case 2:
Console.WriteLine("Introduceti numarul de elemente ");
int nrOfElements;
int.TryParse(Console.ReadLine(), out nrOfElements);

Console.WriteLine("Introduceti elementele separate prin spatiu


", nrOfElements);
string line = Console.ReadLine();
string[] tokens = line.Split(' ');
for (int i = 0; i < nrOfElements; i++)
{
arr = Array.ConvertAll(tokens, int.Parse);
}

break;
default:
Console.WriteLine("Valoare incorecta");
break;

Console.SetCursorPosition(2, 2);
for (int i = 0; i < arr.Length; i++)
{

for (int j = i + 1; j < arr.Length; j++)


{

writeNum(arr, getPosition(arr, i), i, 1);

writeNums(arr, i, j, ConsoleColor.Blue);

writeNum(arr, getPosition(arr, j), j, 3);

Thread.Sleep(1000);

if (arr[i] > arr[j])


{
for (int k = 0; k < Math.Abs(i - j); k++)
{
writeNum(arr, getPosition(arr, i + k), i, 1);

writeNum(arr, getPosition(arr, j - k), j, 3);

Thread.Sleep(100);
}

Schimb(ref arr[i], ref arr[j]);

Console.Clear();
writeNums(arr, i, j, ConsoleColor.Red);
Thread.Sleep(1000);
}
}
}

writeResult(arr, 6);
Console.ReadKey();
}

public static void Schimb(ref int a, ref int b)


{
int temp=a;
a=b;
b=temp;
}

public static void ClearCurrentConsoleLine()


{
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
}

public static void writeNums(int[] arr, int i, int j, ConsoleColor fgColor)


{
Console.SetCursorPosition(0, 2);
foreach (var num in arr)
{
if (num == arr[i] || num == arr[j])
{
Console.ForegroundColor = fgColor;
}

Console.Write("{0} ", num);


Console.ForegroundColor = ConsoleColor.White;

}
}

public static void writeNum(int[] arr, int pos, int index, int line)
{
Console.SetCursorPosition(0, line);
ClearCurrentConsoleLine();
Console.SetCursorPosition(pos, line);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(arr[index]);
Console.ForegroundColor = ConsoleColor.White;
}

public static int getNumLength(int num)


{
int counter = 1;
while (num > 10)
{
num /= 10;
counter++;
}
return counter;
}

public static int getPosition(int[] arr, int index)


{
int position = 0;
for (int i = 0; i < index; i++)
{
position += getNumLength(arr[i]) + 1;
}
return position;
}

public static void writeResult(int[] arr, int nBlinks)


{
ConsoleColor color = ConsoleColor.White;
while (nBlinks > 0)
{
Console.Clear();
Console.SetCursorPosition(0, 2);
color = color == ConsoleColor.Green ? ConsoleColor.White :
ConsoleColor.Yellow;
Console.ForegroundColor = color;
foreach (var num in arr)
{
Console.Write("{0} ", num);
}
nBlinks--;
Thread.Sleep(400);
}
}
}

You might also like