compareTo并插入排序

时间:2013-09-27 22:32:24

标签: c#

我不仅是编程新手,还是排序算法。我在一个算法和设计分析课,所以我更好地理解这些概念......直到这个问题。

我的插入排序不起作用。因为这是通用的,我必须做很多比较(或者这可能只是我编写代码的方式)。因此,它在temp = list [i]和我的while循环中给出了这个错误“运算符>不能应用于T和int类型的操作数,也不能将类型T隐式转换为int。有人可以向我解释为什么我使用的比较没有解决这个问题。另外,请详细解释,以便我可以学习它。我需要比较temp和j吗?我无法弄明白。

using System;

namespace ArrayListNamespace
{
public abstract class ArrayList<T> where T : IComparable
{
    protected T[] list;
    protected int length;

    public ArrayList()
    {
        list = new T[100];
        length = 0;
    }

    public abstract void insert(ref T item);

    public int remove(ref T item)
    {
        if (length == 0) return 0;
        else
        {
            //find value, if it exists
            for (int i = 0; i < length; i++)
            {
                if (item.Equals(list[i]))
                {
                    list[i] = list[length - 1];
                    length--;
                    return 1;
                }
            }
            return -1;
        }
    }
    public void print()
    {
        for (int i = 0; i < length; i++)
        {
            Console.WriteLine(list[i]);
        }
    }
    public void removeAll(ref T item)
    {
        for (; ; )
        {
            int r = remove(ref item);
            if (r == -1) break;
        }
    }

    public T min(ref T item)
    {
        T tempItem = list[0];

        for (int i = 0; i < length; i++)
        {
            if (list[i].CompareTo(tempItem) < 0)
            {
                tempItem = list[i];
            }
        }
        return tempItem;
    }

    public T max(ref T item)
    {
        T tempItem = list[0];

        for (int i = 0; i > length; i++)
        {
            if (list[i].CompareTo(tempItem) < 0)
            {
                tempItem = list[i];
            }
        }
        return tempItem;
    }

    public void insertSort()
    {
        int temp, j;

        for (int i = 1; i < length; i++)
        {
           if (list[i].CompareTo(temp) < 0)
            {
                temp = list[i];
                j = i - 1;
                while (j >= 0 && list[j] > temp)
                {
                    list[j + 1] = list[j];
                    j--;
                }
                list[j + 1] = temp;
            }
        }
    }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnorderedArrayListNamespace;

namespace test
{
class Program
{
    static void Main(string[] args)
    {
        UnorderedArrayList<int> u = new UnorderedArrayList<int>();
        Console.WriteLine("This is the list before removal...");
        u.print();
        int var = 5;
        u.insert(ref var);
        var = 12;
        u.insert(ref var);
        var = 2;
        u.insert(ref var);
        var = 5;
        u.insert(ref var);
        var = 29;
        u.insert(ref var);
        var = 33;
        u.insert(ref var);
        var = 31;
        u.insert(ref var);
        var = 7;
        u.insert(ref var);
        var = 13;
        u.insert(ref var);


        u.print();
        Console.WriteLine();
        Console.WriteLine("This is the list after removal...");
        var = 5;
        u.removeAll(ref var);
        u.print();

        Console.WriteLine("\nThe min value for integers is " + u.min(ref var));
        Console.WriteLine("The max value for integers is " + u.max(ref var));

        UnorderedArrayList<string> p = new UnorderedArrayList<string>();
        Console.WriteLine("\nThis is the list before removal..."); ;
        p.print();
        string cow = "cow";
        p.insert(ref cow);
        cow = "dog";
        p.insert(ref cow);
        cow = "cat";
        p.insert(ref cow);
        cow = "wolf";
        p.insert(ref cow);
        cow = "dog";
        p.insert(ref cow);
        cow = "whale";
        p.insert(ref cow);
        cow = "buffalo";
        p.insert(ref cow);
        cow = "monkey";
        p.insert(ref cow);
        cow = "walrus";
        p.insert(ref cow);
        p.print();
        Console.WriteLine();
        Console.WriteLine("This is the list after removal...");
        cow = "cow";
        p.removeAll(ref cow);
        p.print();

        Console.WriteLine("\nThe min value for strings is..." + p.min(ref cow));
        Console.WriteLine("The max value for strings is..." + p.max(ref cow));

        UnorderedArrayList<double> q = new UnorderedArrayList<double>();
        Console.WriteLine("\nThis is the list before removal...");
        q.print();
        double dub = 5.2;
        q.insert(ref dub);
        q.insert(ref dub);
        dub = 12.54;
        q.insert(ref dub);
        dub = 2.14;
        q.insert(ref dub);
        dub = 29.13;
        q.insert(ref dub);
        dub = 3.56;
        q.insert(ref dub);
        dub = 32.14;
        q.insert(ref dub);
        dub = 43.23;
        q.insert(ref dub);
        dub = 2.33;
        q.insert(ref dub);
        dub = 4.77;
        q.insert(ref dub);
        dub = 15.46;
        q.insert(ref dub);
        q.print();
        Console.WriteLine();
        Console.WriteLine("This is the list after removal...");
        dub = 5.2;
        q.removeAll(ref dub);
        q.print();

        Console.WriteLine("\nThe min value for double is " + q.min(ref dub));
        Console.WriteLine("The max value for double is " + q.max(ref dub));
        Console.WriteLine();
    }
}
}
using System;

namespace ArrayListADTNamespace
{
public interface ArrayListADT<T>
{
    // insert() method places one item in the list
    void insert(ref T item);
    // remove() method removes first instance of item in list
    int remove(ref T item);
    // print() method prints all items in list
    void print();
    // removal all method
    void removeAll(ref T item);
    // min method
    T min(ref T item);
    // max method
    T max(ref T item);
    void insertSort();
  }
  }
using System;
using ArrayListNamespace;
using ArrayListADTNamespace;

namespace UnorderedArrayListNamespace
{
public class UnorderedArrayList<T> : ArrayList<T>, ArrayListADT<T> where T: IComparable
{
    public UnorderedArrayList()
    {
    }

    public override void insert(ref T item)
    {
        list[length] = item;
        length++;
    }
}

}

1 个答案:

答案 0 :(得分:2)

list类型为T[],意味着list[i]将返回T,但tempint。尝试将temp变量更改为T并使用CompareTo代替list[j] > T

public void insertSort()
{
    T temp = list[0];
    int j;

    for (int i = 1; i < length; i++)
    {
       if (list[i].CompareTo(temp) < 0)
        {
            temp = list[i];
            j = i - 1;
            while (j >= 0 && list[j].CompareTo(temp) > 0)
            {
                list[j + 1] = list[j];
                j--;
            }
            list[j + 1] = temp;
        }
    }
}

此外,您实际上不需要在每个参数上使用ref关键字。 refout允许您的方法修改参数的值并将该修改后的值返回给调用者 - 这几乎是不必要的。有关详细信息,请参阅ref C# Reference