我用C#编写了一个基本的链表类,其中有一个节点对象,代表着链表中的每个节点。
虽然代码没有使用IEnumerable,但我能否实现排序函数? 我使用的编程语言是C#。是否有关于此的C#示例?
我正在参考这个示例:
谢谢!
虽然代码没有使用IEnumerable,但我能否实现排序函数? 我使用的编程语言是C#。是否有关于此的C#示例?
我正在参考这个示例:
谢谢!
以下是使用函数式风格编写的快速排序和归并排序方法:
class List
{
public int item;
public List rest;
public List(int item, List rest)
{
this.item = item;
this.rest = rest;
}
// helper methods for quicksort
public static List Append(List xs, List ys)
{
if (xs == null) return ys;
else return new List(xs.item, Append(xs.rest, ys));
}
public static List Filter(Func<int,bool> p, List xs)
{
if (xs == null) return null;
else if (p(xs.item)) return new List(xs.item, Filter(p, xs.rest));
else return Filter(p, xs.rest);
}
public static List QSort(List xs)
{
if (xs == null) return null;
else
{
int pivot = xs.item;
List less = QSort(Filter(x => x <= pivot, xs.rest));
List more = QSort(Filter(x => x > pivot, xs.rest));
return Append(less, new List(pivot, more));
}
}
// Helper methods for mergesort
public static int Length(List xs)
{
if (xs == null) return 0;
else return 1 + Length(xs.rest);
}
public static List Take(int n, List xs)
{
if (n == 0) return null;
else return new List(xs.item, Take(n - 1, xs.rest));
}
public static List Drop(int n, List xs)
{
if (n == 0) return xs;
else return Drop(n - 1, xs.rest);
}
public static List Merge(List xs, List ys)
{
if (xs == null) return ys;
else if (ys == null) return xs;
else if (xs.item <= ys.item) return new List(xs.item, Merge(xs.rest, ys));
else return new List(ys.item, Merge(xs, ys.rest));
}
public static List MSort(List xs)
{
if (Length(xs) <= 1) return xs;
else
{
int len = Length(xs) / 2;
List left = MSort(Take(len, xs));
List right = MSort(Drop(len, xs));
return Merge(left, right);
}
}
public static string Show(List xs)
{
if(xs == null) return "";
else return xs.item.ToString() + " " + Show(xs.rest);
}
}
额外加分项:使用函数式Pairing堆进行堆排序。
class List
{
// ...
public static Heap List2Heap(List xs)
{
if (xs == null) return null;
else return Heap.Merge(new Heap(null, xs.item, null), List2Heap(xs.rest));
}
public static List HSort(List xs)
{
return Heap.Heap2List(List2Heap(xs));
}
}
class Heap
{
Heap left;
int min;
Heap right;
public Heap(Heap left, int min, Heap right)
{
this.left = left;
this.min = min;
this.right = right;
}
public static Heap Merge(Heap a, Heap b)
{
if (a == null) return b;
if (b == null) return a;
Heap smaller = a.min <= b.min ? a : b;
Heap larger = a.min <= b.min ? b : a;
return new Heap(smaller.left, smaller.min, Merge(smaller.right, larger));
}
public static Heap DeleteMin(Heap a)
{
return Merge(a.left, a.right);
}
public static List Heap2List(Heap a)
{
if (a == null) return null;
else return new List(a.min, Heap2List(DeleteMin(a)));
}
}
为了实际使用,您需要重写辅助方法而不使用递归,并且可能使用内置的可变列表。
使用方法:
List xs = new List(4, new List(2, new List(3, new List(1, null))));
Console.WriteLine(List.Show(List.QSort(xs)));
Console.WriteLine(List.Show(List.MSort(xs)));
Console.WriteLine(List.Show(List.HSort(xs)));
请求提供一个原地版本。这里是一个非常快速的实现。我一字不漏地编写了这个代码,没有寻找改进代码的机会,也就是说每一行都是我首先想到的。因为我使用 null 作为空列表,所以它非常丑陋 :) 缩进不一致等等。
此外,我仅在一个示例上测试了这个代码:
MList ys = new MList(4, new MList(2, new MList(3, new MList(1, null))));
MList.QSortInPlace(ref ys);
Console.WriteLine(MList.Show(ys));
神奇的是它第一次就可行了!但我相当确定这段代码包含了一些bug,请不要追究我。
class MList
{
public int item;
public MList rest;
public MList(int item, MList rest)
{
this.item = item;
this.rest = rest;
}
public static void QSortInPlace(ref MList xs)
{
if (xs == null) return;
int pivot = xs.item;
MList pivotNode = xs;
xs = xs.rest;
pivotNode.rest = null;
// partition the list into two parts
MList smaller = null; // items smaller than pivot
MList larger = null; // items larger than pivot
while (xs != null)
{
var rest = xs.rest;
if (xs.item < pivot) {
xs.rest = smaller;
smaller = xs;
} else {
xs.rest = larger;
larger = xs;
}
xs = rest;
}
// sort the smaller and larger lists
QSortInPlace(ref smaller);
QSortInPlace(ref larger);
// append smaller + [pivot] + larger
AppendInPlace(ref pivotNode, larger);
AppendInPlace(ref smaller, pivotNode);
xs = smaller;
}
public static void AppendInPlace(ref MList xs, MList ys)
{
if(xs == null){
xs = ys;
return;
}
// find the last node in xs
MList last = xs;
while (last.rest != null)
{
last = last.rest;
}
last.rest = ys;
}
public static string Show(MList xs)
{
if (xs == null) return "";
else return xs.item.ToString() + " " + Show(xs.rest);
}
}
List<T>
或者通过LINQ的 IEnumerable<T>
),并重新用排序后的数据重建链表。Comparer<T>.Default
很有用(假设你使用泛型)。这应该允许你比较任何实现了 IComparable<T>
或者 IComparable
接口的项目。LinkedList<T>
等;如果这只是为了学习等目的,那就没问题了 ;-p
有些人(包括我)可能想对来自.net库的LinkedList<T>
进行排序。
简单的方法是使用Linq排序,最后创建一个新的链表。但这会产生垃圾。对于小集合来说,这不是问题,但对于大集合来说,效率可能不高。
对于想要一定程度优化的人,这里提供了一个通用的在.net双向链表中实现的原地快速排序。
此实现不进行分裂/合并,而是检查每个递归的边界节点。
/// <summary>
/// in place linked list sort using quick sort.
/// </summary>
public static void QuickSort<T>(this LinkedList<T> linkedList, IComparer<T> comparer)
{
if (linkedList == null || linkedList.Count <= 1) return; // there is nothing to sort
SortImpl(linkedList.First, linkedList.Last, comparer);
}
private static void SortImpl<T>(LinkedListNode<T> head, LinkedListNode<T> tail, IComparer<T> comparer)
{
if (head == tail) return; // there is nothing to sort
void Swap(LinkedListNode<T> a, LinkedListNode<T> b)
{
var tmp = a.Value;
a.Value = b.Value;
b.Value = tmp;
}
var pivot = tail;
var node = head;
while (node.Next != pivot)
{
if (comparer.Compare(node.Value, pivot.Value) > 0)
{
Swap(pivot, pivot.Previous);
Swap(node, pivot);
pivot = pivot.Previous; // move pivot backward
}
else node = node.Next; // move node forward
}
if (comparer.Compare(node.Value, pivot.Value) > 0)
{
Swap(node, pivot);
pivot = node;
}
// pivot location is found, now sort nodes below and above pivot.
// if head or tail is equal to pivot we reached boundaries and we should stop recursion.
if (head != pivot) SortImpl(head, pivot.Previous, comparer);
if (tail != pivot) SortImpl(pivot.Next, tail, comparer);
}
for(int i=0; i<counter;i++)
{
while(current.next!=null)
{
if(current.elemen>current.next.element)
{
Swap(current.elment,current.next.element);
}
current=current.next;
}
}
当您向链表添加或插入元素时,增加计数器
这可能不是最佳解决方案,但它是我能想到的最简单的。如果有人能想到更简单但仍然快速的解决方案,我很乐意听听。
抱歉,它是C++,但应该可以翻译。
List * SortList(List * p_list)
{
if(p_list == NULL || p_list->next == NULL)
return p_list;
List left, right;
List * piviot = p_list;
List * piviotEnd = piviot;
List * next = p_list->next;
do
{
p_list = next;
next = next->next;
//FIGURE OUT WHICH SIDE I'M ADDING TO.
if(p_list->data > piviot->data )
right.InsertNode(p_list);
else if(p_list->data < piviot->data)
left.InsertNode(p_list);
else
{ //we put this in it's own list because it doesn't need to be sorted
piviotEnd->next = p_list;
piviotEnd= p_list;
}
}while(next);
//now left contains values < piviot and more contains all the values more
left.next = SortList(left.next);
right.next = SortList(right.next);
//add the piviot to the list then add the rigth list to the full list
left.GetTail()->next = piviot;
piviotEnd->next = right.next;
return left.next;
}
public LinkedListNode<int> Sort2(LinkedListNode<int> head, int count)
{
var cur = head;
var prev = cur;
var min = cur;
var minprev = min;
LinkedListNode<int> newHead = null;
LinkedListNode<int> newTail = newHead;
for (int i = 0; i < count; i++)
{
cur = head;
min = cur;
minprev = min;
while (cur != null)
{
if (cur.Value < min.Value)
{
min = cur;
minprev = prev;
}
prev = cur;
cur = cur.Next;
}
if (min == head)
head = head.Next;
else if (min.Next == null)
minprev.Next = null;
else
minprev.Next = minprev.Next.Next;
if (newHead != null)
{
newTail.Next = min;
newTail = newTail.Next;
}
else
{
newHead = min;
newTail = newHead;
}
}
return newHead;
}
如果你想真正利用链表的优势,而不是绕过它,我建议使用插入排序。
通常情况下,插入排序效率不高 - 最坏情况下为O(n^2)
,但使用链表可以将其提高到O(n log n)
伪代码:
for i in range (1,n):
item = arr[i]
location = binary_search(l=root, r=i, value=item.val) // O(log i)
insert_item_before(arr[location], item) // O(1)
这是一个基于插入排序的实现。它应该足够快,适用于较小的链表和/或列表几乎已经排序完毕的情况下。对于大型列表,请查看M.kazem Akhgary的答案(使用快速排序)。
static void Sort<T>(this LinkedList<T> list, IComparer<T> comparer)
{
var node = list.First;
while (node != null)
{
var next = node.Next;
var min = node;
for (var comp = node.Previous;
comp != null && comparer.Compare(node.Value, comp.Value) < 0;
comp = comp.Previous)
{
min = comp;
}
if (node != min)
{
list.Remove(node);
list.AddBefore(min, node);
}
node = next;
}
}