2.顺序表实现与各种排序算法
namespace DSList
{
public partial class SeqList<T> : IListDS<T> where T:IComparable<T>
{
//fields
private T[] data;
private int last;
private int maxsize;
//properties
public T this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
public int Last
{
get
{
return last;
}
}
public int Maxsize
{
get
{
return maxsize;
}
set
{
maxsize = value;
}
}
//Constructor
public SeqList()
: this(100)
{ }
public SeqList(int size)
{
data = new T[size] ;
last = -1;
maxsize = size;
}
// base Methods
public int GetLength()
{
return last + 1;
}
public bool IsEmpty()
{
if (last == -1)
{
return true;
}
else
{
return false;
}
}
public bool IsFull()
{
if (Maxsize == last + 1)
{
return true;
}
else
{
return false;
}
}
public void Clear()
{
last = -1;
}
public void Append(T item)
{
if (IsFull())
{
Console.WriteLine("List is full!");
return;
}
data[++last] = item;
}
public void Insert(int pos, T item)
{
if (IsFull())
{
Console.WriteLine("List is full!");
return;
}
if ( pos < 1 || pos > last + 2)
{
Console.WriteLine("Position is error!");
return;
}
if (pos == last + 2)
{
data[last+1] = item;
return;
}
else
{
for (int index = last; index >= pos-1; --index)
{
data[index + 1] = data[index];
}
data[pos-1] = item;
}
++last;
return;
}
public T Remove(int pos)
{
T temp = default(T);
if (IsEmpty() || pos < 1 || pos > last + 1)
{
Console.WriteLine("List is empty or Position is error!");
return temp;
}
if (pos == last + 1)
{
temp = data[last];
return temp;
}
else
{
temp = data[pos - 1];
for (int index = pos - 1; index < last; ++index)
{
data[index] = data[index + 1];
}
}
--last;
return temp;
}
public void Delete(T item)
{
int pos = this.Locate(item);
this.Remove(pos);
}
public T GetElem(int pos)
{
if (IsEmpty() || pos < 1 || pos > last + 1)
{
Console.WriteLine("List is empty or Position is error!");
return default(T);
}
return data[pos - 1];
}
public int Locate(T item)
{
if (IsEmpty())
{
Console.WriteLine("List is empty!");
return 0;
}
int index = 0;
for (index = 0; index <= last; ++index)
{
if (data[index].Equals(item))
{
break;
}
}
if (index > last)
{
return -1;
}
return ++index;
}
}
public partial class SeqList<T> : IListDS<T> where T : IComparable<T>
{
//Other methods
//反转顺序表
public void Reverse()
{
T temp = default(T);
int len = this.GetLength() - 1;
for (int index = 0; index <= len / 2; ++index)
{
temp = data[len - index];
data[len - index] = data[index];
data[index] = temp;
}
}
//合并两个已升序排列的顺序表,合并后也升序排列
public void Merge(SeqList<T> la, SeqList<T> lb)
{
this.Maxsize = la.Maxsize + lb.Maxsize;
int i = 0;
int j = 0;
while (i < la.GetLength() && j<lb.GetLength())
{
if (la[i].CompareTo(lb[j]) < 0)
{
this.Append(la[i++]);
}
else
{
this.Append(lb[j++]);
}
}
while (i < la.GetLength())
{
this.Append(la[i++]);
}
while (j < lb.GetLength())
{
this.Append(lb[j++]);
}
}
//删除顺序表中相同的元素
public void Purge(SeqList<T> la)
{
this.Append(la[0]);
for (int i = 1; i <la.GetLength(); ++i)
{
int j = 0;
for (j = 0; j < this.GetLength(); ++j)
{
if (la[i].Equals(data[j]))
{
break;
}
}
if (j> this.GetLength()-1)
{
this.Append(la[i]);
}
}
}
//直接插入排序
public void InsertSort()
{
for (int i = 1; i <= Last; ++i)
{
if (data[i].CompareTo( data[i - 1])<0)
{
T tmp = data[i];
int j = 0;
for (j = i - 1; j >= 0 && tmp.CompareTo(data[j])<0; --j)
{
data[j + 1] = data[j];
}
data[j + 1] = tmp;
}
}
}
//冒泡排序
public void BubbleSort()
{
T tmp = default(T);
for (int i = 0; i <= Last; ++i)
{
for (int j = Last - 1; j >= i; --j)
{
if (data[j + 1].CompareTo(data[j])<0)
{
tmp = data[j + 1];
data[j + 1] = data[j];
data[j] = tmp;
}
}
}
}
//简单选择排序
public void SimpleSelectSort()
{
T tmp = default(T);
int t = 0;
for (int i = 0; i <= Last; ++i)
{
t = i;
for (int j = i + 1; j <= Last; ++j)
{
if (data[t].CompareTo(data[j])>0)
{
t = j;
}
}
tmp = data[i];
data[i] = data[t];
data[t] = tmp;
}
}
//快速排序
public void QuickSort()
{
quickSort(0, Last);
}
private void quickSort(int low, int high)
{
if (low < high)
{
int tmp = Partition(low, high);
quickSort(low, tmp - 1);
quickSort(tmp + 1, high);
}
}
private int Partition(int low, int high)
{
T pivot = data[low];
while (low < high)
{
while (low < high && data[high].CompareTo(pivot) >= 0)
{
high--;
}
if (low != high)
{
data[low] = data[high];
low++;
}
while (low < high && data[low].CompareTo(pivot) <= 0)
{
low++;
}
if (low != high)
{
data[high] = data[low];
high--;
}
}
data[low] = pivot;
return low;
}
//堆排序
public void HeapSort()
{
T tmp = default(T);
CreateHeap(0, Last);
for (int i = Last; i > 0; --i)
{
tmp = data[0];
data[0] = data[i];
data[i] = tmp;
CreateHeap(0, i - 1);
}
}
private void CreateHeap(int low, int high)
{
if ((low < high) && (high <= Last))
{
int j = 0;
T tmp = default(T);
int k = 0;
for (int i = high / 2; i >= low; --i)
{
k = i;
j = 2 * k + 1;
tmp = data[i];
while (j <= high)
{
if (j < high && j + 1 <= high && data[j].CompareTo(data[j + 1]) < 0)
{
++j;
}
if (tmp.CompareTo(data[j]) < 0)
{
data[k] = data[j];
k = j;
j = 2 * k + 1;
}
else
{
j = high + 1;
}
}
data[k] = tmp;
}
}
}
//二路归并排序算法
public void MergeSort()
{
int k = 1; //归并增量
while (k < GetLength())
{
Merge(k);
k *= 2;
}
}
private void Merge(int len)
{
int m = 0; //临时顺序表的起始位置
int l1 = 0; //第1个有序表的起始位置
int h1; //第1个有序表的结束位置
int l2; //第2个有序表的起始位置
int h2; //第2个有序表的结束位置
int i = 0;
int j = 0;
//临时表,用于临时将两个有序表合并为一个有序表
SeqList<T> tmp = new SeqList<T>(GetLength());
//归并处理
while (l1 + len < GetLength())
{
l2 = l1 + len; //第2个有序表的起始位置
h1 = l2 - 1; //第1个有序表的结束位置
//第2个有序表的结束位置
h2 = (l2 + len - 1 < GetLength()) ? l2 + len - 1 : Last;
j = l2;
i = l1;
//两个有序表中的记录没有排序完
while ((i <= h1) && (j <= h2))
{
//第1个有序表记录的关键码小于第2个有序表记录的关键码
if (data[i].CompareTo(data[j]) <= 0)
{
tmp[m++] = data[i++];
}
//第2个有序表记录的关键码小于第1个有序表记录的关键码
else
{
tmp[m++] = data[j++];
}
}
//第1个有序表中还有记录没有排序完
while (i <= h1)
{
tmp[m++] = data[i++];
}
//第2个有序表中还有记录没有排序完
while (j <= h2)
{
tmp[m++] = data[j++];
}
l1 = h2 + 1;
}
i = l1;
//原顺序表中还有记录没有排序完
while (i < GetLength())
{
tmp[m++] = data[i++];
}
//临时顺序表中的记录复制到原顺序表,使原顺序表中的记录有序
for (i = 0; i < GetLength(); ++i)
{
data[i] = tmp[i];
}
}
//希尔排序
public void ShellSort()
{
int i, j, k, h, hCnt;
int[] increments = new int[20];
T tmp;
for (h = 1, i = 0; h < GetLength(); i++)
{
increments[i] = h;
h = 3 * h + 1;
}
for (i--; i >= 0; i--)
{
h = increments[i];
for (hCnt = h; hCnt < 2 * h; hCnt++)
{
for (j = hCnt; j < GetLength(); )
{
tmp = data[j];
k = j;
while (k - h >= 0 && tmp.CompareTo(data[k - h]) < 0)
{
data[k] = data[k - h];
k -= h;
}
data[k] = tmp;
j += h;
}
}
}
}
//基数排序
public void RadixSort(SeqList<int> data)
{
int radix = 10;
int digits = 10;
int d=0, j=0, k=0, factor=0;
CSeqQueue<int>[] sq = new CSeqQueue<int>[radix];
for (d = 0; d < radix; d++)
{
sq[d] = new CSeqQueue<int>();
}
for (d = 1, factor = 1; d <= digits; factor *= radix, d++)
{
for (j = 0; j < GetLength(); j++)
{
sq[(data[j] / factor) % radix].In(data[j]);
}
for (j = k = 0; j < radix; j++)
{
while (!sq[j].IsEmpty())
{
data[k++] = sq[j].Out();
}
}
}
}
//位基数排序
public void BitRadixSort(SeqList<int> data)
{
int d, j, k, mask = 1;
CSeqQueue<int>[] sq = new CSeqQueue<int>[2];
sq[0] = new CSeqQueue<int>();
sq[1] = new CSeqQueue<int>();
for (d = 1; d <= data.GetLength(); d++)
{
for (j = 0; j < data.GetLength(); j++)
{
sq[(data[j] & mask) == 0 ? 0 : 1].In(data[j]);
}
mask <<= 1;
k = 0;
while (!sq[0].IsEmpty())
{
data[k++] = sq[0].Out();
}
while (!sq[1].IsEmpty())
{
data[k++] = sq[1].Out();
}
k = 0;
}
}
public void Output()
{
foreach(T i in data)
{
Console.Write("{0} " , i);
}
Console.WriteLine();
}
}
}