Comments on: Quicksort Algorithm in C# https://code-maze.com/csharp-quicksort-algorithm/ Learn. Code. Succeed. Tue, 11 Oct 2022 11:03:16 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: gerik https://code-maze.com/csharp-quicksort-algorithm/#comment-5642 Thu, 12 May 2022 05:31:04 +0000 https://drafts.code-maze.com/?p=68063#comment-5642 Can you advise other site where algorithms will be as good explaned as here

]]>
By: MarinkoSpasojevic https://code-maze.com/csharp-quicksort-algorithm/#comment-5422 Wed, 06 Apr 2022 20:39:58 +0000 https://drafts.code-maze.com/?p=68063#comment-5422 In reply to Chris J Breisch.

Thank you Chris. The author of the article is looking into this problem. Thanks for the suggestions. And of course for the code.

]]>
By: Chris J Breisch https://code-maze.com/csharp-quicksort-algorithm/#comment-5421 Wed, 06 Apr 2022 20:10:33 +0000 https://drafts.code-maze.com/?p=68063#comment-5421 In reply to Chris J Breisch.

This fixes both of your problems, I think:

    public int Partition(int[] array, int leftIndex, int rightIndex)
    {
      var pivot = array[leftIndex];
      var start = leftIndex;

      while (true)
      {
        while (array[leftIndex] < pivot)
        {
          leftIndex++;
        }
        while (rightIndex > start && array[rightIndex] >= pivot)
        {
          rightIndex--;
        }
        if (leftIndex < rightIndex)
        {
          var tempVar = array[rightIndex];
          array[rightIndex] = array[leftIndex];
          array[leftIndex] = tempVar;
        }
        else
        {
          return rightIndex;
        }
      }
    }

    public void SortArray(int[] array, int leftIndex, int rightIndex, string arrayName)
    {
      if (leftIndex >= rightIndex) return;

      Stack<Tuple<int, int>> stack = new Stack<Tuple<int, int>>();
      int pivot;

      while (true)
      {
        if (leftIndex < rightIndex)
        {
          pivot = Partition(array, leftIndex, rightIndex);
          stack.Push(new Tuple<int, int>(pivot + 1, rightIndex));
          rightIndex = pivot;
        }
        else if (stack.Any())
        {
          var tuple = stack.Pop();
          leftIndex = tuple.Item1;
          rightIndex = tuple.Item2;
        }
        else
          break;
      }
    }
]]>
By: Chris J Breisch https://code-maze.com/csharp-quicksort-algorithm/#comment-5416 Tue, 05 Apr 2022 20:52:28 +0000 https://drafts.code-maze.com/?p=68063#comment-5416 In reply to Chris J Breisch.

You also get a StackOverflowException if you run it on a sorted list containing 20,000 items.

new QuickSortMethods().SortArray(GenerateSortedNumber(20000), 0, 19999, "sortedTest");
]]>
By: Chris J Breisch https://code-maze.com/csharp-quicksort-algorithm/#comment-5413 Tue, 05 Apr 2022 14:46:06 +0000 https://drafts.code-maze.com/?p=68063#comment-5413 In reply to Chris J Breisch.

My mistake. The second “problem” isn’t a problem. After the swap, that expression can be true.

but it still fails if the values are not unique.

]]>
By: Chris J Breisch https://code-maze.com/csharp-quicksort-algorithm/#comment-5412 Tue, 05 Apr 2022 14:37:52 +0000 https://drafts.code-maze.com/?p=68063#comment-5412 You have a couple of problems. Probably related, but I haven’t had a chance to dig into it yet to be sure.

The algorithm gets caught in an infinite loop if there are duplicate items in the array. Test case:

int[] intTest = { 73, 57, 49, 99, 133, 20, 1, 73, 99, 20, 73, 1 };

It gets caught when it hits the second 73.

Second, there’s a clear problem here in your code:

 var pivot = array[leftIndex]; 
 while (true)    
 {        
   while (array[leftIndex] < pivot)
   {
            leftIndex++;        
   }

Well, that's never going to be true, is it? if pivot = array[leftIndex], then array[leftIndex] will never be < pivot.
]]>