Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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#快速排序字符串数组类_C#_Arrays - Fatal编程技术网

c#快速排序字符串数组类

c#快速排序字符串数组类,c#,arrays,C#,Arrays,如何使函数返回排序数组的结果 class quiksort { public static char[] qsort(char[] items) { return qs(items, 0, items.Length - 1); } // A recursive version of Quicksort for characters. static char[] qs(char[] items, int left, int right)

如何使函数返回排序数组的结果

class quiksort
{
    public static char[] qsort(char[] items)
    {
        return qs(items, 0, items.Length - 1);
    }

    // A recursive version of Quicksort for characters. 
    static char[] qs(char[] items, int left, int right)
    {
        int i, j;
        char x, y;

        i = left; j = right;
        x = items[(left + right) / 2];

        do
        {
            while ((items[i] < x) && (i < right)) i++;
            while ((x < items[j]) && (j > left)) j--;

            if (i <= j)
            {
                y = items[i];
                items[i] = items[j];
                items[j] = y;
                i++; j--;
            }
        } while (i <= j);

        if (left < j)
        {
            return qs(items, left, j);
        }
        if (i < right)
        {
            return qs(items, i, right);
        }
    } 
}
类quiksort { 公共静态字符[]qsort(字符[]项) { 返回qs(项目,0,项目.长度-1); } //字符快速排序的递归版本。 静态字符[]qs(字符[]项,左整数,右整数) { int i,j; 字符x,y; i=左;j=右; x=项目[(左+右)/2]; 做 { 而((项目[i]left))j--;
如果(i问题出在第二个方法中。由于错误说明所有代码路径都必须返回值。您的返回语句位于
if
语句中。即使其中一个语句始终执行,编译器也不关心,就其而言,您的方法可能不会返回任何内容。您需要添加一个返回语句作为函数的最后一行

    static char[] qs(char[] items, int left, int right)
    {
        int i, j;
        char x, y;

        i = left; j = right;
        x = items[(left + right) / 2];

        do
        {
            while ((items[i] < x) && (i < right)) i++;
            while ((x < items[j]) && (j > left)) j--;

            if (i <= j)
            {
                y = items[i];
                items[i] = items[j];
                items[j] = y;
                i++; j--;
            }
        } while (i <= j);

        if (left < j)
        {
            return qs(items, left, j);
        }
        if (i < right)
        {
            return qs(items, i, right);
        }
        return //whatever is most appropriate in the case that you arrive here
    }
static char[]qs(char[]items,int left,int right)
{
int i,j;
字符x,y;
i=左;j=右;
x=项目[(左+右)/2];
做
{
而((项目[i]left))j--;

如果(i问题出在第二个方法中。由于错误说明所有代码路径都必须返回值。您的返回语句位于
if
语句中。即使其中一个语句始终执行,编译器也不关心,就其而言,您的方法可能不会返回任何内容。您需要添加一个返回语句作为函数的最后一行

    static char[] qs(char[] items, int left, int right)
    {
        int i, j;
        char x, y;

        i = left; j = right;
        x = items[(left + right) / 2];

        do
        {
            while ((items[i] < x) && (i < right)) i++;
            while ((x < items[j]) && (j > left)) j--;

            if (i <= j)
            {
                y = items[i];
                items[i] = items[j];
                items[j] = y;
                i++; j--;
            }
        } while (i <= j);

        if (left < j)
        {
            return qs(items, left, j);
        }
        if (i < right)
        {
            return qs(items, i, right);
        }
        return //whatever is most appropriate in the case that you arrive here
    }
static char[]qs(char[]items,int left,int right)
{
int i,j;
字符x,y;
i=左;j=右;
x=项目[(左+右)/2];
做
{
而((项目[i]left))j--;

如果(i问题出在第二个方法中。由于错误说明所有代码路径都必须返回值。您的返回语句位于
if
语句中。即使其中一个语句始终执行,编译器也不关心,就其而言,您的方法可能不会返回任何内容。您需要添加一个返回语句作为函数的最后一行

    static char[] qs(char[] items, int left, int right)
    {
        int i, j;
        char x, y;

        i = left; j = right;
        x = items[(left + right) / 2];

        do
        {
            while ((items[i] < x) && (i < right)) i++;
            while ((x < items[j]) && (j > left)) j--;

            if (i <= j)
            {
                y = items[i];
                items[i] = items[j];
                items[j] = y;
                i++; j--;
            }
        } while (i <= j);

        if (left < j)
        {
            return qs(items, left, j);
        }
        if (i < right)
        {
            return qs(items, i, right);
        }
        return //whatever is most appropriate in the case that you arrive here
    }
static char[]qs(char[]items,int left,int right)
{
int i,j;
字符x,y;
i=左;j=右;
x=项目[(左+右)/2];
做
{
而((项目[i]left))j--;

如果(i问题出在第二个方法中。由于错误说明所有代码路径都必须返回值。您的返回语句位于
if
语句中。即使其中一个语句始终执行,编译器也不关心,就其而言,您的方法可能不会返回任何内容。您需要添加一个返回语句作为函数的最后一行

    static char[] qs(char[] items, int left, int right)
    {
        int i, j;
        char x, y;

        i = left; j = right;
        x = items[(left + right) / 2];

        do
        {
            while ((items[i] < x) && (i < right)) i++;
            while ((x < items[j]) && (j > left)) j--;

            if (i <= j)
            {
                y = items[i];
                items[i] = items[j];
                items[j] = y;
                i++; j--;
            }
        } while (i <= j);

        if (left < j)
        {
            return qs(items, left, j);
        }
        if (i < right)
        {
            return qs(items, i, right);
        }
        return //whatever is most appropriate in the case that you arrive here
    }
static char[]qs(char[]items,int left,int right)
{
int i,j;
字符x,y;
i=左;j=右;
x=项目[(左+右)/2];
做
{
而((项目[i]left))j--;

如果(ievanmcdonnal对错误的回答是正确的

但是,更一般地说,让一个方法接受一个
char[]
参数,更改该
char[]
,然后返回一个
char[]
(相同的
char[]
,但仅从签名看不明显)

如果您要执行这样的就地替换,那么如果您只返回
void
,那么您的代码将更加清晰,这表明您更改了作为参数传递的
char[]


相反,如果要返回一个排序数组,则返回一个新数组,并保持传递的数组不变。

evanmcdonnal的错误答案是正确的

但是,更一般地说,让一个方法接受一个
char[]
参数,更改该
char[]
,然后返回一个
char[]
(相同的
char[]
,但仅从签名看不明显)

如果您要执行这样的就地替换,那么如果您只返回
void
,那么您的代码将更加清晰,这表明您更改了作为参数传递的
char[]


相反,如果要返回一个排序数组,则返回一个新数组,并保持传递的数组不变。

evanmcdonnal的错误答案是正确的

但是,更一般地说,让一个方法接受一个
char[]
参数,更改该
char[]
,然后返回一个
char[]
(相同的
char[]
,但仅从签名看不明显)

如果您要执行这样的就地替换,那么如果您只返回
void
,那么您的代码将更加清晰,这表明您更改了作为参数传递的
char[]


相反,如果要返回一个排序数组,则返回一个新数组,并保持传递的数组不变。

evanmcdonnal的错误答案是正确的

但是,更一般地说,让一个方法接受一个
char[]
参数,更改该
char[]
,然后返回一个
char[]
(相同的
char[]
,但仅从签名看不明显)

如果您要执行这样的就地替换,那么如果您只返回
void
,那么您的代码将更加清晰,这表明您更改了作为参数传递的
char[]

相反,如果要返回一个排序数组,则返回一个新数组,并保留当前数组