我不仅是编程新手,而且排序算法也是如此。我在一个算法和设计分析课上,所以我更好地理解了这些概念……直到这个问题。
我的插入排序不起作用。因为这是通用的,所以我必须做很多比较它看起来(或者也许这就是我编写代码的方式)。因此,它在 temp = list[i] 以及我的 while 循环中给出了这个错误“运算符 > 不能应用于 T 和 int 类型的操作数,并且不能将 T 类型隐式转换为 int。有人可以向我解释为什么我用的compareto没有解决这个问题。另外,请详细解释一下,以便我学习。我需要比较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++;
}
}
}