C# 如何停止这段代码的迭代我正在使用选择排序算法,如果数组已经排序,我希望迭代停止

C# 如何停止这段代码的迭代我正在使用选择排序算法,如果数组已经排序,我希望迭代停止,c#,arrays,algorithm,boolean,C#,Arrays,Algorithm,Boolean,这是我当前的代码,我放置了额外的foreach以便可以看到迭代。我需要的代码停止后,数组已经排序的权利,我认为是把一个休息;如果程序打印同一数组两次,则使用布尔值,因为这意味着没有更改,并且数组已经排序 static void Main(string[] args) { int[] array = { 12, 114, 5, 10, 14 }; foreach (int item in array) Console.Write("\t&qu

这是我当前的代码,我放置了额外的foreach以便可以看到迭代。我需要的代码停止后,数组已经排序的权利,我认为是把一个休息;如果程序打印同一数组两次,则使用布尔值,因为这意味着没有更改,并且数组已经排序

  static void Main(string[] args)
     {
        int[] array = { 12, 114, 5, 10, 14 };
        foreach (int item in array) Console.Write("\t" + item);
        Console.WriteLine("\n Press Enter key to start the selection...");
        Console.ReadLine();


        for (int i = 0; i < array.Length - 1; i++)
        {
            int indexofSmallests = i;
            //comparison to find the smallest element in the unsorted array

            for (int j = i + 1; j < array.Length; j++)
            {
                if (array[j] < array[indexofSmallests])
                 indexofSmallests = j;
            }
            //swapping part of the program
            int temp = array[i]; // temporarily storing the first element
            array[i] = array[indexofSmallests]; // copying of the smallest element
            array[indexofSmallests] = temp;

            
            foreach (int item in array) Console.Write("\t" + item);//Code so i can see the iterations

            Console.ReadLine();
        }
        Console.WriteLine("\n Sorted Array: ");// printing the final iterations
        foreach (int item in array) Console.Write("\t" + item);
        Console.ReadLine();
static void Main(字符串[]args)
{
int[]数组={12,114,5,10,14};
foreach(数组中的int项)控制台。Write(“\t”+项);
Console.WriteLine(“\n按Enter键开始选择…”);
Console.ReadLine();
for(int i=0;i
您需要检查内部循环期间是否需要排序:

bool stopSort = true;

for (int j = i + 1; j < array.Length; j++)
{
    if (array[j] < array[indexofSmallests])
        indexofSmallests = j;
    if (array[j] < array[i])
        stopSort = false;
}

if (stopSort)
    break;
bool stopSort=true;
对于(int j=i+1;j
目标是什么?只是为了学习?通常您会使用内置排序方法,如果您想要非常简单的排序,插入排序具有更好的最佳案例性能。