Массив после сортировки никакого отношения не имеет к рандомно заданному исходному - C#
Формулировка задачи:
В общем, получается так, что массив после сортировки никакого отношения не имеет к рандомно заданному исходному.
Подскажите, что не так ?
public static void Main(string[] args)
{
#region Простой выбор
{
Console.WriteLine("Программа для сортировки массива методом простого выбора ");
int[] mas = new int[200];
Random r = new Random();
for (int i = 0; i < mas.Length; i++)
{
mas[i] = r.Next(1, 200);
Console.Write(mas[i] + " ");
}
Console.WriteLine();
int perestanovka = 0;
for (int i = 0; i < mas.Length - 1; i++)
{
int min = i;
for (int j = i + 1; j < mas.Length; j++)
{
if (mas[j] < mas[min])
{
min = j;
}
}
if (min != i)
{
int temp = mas[i];
mas[i] = mas[min];
mas[min] = temp;
perestanovka++;
}
}
Console.WriteLine("BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOLWOI ABZATC\n");
for (int i = 0; i < mas.Length; i++)
{
Console.Write(mas[i] + " ");
}
Console.WriteLine();
Console.WriteLine("Количество обменов: {0}", perestanovka);
Console.ReadLine();
}
#endregion
#region Простой обмен
{
Console.WriteLine("Программа для сортировки массива методом простого обмена ");
int[] mas = new int[200];
Random r = new Random();
for (int i = 0; i < mas.Length; i++)
{
mas[i] = r.Next(1, 200);
Console.Write(mas[i] + " ");
}
Console.WriteLine();
int perestanovka = 0;
for (int i = 0; i < mas.Length - 1; i++)
{
for (int j = i + 1; j < mas.Length; j++)
{
if (mas[i] > mas[j])
{
int temp = mas[i];
mas[i] = mas[j];
mas[j] = temp;
perestanovka++;
}
}
}
Console.WriteLine("BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOLWOI ABZATC\n");
for (int i = 0; i < mas.Length; i++)
{
Console.Write(mas[i] + " ");
}
Console.WriteLine();
Console.WriteLine("Количество обменов: {0}", perestanovka);
Console.ReadLine();
}
#endregion
{
Console.WriteLine("Программа для сортировки массива методом вставки ");
int[] mas = new int[200];
Random r = new Random();
for (int i = 0; i < mas.Length; i++)
{
mas[i] = r.Next(1, 200);
Console.Write(mas[i] + " ");
}
Console.WriteLine();
int perestanovka = 0;
for (int i = 1; i < mas.Length; i++)
for (int j = i; j > 0 && mas[j] < mas[j - 1]; --j)
{
{ mas[j] = mas[j - 1]; }
}
perestanovka++;
Console.WriteLine("BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOLWOI ABZATC\n");
for (int i = 0; i < mas.Length; i++)
{
Console.Write(mas[i] + " ");
}
Console.WriteLine();
Console.WriteLine("Количество обменов: {0}", perestanovka);
Console.ReadLine();
}Решение задачи: «Массив после сортировки никакого отношения не имеет к рандомно заданному исходному»
textual
Листинг программы
Console.WriteLine("Программа для сортировки массива методом вставки ");
int[] mas = new int[5];
Random r = new Random();
for (int i = 0; i < mas.Length; i++)
{
mas[i] = r.Next(1, 200);
Console.Write(mas[i] + " ");
}
Console.WriteLine();
int perestanovka = 0;
for (int i = 1; i < mas.Length; i++)
{
for (int j = i; j > 0 && mas[j] < mas[j - 1]; --j)
{
int temp = mas[j];
mas[j] = mas[j - 1];
mas[j - 1] = temp;
}
}
perestanovka++;
Console.WriteLine("BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOLWOI ABZATC\n");
for (int i = 0; i < mas.Length; i++)
{
Console.Write(mas[i] + " ");
}
Console.WriteLine();
Console.WriteLine("Количество обменов: {0}", perestanovka);
Console.ReadLine();