几种常用排序算法运行效率比较
2007-10-25 23:33
495 查看
google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");
今天突发奇想,想玩玩一些基本的算法,看看他们的运行效率如何,废话少说,看看试验的结果:
1.数量级: 1024 * 1
冒泡排序用的时间是:0.0127910111480649 秒
选择排序用的时间是:0.00482072442167929 秒
插入排序用的时间是:0.0295230259711779 秒
希尔排序用的时间是:0.00567558167308974 秒
快速排序用的时间是:0.00129737159331703 秒
2.数量级:1024 * 50
冒泡排序用的时间是:19.2047039498037 秒
选择排序用的时间是:12.8408982147172 秒
插入排序用的时间是:8.41939883420938 秒
希尔排序用的时间是:9.90005713016599 秒
快速排序用的时间是:0.0246495015427938 秒
2.数量级:1024 * 100
冒泡排序用的时间是:82.4504259619589 秒
选择排序用的时间是:51.1897618272713 秒
插入排序用的时间是:32.164371449444 秒
希尔排序用的时间是:37.1519774161241 秒
快速排序用的时间是:0.0520460002598096 秒
参考大大们修改出来的测试代码:
/**//**************************************************************************************
* 功能:测试各种排序算法的效率
*
* 作者:清风工作室
* 日期:2007-7-15
**************************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Security.Cryptography;
namespace LMB.Sort
...{
class SortTest
...{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count);
//测试数据个数
const int MAXSIZE = 1024 * 100;
生成随机测试数组#region 生成随机测试数组
/**//// <summary>
/// 生成随机测试数组
/// </summary>
/// <param name="list"></param>
static void RandomArray(ref int[] list)
...{
for (int i = 0; i < MAXSIZE; i++)
...{
//使用简单的随机数生成器
int randomNum = new Random().Next(1, 1024 * 1024);
//使用加密服务生成随机数
byte[] randomNumber = new byte[1];
RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
Gen.GetBytes(randomNumber);
//合成两者作为随机数
list[i] = Convert.ToInt32(randomNumber[0]) % randomNum + randomNum;
}
}
#endregion
冒泡排序#region 冒泡排序
/**//// <summary>
/// 冒泡排序
/// </summary>
/// <param name="list"></param>
static void DoubleSorter(int[] list)
...{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
...{
done = true;
for (i = 0; i < list.Length - j; i++)
...{
if (list[i] > list[i + 1])
...{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
#endregion
选择排序#region 选择排序
/**//// <summary>
/// 选择排序
/// </summary>
/// <param name="list"></param>
static void SelectionSorter(int[] list)
...{
int min;
for (int i = 0; i < list.Length - 1; i++)
...{
min = i;
for (int j = i + 1; j < list.Length; j++)
...{
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
}
}
#endregion
插入排序#region 插入排序
/**//// <summary>
/// 插入排序
/// </summary>
/// <param name="list"></param>
static void InsertionSorter(int[] list)
...{
for (int i = 1; i < list.Length; i++)
...{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
...{
list[j] = list[j - 1];
j--;
}
list[j] = t;
}
}
#endregion
希尔排序#region 希尔排序
/**//// <summary>
/// 希尔排序
/// </summary>
/// <param name="list"></param>
static void ShellSorter(int[] list)
...{
int inc;
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
...{
for (int i = inc + 1; i <= list.Length; i += inc)
...{
int t = list[i - 1];
int j = i;
while ((j > inc) && (list[j - inc - 1] > t))
...{
list[j - 1] = list[j - inc - 1];
j -= inc;
}
list[j - 1] = t;
}
}
}
#endregion
快速排序#region 快速排序
/**//// <summary>
/// 快速排序辅助函数(两数互交换)
/// </summary>
/// <param name="l"></param>
/// <param name="r"></param>
static void Swap(ref int left, ref int right)
...{
int tmp;
tmp = left;
left = right;
right = tmp;
}
/**//// <summary>
/// 快速排序辅助排序函数
/// </summary>
/// <param name="list"></param>
/// <param name="low"></param>
/// <param name="high"></param>
static void Sort(int[] list, int low, int high)
...{
if (high <= low)
...{
return;
}
else if (high == low + 1)
...{
if (list[low] > list[high])
...{
Swap(ref list[low], ref list[high]);
return;
}
}
QuickSorter(list, low, high);
}
/**//// <summary>
/// 快速排序辅助函数(分割数组)
/// </summary>
/// <param name="list"></param>
/// <param name="low"></param>
/// <param name="high"></param>
/// <returns></returns>
static int Partition(int[] list, int low, int high)
...{
int pivot;
pivot = list[low];
while (low < high)
...{
while (low < high && list[high] >= pivot)
...{
high--;
}
if (low != high)
...{
Swap(ref list[low], ref list[high]);
low++;
}
while (low < high && list[low] <= pivot)
...{
low++;
}
if (low != high)
...{
Swap(ref list[low], ref list[high]);
high--;
}
}
return low;
}
/**//// <summary>
/// 快速排序
/// </summary>
/// <param name="list"></param>
/// <param name="low"></param>
/// <param name="high"></param>
static void QuickSorter(int[] list, int low, int high)
...{
if (low < high)
...{
int pivot = Partition(list, low, high);
QuickSorter(list, low, pivot - 1);
QuickSorter(list, pivot + 1, high);
}
}
#endregion
/**//// <summary>
/// 测试程序
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
...{
long count = 0;
long count1 = 0;
long freq = 0;
double result = 0;
int[] list = new int[MAXSIZE];
RandomArray(ref list);
冒泡排序测试#region 冒泡排序测试
int[] listDouble = new int[MAXSIZE];
list.CopyTo(listDouble, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
DoubleSorter(listDouble);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("冒泡排序用的时间是:{0} 秒", result);
#endregion
选择排序测试#region 选择排序测试
int[] listSelection = new int[MAXSIZE];
list.CopyTo(listSelection, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
SelectionSorter(listSelection);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("选择排序用的时间是:{0} 秒", result);
#endregion
插入排序测试#region 插入排序测试
int[] listInsertion = new int[MAXSIZE];
list.CopyTo(listInsertion, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
InsertionSorter(listInsertion);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("插入排序用的时间是:{0} 秒", result);
#endregion
希尔排序测试#region 希尔排序测试
int[] listShell = new int[MAXSIZE];
list.CopyTo(listShell, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
ShellSorter(listShell);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("希尔排序用的时间是:{0} 秒", result);
#endregion
快速排序测试#region 快速排序测试
int[] listQuick = new int[MAXSIZE];
list.CopyTo(listQuick, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
QuickSorter(listQuick, 0, MAXSIZE - 1);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("快速排序用的时间是:{0} 秒", result);
#endregion
Console.ReadLine();
}
}
}
快速排序的速度惊人,我真怀疑我的程序有问题,但是用小量数据验证又没有问题.
今天突发奇想,想玩玩一些基本的算法,看看他们的运行效率如何,废话少说,看看试验的结果:
1.数量级: 1024 * 1
冒泡排序用的时间是:0.0127910111480649 秒
选择排序用的时间是:0.00482072442167929 秒
插入排序用的时间是:0.0295230259711779 秒
希尔排序用的时间是:0.00567558167308974 秒
快速排序用的时间是:0.00129737159331703 秒
2.数量级:1024 * 50
冒泡排序用的时间是:19.2047039498037 秒
选择排序用的时间是:12.8408982147172 秒
插入排序用的时间是:8.41939883420938 秒
希尔排序用的时间是:9.90005713016599 秒
快速排序用的时间是:0.0246495015427938 秒
2.数量级:1024 * 100
冒泡排序用的时间是:82.4504259619589 秒
选择排序用的时间是:51.1897618272713 秒
插入排序用的时间是:32.164371449444 秒
希尔排序用的时间是:37.1519774161241 秒
快速排序用的时间是:0.0520460002598096 秒
参考大大们修改出来的测试代码:
/**//**************************************************************************************
* 功能:测试各种排序算法的效率
*
* 作者:清风工作室
* 日期:2007-7-15
**************************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Security.Cryptography;
namespace LMB.Sort
...{
class SortTest
...{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count);
//测试数据个数
const int MAXSIZE = 1024 * 100;
生成随机测试数组#region 生成随机测试数组
/**//// <summary>
/// 生成随机测试数组
/// </summary>
/// <param name="list"></param>
static void RandomArray(ref int[] list)
...{
for (int i = 0; i < MAXSIZE; i++)
...{
//使用简单的随机数生成器
int randomNum = new Random().Next(1, 1024 * 1024);
//使用加密服务生成随机数
byte[] randomNumber = new byte[1];
RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
Gen.GetBytes(randomNumber);
//合成两者作为随机数
list[i] = Convert.ToInt32(randomNumber[0]) % randomNum + randomNum;
}
}
#endregion
冒泡排序#region 冒泡排序
/**//// <summary>
/// 冒泡排序
/// </summary>
/// <param name="list"></param>
static void DoubleSorter(int[] list)
...{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
...{
done = true;
for (i = 0; i < list.Length - j; i++)
...{
if (list[i] > list[i + 1])
...{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
#endregion
选择排序#region 选择排序
/**//// <summary>
/// 选择排序
/// </summary>
/// <param name="list"></param>
static void SelectionSorter(int[] list)
...{
int min;
for (int i = 0; i < list.Length - 1; i++)
...{
min = i;
for (int j = i + 1; j < list.Length; j++)
...{
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
}
}
#endregion
插入排序#region 插入排序
/**//// <summary>
/// 插入排序
/// </summary>
/// <param name="list"></param>
static void InsertionSorter(int[] list)
...{
for (int i = 1; i < list.Length; i++)
...{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
...{
list[j] = list[j - 1];
j--;
}
list[j] = t;
}
}
#endregion
希尔排序#region 希尔排序
/**//// <summary>
/// 希尔排序
/// </summary>
/// <param name="list"></param>
static void ShellSorter(int[] list)
...{
int inc;
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
...{
for (int i = inc + 1; i <= list.Length; i += inc)
...{
int t = list[i - 1];
int j = i;
while ((j > inc) && (list[j - inc - 1] > t))
...{
list[j - 1] = list[j - inc - 1];
j -= inc;
}
list[j - 1] = t;
}
}
}
#endregion
快速排序#region 快速排序
/**//// <summary>
/// 快速排序辅助函数(两数互交换)
/// </summary>
/// <param name="l"></param>
/// <param name="r"></param>
static void Swap(ref int left, ref int right)
...{
int tmp;
tmp = left;
left = right;
right = tmp;
}
/**//// <summary>
/// 快速排序辅助排序函数
/// </summary>
/// <param name="list"></param>
/// <param name="low"></param>
/// <param name="high"></param>
static void Sort(int[] list, int low, int high)
...{
if (high <= low)
...{
return;
}
else if (high == low + 1)
...{
if (list[low] > list[high])
...{
Swap(ref list[low], ref list[high]);
return;
}
}
QuickSorter(list, low, high);
}
/**//// <summary>
/// 快速排序辅助函数(分割数组)
/// </summary>
/// <param name="list"></param>
/// <param name="low"></param>
/// <param name="high"></param>
/// <returns></returns>
static int Partition(int[] list, int low, int high)
...{
int pivot;
pivot = list[low];
while (low < high)
...{
while (low < high && list[high] >= pivot)
...{
high--;
}
if (low != high)
...{
Swap(ref list[low], ref list[high]);
low++;
}
while (low < high && list[low] <= pivot)
...{
low++;
}
if (low != high)
...{
Swap(ref list[low], ref list[high]);
high--;
}
}
return low;
}
/**//// <summary>
/// 快速排序
/// </summary>
/// <param name="list"></param>
/// <param name="low"></param>
/// <param name="high"></param>
static void QuickSorter(int[] list, int low, int high)
...{
if (low < high)
...{
int pivot = Partition(list, low, high);
QuickSorter(list, low, pivot - 1);
QuickSorter(list, pivot + 1, high);
}
}
#endregion
/**//// <summary>
/// 测试程序
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
...{
long count = 0;
long count1 = 0;
long freq = 0;
double result = 0;
int[] list = new int[MAXSIZE];
RandomArray(ref list);
冒泡排序测试#region 冒泡排序测试
int[] listDouble = new int[MAXSIZE];
list.CopyTo(listDouble, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
DoubleSorter(listDouble);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("冒泡排序用的时间是:{0} 秒", result);
#endregion
选择排序测试#region 选择排序测试
int[] listSelection = new int[MAXSIZE];
list.CopyTo(listSelection, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
SelectionSorter(listSelection);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("选择排序用的时间是:{0} 秒", result);
#endregion
插入排序测试#region 插入排序测试
int[] listInsertion = new int[MAXSIZE];
list.CopyTo(listInsertion, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
InsertionSorter(listInsertion);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("插入排序用的时间是:{0} 秒", result);
#endregion
希尔排序测试#region 希尔排序测试
int[] listShell = new int[MAXSIZE];
list.CopyTo(listShell, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
ShellSorter(listShell);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("希尔排序用的时间是:{0} 秒", result);
#endregion
快速排序测试#region 快速排序测试
int[] listQuick = new int[MAXSIZE];
list.CopyTo(listQuick, 0);
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
QuickSorter(listQuick, 0, MAXSIZE - 1);
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("快速排序用的时间是:{0} 秒", result);
#endregion
Console.ReadLine();
}
}
}
快速排序的速度惊人,我真怀疑我的程序有问题,但是用小量数据验证又没有问题.
相关文章推荐
- 几种常用排序算法运行效率比较
- 几种排序算法运行时间比较
- [转]几种常用分页算法运行效率大比拼
- 转:几种排序算法效率的比较
- 聊聊效率较高的几种常用排序算法
- 转:几种排序算法效率的比较
- 写出几种常用的排序算法以及执行效率
- 几种排序算法效率的比较
- 几种排序算法效率的比较
- 几种常用排序算法的比较
- JavaSE第三十七讲:几种常用排序算法比较
- 几种排序算法的效率比较
- C++基础笔记之六:几种常用排序算法的比较
- 几种常用的排序算法以及执行效率
- 几种排序算法的效率比较
- OFFICE文档转换到PDF的几种方法与转换效率和性能的简单比较
- Python执行效率测试模块timei的使用方法与与常用Python用法的效率比较
- 几种常用算法的复杂度比较
- Java的几种常用排序算法分析
- Java从控制台读取数据,输入流System.in的几种方法效率比较