快速排序程序进行排序,但永不停止

问题描述 投票:-1回答:1

the program running

这是我第一次在C#中实现快速排序应用程序,我认为它可以工作,但是没有出路,因此它会不断递归循环,有人可以帮助我并告诉我如何在不破坏和重建技术的情况下修复该程序吗?这是代码:

using System;
namespace quickso2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 12, 3, 1, 45, 16, 91, 21, 38 ,443,1212,53,2,1,0};
            sort ob = new sort();
            Console.WriteLine("before Sorting: ");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i]+ "  ");
            }

            ob.quicksorting(arr, -1, arr.Length - 1);
            Console.WriteLine("\n after Sorting: ");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i]+"  "  );
            }
        }
    }
    class sort
    {
        public int partition(int[] arr, int low, int high)
        {
            for (int i = low+1; i < high; i++)
            {

                if (arr[i] < arr[high])
                {
                    low++;
                    swap(ref arr[low], ref arr[i]);
                }
            }
            swap(ref arr[high], ref arr[low + 1]);
            //displaying sorting steps
            Console.WriteLine();
            for (int l = 0; l < arr.Length; l++) { 
            Console.Write(arr[l]+"  ");

            }
            return low + 1;
        }
        public void swap(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = temp;
        }
         public void quicksorting(int[] arr, int low, int high)
        {
            int pivot ;
            if (low < high)
            {
                pivot = partition(arr, low, high);
                quicksorting(arr, -1, pivot - 1);
                quicksorting(arr, pivot + 1, arr.Length - 1);
            }
        }
    }
}
c# quicksort
1个回答
0
投票

问题是由于数组索引在对快速排序函数的调用中传递。

无限循环问题的原因:

  ob.quicksorting(arr, 0, arr.Length-1);          // In main method

  quicksorting(arr, -1, pivot - 1);               // Recursive call #1

  quicksorting(arr, pivot + 1, arr.Length - 1);   // Recursive call #2

这里是有效的解决方案,具有更新的数组索引:

using System;
namespace quickso2 {

  public class Program {
        public static void Main(string[] args) {
            int[] arr = { 12, 3, 1, 45, 16, 91, 21, 38 ,443,1212,53,2,1,0};
            sort ob = new sort();
            Console.WriteLine("before Sorting: ");
            for (int i = 0; i < arr.Length; i++) {
                Console.Write(arr[i]+ "  ");
            }
            ob.quicksorting(arr, 0, arr.Length-1);
            Console.WriteLine("\n after Sorting: ");
            for (int i = 0; i < arr.Length; i++) {
                Console.Write(arr[i]+"  "  );
            }
        }
  }

  class sort {
        public int partition(int[] arr, int low, int high) {
            int pivot = arr[low];
            while (true) {
                while (arr[low] < pivot) {
                    low++;
                }

                while (arr[high] > pivot) {
                    high--;
                }

                if (low < high) {
                    if (arr[low] == arr[high]) return high;
                    int temp = arr[low];
                    arr[low] = arr[high];
                    arr[high] = temp;
                }
                else {
                    return high;
                }
            }
        }

        public void quicksorting(int[] arr, int low, int high) {
            if (low < high) {
                int pivot = partition(arr, low, high);
                if (pivot > 1) {
                    quicksorting(arr, low, pivot - 1);
                }
                if (pivot + 1 < high) {
                    quicksorting(arr, pivot + 1, high);
                }
            }
        }
    }
}

输出:

before Sorting: 
12  3  1  45  16  91  21  38  443  1212  53  2  1  0  
 after Sorting: 
0  1  1  2  3  12  16  21  38  45  53  91  443  1212
© www.soinside.com 2019 - 2024. All rights reserved.