Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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语言中字符串数组的插入排序#_C#_Algorithm_Sorting_Insertion Sort - Fatal编程技术网

C# C语言中字符串数组的插入排序#

C# C语言中字符串数组的插入排序#,c#,algorithm,sorting,insertion-sort,C#,Algorithm,Sorting,Insertion Sort,如果我有一个字符串数组,比如 string[] names = {"John Doe", "Doe John", "Another Name", "Name Another"}; 如何使用插入排序对该数组进行排序 维基百科有一些例子: 对我来说很好: class Program { static void Main() { string[] names = { "John Doe", "Doe John", "Another Name", "Name Anoth

如果我有一个字符串数组,比如

string[] names = {"John Doe", "Doe John", "Another Name", "Name Another"};
如何使用插入排序对该数组进行排序

维基百科有一些例子:

对我来说很好:

class Program
{
    static void Main()
    {
        string[] names = { "John Doe", "Doe John", "Another Name", "Name Another" };
        InsertSort(names);
        foreach (var item in names)
        {
            Console.WriteLine(item);
        }
    }

    static void InsertSort(IComparable[] array)
    {
        int i, j;

        for (i = 1; i < array.Length; i++)
        {
            IComparable value = array[i];
            j = i - 1;
            while ((j >= 0) && (array[j].CompareTo(value) > 0))
            {
                array[j + 1] = array[j];
                j--;
            }
            array[j + 1] = value;
        }
    }
}

以下是我的实现:

public static void Swap<T>(ref T a, ref T b)
{
    T t = a;
    a = b;
    b = t;
}

public static void InsertionSort<T>(this T[] a) where T : IComparable<T>
{
    a.InsertionSort(Comparer<T>.Default.Compare);
}

public static void InsertionSort<T>(this T[] a, Comparison<T> c)
{
    int n = a.Length;
    for (int i = 1; i < n; ++i)
        for (int k = i; k > 0 && c(a[k], a[k - 1]) < 0; --k)
            Swap(ref a[k], ref a[k - 1]);
}
公共静态无效交换(参考T a,参考T b)
{
T=a;
a=b;
b=t;
}
公共静态void InsertionSort(此T[]a),其中T:IComparable
{
a、 InsertionSort(Comparer.Default.Compare);
}
公共静态void InsertionSort(此T[]a,比较c)
{
int n=a.长度;
对于(int i=1;i0&&c(a[k],a[k-1])<0;--k)
互换(参考a[k],参考a[k-1]);
}

我上了一些课来做到这一点:

public static class InsertionSort<T> where T : System.IComparable<T>
{
    public static void Sort(ref T[] array)
    {
        T[] tmp = new T[array.Length];
        tmp[0] = array[0];

        for (int i = 1; i < array.Length; i++)
        {
            int place = FindProperPlace(tmp, array[i]);
            ExpandArray(ref tmp, place);
            tmp[place] = array[i];
        }

        array = tmp;
    }

    private static int FindProperPlace(T[] numbersArray, T number)
    {
        int j;

        for (j = 0; j < numbersArray.Length; j++)
        {
            if (number.CompareTo(numbersArray[j]) < 0)
            {
                break;
            }
        }

        return j;
    }

    private static void ExpandArray(ref T[] tmp, int place)
    {
        for (int i = tmp.Length - 1; i > place; i--)
        {
            tmp[i] = tmp[i - 1];
        }
    }
}
公共静态类插入排序,其中T:System.IComparable
{
公共静态无效排序(参考T[]数组)
{
T[]tmp=新的T[array.Length];
tmp[0]=数组[0];
for(int i=1;iplace;i--)
{
tmp[i]=tmp[i-1];
}
}
}

@Larsenal,不,我只是用一个简单的例子。我打赌你的
InsertSort(名称)周围的客户端代码中有一个打字错误/错误呼叫。在检查排序方法是否成功时,可能只是引用了错误的数组。请发布您的客户端代码,而不是已经众所周知并经过测试的InsertSort方法。我实际上是在从文件中读取字符串,我将数组设置为[16],但行数实际上不是16,因此我需要找到一种方法来拥有可变大小的数组。如果您的要求是可变大小的数组,你也可以在单子上叮当作响
class Program
{
    static void Main()
    {
        string[] names = { "John Doe", "Doe John", "Another Name", "Name Another" };
        InsertSort(names);
        foreach (var item in names)
        {
            Console.WriteLine(item);
        }
    }

    static void InsertSort(IComparable[] array)
    {
        int i, j;

        for (i = 1; i < array.Length; i++)
        {
            IComparable value = array[i];
            j = i - 1;
            while ((j >= 0) && (array[j].CompareTo(value) > 0))
            {
                array[j + 1] = array[j];
                j--;
            }
            array[j + 1] = value;
        }
    }
}
Another Name
Doe John
John Doe
Name Another
public static void Swap<T>(ref T a, ref T b)
{
    T t = a;
    a = b;
    b = t;
}

public static void InsertionSort<T>(this T[] a) where T : IComparable<T>
{
    a.InsertionSort(Comparer<T>.Default.Compare);
}

public static void InsertionSort<T>(this T[] a, Comparison<T> c)
{
    int n = a.Length;
    for (int i = 1; i < n; ++i)
        for (int k = i; k > 0 && c(a[k], a[k - 1]) < 0; --k)
            Swap(ref a[k], ref a[k - 1]);
}
public static class InsertionSort<T> where T : System.IComparable<T>
{
    public static void Sort(ref T[] array)
    {
        T[] tmp = new T[array.Length];
        tmp[0] = array[0];

        for (int i = 1; i < array.Length; i++)
        {
            int place = FindProperPlace(tmp, array[i]);
            ExpandArray(ref tmp, place);
            tmp[place] = array[i];
        }

        array = tmp;
    }

    private static int FindProperPlace(T[] numbersArray, T number)
    {
        int j;

        for (j = 0; j < numbersArray.Length; j++)
        {
            if (number.CompareTo(numbersArray[j]) < 0)
            {
                break;
            }
        }

        return j;
    }

    private static void ExpandArray(ref T[] tmp, int place)
    {
        for (int i = tmp.Length - 1; i > place; i--)
        {
            tmp[i] = tmp[i - 1];
        }
    }
}