SortFunctions 排序算法
2008-11-20 22:44 宝宝合凤凰 阅读(358) 评论(0) 收藏 举报SortFunctions 排序算法
我总结了5种排序算法,分别是冒泡排序法(bubbleSort),插入排序法(insertSort),选择排序法(selectionSort),归并排序法(mergeSort)和快速排序法(quickSort),并用Actionscrīpt写出来(用AS写的很少见)
就速度来说,quickSort最快,其次是mergeSort,然后是insertSort,selection和SortbubbleSort最慢.不过,无论如何也超不过Array.sort(),毕竟那是flash的内部函数.
Flash: http://cyjb.6ftp.com/download/SortFunctions.swf 全屏浏览
源文件
/*
* SortFunctions 排序算法
* Version 1.0
* 2006.8.17
* CYJB整理
*/
var AP = Array.prototype;
/*
1.冒泡排序法
从数组的后面位置开始,如果发现有比前面一个位置处的数更小的元素,则交换这两个数的位置,形成一个类似轻的
气泡在水中上升的排序过程.
*/
AP.bubbleSort = function() {
var l = this.length;
var i = l;
while (i-->1) {
var j = -1;
while (j++<i-1) {
if (this[j]>this[j+1]) {
this.arrSwap(j, j+1);
}
}
}
};
/*
2.插入排序法
假定这个数组的顺序是排好的,然后从头往后,如果有数比当前数大,则将这个数(比较大的数)的位置往后挪,直到
当前数大与或等于前面的数为止(也就是把数插入到比它小的数的后面).这个算法在排完前面k个数之后,可以保证
arr[1]到arr[k]是局部有序的,保证了插入过程的正确.
*/
AP.insertSort = function() {
var l = this.length-1;
var tmpArr;
var i = 0;
while (i++<l) {
var j = i;
tmpArr = this[i];
while (tmpArr<this[j-1] && j>0) {
this[j] = this[j-1];
j--;
}
this[j] = tmpArr;
}
};
/*
3.选择排序法
从后往前,把数与前面最大的数交换.
*/
AP.selectionSort = function() {
var l = this.length;
var maxPos;
var i = l;
while (i--) {
maxPos = i;
var j = -1;
while (j++<i) {
if (this[maxPos]<this[j]) {
maxPos = j;
}
if (maxPos != i) {
this.arrSwap(maxPos, i);
}
}
}
};
/*
4.归并排序法
归并排序的算法就是二分法。
分解:将n个元素分解成各含 一半元素的子序列。
解决:用归并排序法对两个子序列递归地排序。
合并:合并两个已排序的子序列排序结果。
在对子序列排列时,当其长度为1时递归结束,因为单个元素被认为是已排好序的.合并排序的.合并排序的关键
步骤在于合并目前产生的两个已排好序的子序列:A[p..q] 和 A[q+1…r],将它们合并成一个已排好序的子序列
A[p..r].
*/
AP.mergeSort = function() {
var temp = this.concat();
this.merge(temp, 0, this.length-1);
};
AP.merge = function(temp:Array, l:Number, r:Number) {
var mid = Math.floor((l+r)/2);
if (l == r) {
return;
}
this.merge(temp, l, mid);
this.merge(temp, mid+1, r);
for (var i = l; i<=r; i++) {
temp[i] = this[i];
}
var i1 = l;
var i2 = mid+1;
for (var cur = l; cur<=r; cur++) {
if (i1 == mid+1) {
this[cur] = temp[i2++];
} else if (i2>r) {
this[cur] = temp[i1++];
} else if (temp[i1]<temp[i2]) {
this[cur] = temp[i1++];
} else {
this[cur] = temp[i2++];
}
}
};
/*
5.快速排序法
主要思想是分治,就是先挑一个数(通常是首项),然后把比这个数小的数字都放到它的左边,比它大(或等于)的数
都放在它的右边.然后在左右两个小区间里反复这样一个过程,直到区间小到不能再分为止,此时排序工作也完成了.
*/
AP.quickSort = function() {
this.partition(0, this.length-1);
};
AP.partition = function(l:Number, u:Number) {
if (l>=u) {
return;
}
var m = l;
var i = l;
while (i++<=u) {
if (this[i]<this[l]) {
this.arrSwap(++m, i);
}
}
this.arrSwap(l, m);
this.partition(l, m-1);
this.partition(m+1, u);
};
AP.arrSwap = function(m:Number, n:Number) {
var tmp = this[m];
this[m] = this[n];
this[n] = tmp;
};
---------------------------------------------------------------------------------------------------
C#实现所有经典排序算法
1、选择排序
class SelectionSorter
{
private int min;
public void Sort(int[] arr)
{
for (int i = 0; i < arr.Length - 1; ++i)
{
min = i;
for (int j = i + 1; j < arr.Length; ++j)
{
if (arr[j] < arr[min])
min = j;
}
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
}
}
2、冒泡排序
class EbullitionSorter
{
public void Sort(int[] arr)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < arr.Length) && (!done))//判断长度
{
done = true;
for (i = 0; i < arr.Length - j; i++)
{
if (arr[i] > arr[i + 1])
{
done = false;
temp = arr[i];
arr[i] = arr[i + 1];//交换数据
arr[i + 1] = temp;
}
}
j++;
}
}
}
3、快速排序
class QuickSorter
{
private void swap(ref int l, ref int r)
{
int temp;
temp = l;
l = r;
r = temp;
}
public void Sort(int[] list, int low, int high)
{
int pivot;//存储分支点
int l, r;
int mid;
if (high <= low)
return;
else if (high == low + 1)
{
if (list[low] > list[high])
swap(ref list[low], ref list[high]);
return;
}
mid = (low + high) >> 1;
pivot = list[mid];
swap(ref list[low], ref list[mid]);
l = low + 1;
r = high;
do
{
while (l <= r && list[l] < pivot)
l++;
while (list[r] >= pivot)
r--;
if (l < r)
swap(ref list[l], ref list[r]);
} while (l < r);
list[low] = list[r];
list[r] = pivot;
if (low + 1 < r)
Sort(list, low, r - 1);
if (r + 1 < high)
Sort(list, r + 1, high);
}
}
4、插入排序
public class InsertionSorter
{
public void Sort(int[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
int t = arr[i];
int j = i;
while ((j > 0) && (arr[j - 1] > t))
{
arr[j] = arr[j - 1];//交换顺序
--j;
}
arr[j] = t;
}
}
}
5、希尔排序
public class ShellSorter
{
public void Sort(int[] arr)
{
int inc;
for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= arr.Length; i += inc)
{
int t = arr[i - 1];
int j = i;
while ((j > inc) && (arr[j - inc - 1] > t))
{
arr[j - 1] = arr[j - inc - 1];//交换数据
j -= inc;
}
arr[j - 1] = t;
}
}
}
}
6、归并排序
/// <summary>
/// 归并排序之归:归并排序入口
/// </summary>
/// <param name="data">无序的数组</param>
/// <returns>有序数组</returns>
/// <author>Lihua(www.zivsoft.com)</author>
int[] Sort(int[] data)
{
//取数组中间下标
int middle = data.Length / 2;
//初始化临时数组let,right,并定义result作为最终有序数组
int[] left = new int[middle], right = new int[middle], result = new int[data.Length];
if (data.Length % 2 != 0)//若数组元素奇数个,重新初始化右临时数组
{
right = new int[middle + 1];
}
if (data.Length <= 1)//只剩下1 or 0个元数,返回,不排序
{
return data;
}
int i = 0, j = 0;
foreach (int x in data)//开始排序
{
if (i < middle)//填充左数组
{
left[i] = x;
i++;
}
else//填充右数组
{
right[j] = x;
j++;
}
}
left = Sort(left);//递归左数组
right = Sort(right);//递归右数组
result = Merge(left, right);//开始排序
//this.Write(result);//输出排序,测试用(lihua debug)
return result;
}
/// <summary>
/// 归并排序之并:排序在这一步
/// </summary>
/// <param name="a">左数组</param>
/// <param name="b">右数组</param>
/// <returns>合并左右数组排序后返回</returns>
int[] Merge(int[] a, int[] b)
{
//定义结果数组,用来存储最终结果
int[] result = new int[a.Length + b.Length];
int i = 0, j = 0, k = 0;
while (i < a.Length && j < b.Length)
{
if (a[i] < b[j])//左数组中元素小于右数组中元素
{
result[k++] = a[i++];//将小的那个放到结果数组
}
else//左数组中元素大于右数组中元素
{
result[k++] = b[j++];//将小的那个放到结果数组
}
}
while (i < a.Length)//这里其实是还有左元素,但没有右元素
{
result[k++] = a[i++];
}
while (j < b.Length)//右右元素,无左元素
{
result[k++] = b[j++];
}
return result;//返回结果数组
}
注:此算法由周利华提供(http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html
)
7、基数排序
//基数排序
public int[] RadixSort(int[] ArrayToSort, int digit)
{
//low to high digit
for (int k = 1; k <= digit; k++)
{
//temp array to store the sort result inside digit
int[] tmpArray = new int[ArrayToSort.Length];
//temp array for countingsort
int[] tmpCountingSortArray = new int[10]{0,0,0,0,0,0,0,0,0,0};
//CountingSort
for (int i = 0; i < ArrayToSort.Length; i++)
{
//split the specified digit from the element
int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(10,k-1) - (ArrayToSort[i]/(int)Math.Pow(10,k))*10;
tmpCountingSortArray[tmpSplitDigit] += 1;
}
for (int m = 1; m < 10; m++)
{
tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];
}
//output the value to result
for (int n = ArrayToSort.Length - 1; n >= 0; n--)
{
int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k - 1) - (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;
tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];
tmpCountingSortArray[tmpSplitDigit] -= 1;
}
//copy the digit-inside sort result to source array
for (int p = 0; p < ArrayToSort.Length; p++)
{
ArrayToSort[p] = tmpArray[p];
}
}
return ArrayToSort;
}
8、计数排序
//计数排序
/// <summary>
/// counting sort
/// </summary>
/// <param name="arrayA">input array</param>
/// <param name="arrange">the value arrange in input array</param>
/// <returns></returns>
public int[] CountingSort(int[] arrayA, int arrange)
{
//array to store the sorted result,
//size is the same with input array.
int[] arrayResult = new int[arrayA.Length];
//array to store the direct value in sorting process
//include index 0;
//size is arrange+1;
int[] arrayTemp = new int[arrange+1];
//clear up the temp array
for(int i = 0; i <= arrange; i++)
{
arrayTemp[i] = 0;
}
//now temp array stores the count of value equal
for(int j = 0; j < arrayA.Length; j++)
{
arrayTemp[arrayA[j]] += 1;
}
//now temp array stores the count of value lower and equal
for(int k = 1; k <= arrange; k++)
{
arrayTemp[k] += arrayTemp[k - 1];
}
//output the value to result
for (int m = arrayA.Length-1; m >= 0; m--)
{
arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];
arrayTemp[arrayA[m]] -= 1;
}
return arrayResult;
}
9、小根堆排序
/// <summary>
/// 小根堆排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param>
/// <returns></returns>
private void HeapSort(ref double[] dblArray)
{
for (int i = dblArray.Length - 1; i >= 0; i--)
{
if (2 * i + 1 < dblArray.Length)
{
int MinChildrenIndex = 2 * i + 1;
//比较左子树和右子树,记录最小值的Index
if (2 * i + 2 < dblArray.Length)
{
if (dblArray[2 * i + 1] > dblArray[2 * i + 2])
MinChildrenIndex = 2 * i + 2;
}
if (dblArray[i] > dblArray[MinChildrenIndex])
{
ExchageValue(ref dblArray[i], ref dblArray[MinChildrenIndex]);
NodeSort(ref dblArray, MinChildrenIndex);
}
}
}
}
/// <summary>
/// 节点排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param>
private void NodeSort(ref double[] dblArray, int StartIndex)
{
while (2 * StartIndex + 1 < dblArray.Length)
{
int MinChildrenIndex = 2 * StartIndex + 1;
if (2 * StartIndex + 2 < dblArray.Length)
{
if (dblArray[2 * StartIndex + 1] > dblArray[2 * StartIndex + 2])
{
MinChildrenIndex = 2 * StartIndex + 2;
}
}
if (dblArray[StartIndex] > dblArray[MinChildrenIndex])
{
ExchageValue(ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);
StartIndex = MinChildrenIndex;
}
}
}
/// <summary>
/// 交换值
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
private void ExchageValue(ref double A, ref double B)
{
double Temp = A;
A = B;
B = Temp;
}
浙公网安备 33010602011771号