您的位置:首页 > 编程语言 > C#

菜鸟学排序算法

2015-06-09 18:50 387 查看
插入排序:

public List<int> InsertSort(List<int> arr)
{
List<int> ARRAY = new List<int>();
ARRAY.Add(arr[0]);
int value;
int count;

for (int arrIndex = 1; arrIndex < arr.Count; arrIndex++)
{
value = arr[arrIndex];
count = ARRAY.Count;

for (int ArrayIndex = 0; ArrayIndex < count; ArrayIndex++)
{
if (value <= ARRAY[ArrayIndex])
{
ARRAY.Insert(ArrayIndex, value);
break;
}
}
if (count >= ARRAY.Count)
ARRAY.Add(value);
}
return ARRAY;
}


希尔排序:

private List<int> ShellSort(List<int> arr)
{
List<int> ARRAY = new List<int>();
int delta = arr.Count;
ARRAY = arr;
do
{
delta = (delta / 3) + 1;
for (int i = 0; i < delta; i++)
{
List<int> list = new List<int>();
for (int j = 0; j < arr.Count / delta; j++)
{
list.Add(arr[j * delta + i]);
}
list = InsertSort(list);
for (int j = 0; j < arr.Count / delta; j++)
{
arr[j * delta + i] = list[j];
}
}
}
while (delta > 1);

return ARRAY;
}


快速排序:

private List<int> QuickSort(List<int> arr, int first, int last)
{
if (first >= last)
return null;

int First = first;
int Last = last;
int key = array[first];

while (first < last)
{
while (array[last] >= key && last > first)
--last;

array[first] = array[last];
while (array[first] <= key && last > first)
++first;

array[last] = array[first];
}
array[first] = key;

QuickSort(arr, First, first - 1);
QuickSort(arr, last + 1, Last);
return arr;
}


归并排序:

private List<int> MergeSort(List<int> arr)
{
List<int> ARRAY = new List<int>();
int delta = 1;
int i, j;

while (delta < arr.Count)
{
ARRAY.Clear();
while (arr.Count > 0)
{
i = 0; j = i + delta;

while (i < delta && j < 2 * delta && j < arr.Count)
{
if (arr[i] < arr[j])
{
ARRAY.Add(arr[i]);
i++;
}
else
{
ARRAY.Add(arr[j]);
j++;
}
}
while (i < delta && i < arr.Count)
{
ARRAY.Add(arr[i]);
i++;
}
while (j < 2 * delta && j < arr.Count)
{
ARRAY.Add(arr[j]);
j++;
}

arr.RemoveRange(0, Math.Min(j, arr.Count));
}
delta *= 2;
arr = new List<int>(ARRAY);
}

return ARRAY;
}


基数排序:

private List<int> RadixSort(List<int> arr)
{
List<int> ARRAY = new List<int>();
List<int>[] ARRAYS = new List<int>[10];
int count = 0;int index = 0;
int Max = arr.Max().ToString().Length;

while (count < Max)
{
ARRAY.Clear();
for (int i = 0; i < ARRAYS.Length; i++)
ARRAYS[i] = new List<int>();
foreach (int Item in arr)
{
index = (Item / (int)Math.Pow(10, count)) % 10;
{
ARRAYS[index].Add(Item);
}
}

for (int i = 0; i < 10; i++)
{
ARRAY.AddRange(ARRAYS[i]);
}

arr = new List<int>(ARRAY);
count++;
}

return ARRAY;
}


二叉树定义:

class Tree
{
public int value;
public Tree left = null;
public Tree right = null;

public Tree(int key)
{
value = key;
}
}


二叉排序树:

public List<int> BinaryTreeSort(List<int> arr)
{
Tree tree = null;
AddListToBinaryTree(ref tree, arr);
arr.Cle
aa24
ar();
arr = LoadListFromTree(tree, ref arr);

return arr;
}

private void AddListToBinaryTree(ref Tree tree, List<int> list)
{
foreach (int key in list)
{
AddKeyToBinaryTree(ref tree, key);
}
}

private void AddKeyToBinaryTree(ref Tree tree, int key)
{
if (tree == null)
{
tree = new Tree(key);
}
else if (  key < tree.value)
{
AddKeyToBinaryTree(ref tree.left, key);
}
else if (tree.value < key)
{
AddKeyToBinaryTree(ref tree.right, key);
}
}

private List<int> LoadListFromTree(Tree tree, ref List<int> list)
{
if (tree == null)
return null;

LeftLoadListFromTree(tree.left, ref list);
list.Add(tree.value);
LeftLoadListFromTree(tree.right, ref list);

return list;
}


堆排序(求最大值):

private int HeapSort(List<int> arr)
{
Tree tree = null;
AddListToHeapSort(ref tree, arr);
return tree.value;
}
private void AddListToHeapSort(ref Tree tree, List<int> list)
{
foreach (int key in list)
{
AddKeyToHeapSort(ref tree, key);
}
}
private void AddKeyToHeapSort(ref Tree tree, int key)
{
if (tree == null)
{
tree = new Tree(key);
}
else if (tree.value >= key)
{
if (tree.left == null)
AddKeyToHeapSort(ref tree.left, key);
else if (tree.right == null)
AddKeyToHeapSort(ref tree.right, key);
else
{
if (tree.left.value > tree.right.value)
{
AddKeyToHeapSort(ref tree.left, key);
}
else
AddKeyToHeapSort(ref tree.right, key);
}
}
else
{
AddKeyToHeapSort(ref tree, tree.value);
tree.value = key;
}
}


写的很烂。。。

权当每天的日记吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#