Quicksort算法帮助 [英] Quicksort Algorithm help

查看:112
本文介绍了Quicksort算法帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助。我的编程技巧很差,所以我决定用goto递归实现快速排序算法。我的排序代码有效,但循环是无止境的。如何阻止它。每次代码都从quickLeft到quickRight,不要停止。我认为需要从计算var像分区计数。

  static   void  Main( string  [] args)
{
int [] array = { 15 2 1 11 3 5 8 4 };
int pivot,left = 0 ,right = array.Length -1;
pivot = left;

quickRight:
{
while (array [pivot]< array [right])
{
right--;
}
if (array [pivot] > array [right])
{

int variableNum = array [pivot];
array [pivot] = array [right];
array [right] = variableNum;
pivot = right;
goto quickLeft;
}
else
{
if (pivot < array.Length -1)
{
left = pivot + 1 ;
right = array.Length -1;
pivot = right;
goto quickLeft;
}
}
}

quickLeft:
{
while ( array [pivot] > array [left])
{
left ++;
}
if (array [pivot] < array [left])
{
int variableNum = array [pivot];
array [pivot] = array [left];
array [left] = variableNum;
pivot = left;
goto quickRight;
}
else
{
if (pivot > 0
{
right = pivot - 1 ;
pivot = 0 ;
left = 0 ;
goto quickRight;
}
}
}
// Console.WriteLine(左) );
// Console.WriteLine(右);

for int i = 0 ; i < array.Length; i ++)
Console.WriteLine(array [i] + ');

}



< code>

解决方案

请参阅我对该问题的评论。我的想法是说服您丢弃代码并从头开始实施。从哪儿开始?在这里,您将找到所需的一切: http://en.wikipedia.org/wiki/Quicksort [ ^ ]。



-SA

I need your help. My skills are poor in programming so i decide to implement quick sort algorithm with goto recursion. My sort code works but loop is endless. How to stop it. Everytime code go from quickLeft to quickRight and dont stop. I think there is need from counting var like partition count.

static void Main(string[] args)
       {
           int[] array = { 15,2,1,11,3,5,8,4 };
           int pivot, left = 0, right = array.Length -1;
           pivot = left;

       quickRight:
       {
           while(array[pivot]<array[right])
           {
               right--;
           }
           if (array[pivot] > array[right])
           {

               int variableNum = array[pivot];
               array[pivot] = array[right];
               array[right] = variableNum;
               pivot = right;
               goto quickLeft;
           }
           else
           {
              if (pivot < array.Length -1)
              {
                  left = pivot + 1;
                  right = array.Length -1;
                  pivot = right;
             goto quickLeft;
              }
           }
           }

       quickLeft:
       {
           while (array[pivot] > array[left])
           {
               left++;
           }
           if (array[pivot] < array[left])
           {
               int variableNum = array[pivot];
               array[pivot] = array[left];
               array[left] = variableNum;
               pivot = left;
               goto quickRight;
           }
           else
           {
               if (pivot > 0)
               {
                   right = pivot - 1;
                   pivot = 0;
                   left = 0;
               goto quickRight;
               }
           }
       }
           //Console.WriteLine(left);
           //Console.WriteLine(right);

           for (int i = 0; i < array.Length; i++)
               Console.WriteLine(array[i] + "'");

         }


<code>

解决方案

Please see my comments to the question. My idea is to convince you to discard your code and start your implementation from scratch. Where to start? Here you will find all you need: http://en.wikipedia.org/wiki/Quicksort[^].

—SA


这篇关于Quicksort算法帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆