比较到并插入排序

本文关键字:插入排序 比较 | 更新日期: 2023-09-27 18:33:02

我不仅对编程不熟悉,而且对算法也很陌生。我在算法和设计分析课上,所以我更好地理解了这些概念,因此......直到这个问题。

我的插入排序不起作用。因为这是通用的,所以我必须做很多比较(或者也许这就是我编写代码的方式)。因此,它在 temp = list[i] 以及我的 while 循环中给了我这个错误"运算符> 不能应用于 T 和 int 类型的操作数,并且不能隐式将 T 类型转换为 int。有人可以向我解释为什么我使用的比较器没有解决这个问题。另外,请详细解释,以便我学习。我需要比较温度和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++;
    }
}

}

比较到并插入排序

list的类型T[]意思是list[i]将返回一个T,但temp是一个int。尝试将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允许方法修改参数的值并将修改后的值返回给调用方 - 这几乎从来都不是必需的。有关详细信息,请参阅 C# 参考