Сделать один цикл while. Сортировка пузырьком c#
Формулировка задачи:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[0]);
string[] str=args[0].Split(new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
int[] x = new int[15];
for (int i=0; i<15; i++)
{
x[i]= Int32.Parse(str[i]);
}
Stopwatch watch = new Stopwatch();
watch.Start();
{
int b=0;
for (int i=0; i<15; i++)
{
int j =15-1;
while (j>1)
{
if (x[j]<x[j-1])
{
b=x[j];
x[j]=x[j-1];
x[j-1]=b;
}
j--;
}
}
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
for (int i=0; i<15; i++)
{
Console.WriteLine(x[i]);
}
Console.ReadKey();
}
}
}Решение задачи: «Сделать один цикл while. Сортировка пузырьком c#»
textual
Листинг программы
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[0]);
string[] str = args[0].Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
int[] x = new int[15];
for (int i = 0; i < 15; i++)
{
x[i] = Int32.Parse(str[i]);
}
Stopwatch watch = new Stopwatch();
watch.Start();
{
int stop = 0;
int tmp;
int i = 0;
while(stop < 14)
{
if(x[i] < x[i+1])
{
tmp = x[i+1];
x[i + 1] = x[i];
x[i] = tmp;
}
i++;
if(i == 14)
{
i = 0;
stop++;
}
}
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
for (i = 0; i < 15; i++)
{
Console.WriteLine(x[i]);
}
Console.ReadKey();
}
}
}
}