Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何让应用程序在Visual Studio中暂停并更新条形图?_C#_Visual Studio_Sorting_Windows Forms Designer - Fatal编程技术网

C# 如何让应用程序在Visual Studio中暂停并更新条形图?

C# 如何让应用程序在Visual Studio中暂停并更新条形图?,c#,visual-studio,sorting,windows-forms-designer,C#,Visual Studio,Sorting,Windows Forms Designer,我试图在排序过程中画出一个快速排序算法。下面是快速排序的代码 public void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } /* This function takes last element as a pivot,

我试图在排序过程中画出一个快速排序算法。下面是快速排序的代码

public void swap(int[] arr, int i, int j)
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            
        }

        /* This function takes last element as a pivot, places
        the pivot element at its correct position in sorted
        array, and places all smaller (smaller than pivot)
        to left of pivot and all greater elements to right
        of pivot */
        }

public int partition(int[] arr, int low, int high)
        {
            // pivot
            int pivot = arr[high];

            // Index of smaller element and
            // indicates the right position
            // of pivot found so far
            int i = (low - 1);

            for (int j = low; j <= high - 1; j++)
            {

                // If current element is smaller
                // than the pivot
                if (arr[j] < pivot)
                {

                    // Increment index of
                    // smaller element
                    i++;
                    swap(arr, i, j);
                }
            }
            swap(arr, i + 1, high);
            return (i + 1);
        }

public void quickSort(int[] arr, int low, int high)
        {
            if (low < high)
            {

                // pi is partitioning index, arr[p]
                // is now at right place
                int pi = partition(arr, low, high);

                // Separately sort elements before
                // partition and after partition
                quickSort(arr, low, pi - 1);
                quickSort(arr, pi + 1, high);
                updateBarChart(arr);
            }
        }
我能够在一步一步的过程中获得插入排序来绘制条形图的方法是使用
Task.Delay(some value)
,其中
some value
是一个基于轨迹条的整数。它会暂停指定的时间,然后绘制条形图,用户可以在面前查看排序过程。这种方法可以很好地用于插入排序,我将在本段下面列出代码,但是当我尝试使用快速排序函数时,它不起作用。数组将完全排序,然后在已排序后绘制。用户无法观看排序过程,这违背了排序可视化工具的目的。我假设这个问题源于这样一个事实,即快速排序过程涉及递归,这会使
Task.Delay
调用偏离轨道。这是我的假设,但我真的不知道

下面是我的插入排序函数

public async void InsertionSort(int[] intArray)
        {
            int i = 1;
            int j = 1;
            int placeHolder = 1;
            while (i < intArray.Length && stopStatus == false)
            {
                j = i;
                while (j > 0 && intArray[j - 1] > intArray[j])
                {
                    placeHolder = intArray[j];
                    intArray[j] = intArray[j - 1];
                    intArray[j - 1] = placeHolder;
                    j--;
                    updateBarChart(intArray);
                    await Task.Delay(sortSpeedBar.Value);
                }
                i++;
            }

如果您不介意阻塞主线程,可以尝试thread.Sleep(毫秒):

如果需要异步,还可以在新的分离线程上调用该函数

public async void InsertionSort(int[] intArray)
        {
            int i = 1;
            int j = 1;
            int placeHolder = 1;
            while (i < intArray.Length && stopStatus == false)
            {
                j = i;
                while (j > 0 && intArray[j - 1] > intArray[j])
                {
                    placeHolder = intArray[j];
                    intArray[j] = intArray[j - 1];
                    intArray[j - 1] = placeHolder;
                    j--;
                    updateBarChart(intArray);
                    await Task.Delay(sortSpeedBar.Value);
                }
                i++;
            }
Class Program
{
    static void Main()
    {
        int[] arr = {7, 4, 9, 1, 12, 5, 0, 6, 4};
        int arrayLength = arr.Length - 1;
        quickSort(arr, 0, arrayLength);
    }
}
public void quickSort(int[] arr, int low, int high)
    {
            ....
            quickSort(arr, low, pi - 1);
            quickSort(arr, pi + 1, high);
            updateBarChart(arr);
            Thread.Sleep(sortSpeedBar.Value);
        }
    }