110,555
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 快速排序Bata2
{
using System;
public static class Program
{
public static void Main()
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
const int z=10000000;
//随机生成一千万个数。
int[] a = new int[z];
Random random = new Random();
for (int i = 0; i < z; i++)
{
a[i] = random.Next(0, z);
}
watch.Start();
//计时器开始。
QuickSort(a, 0, z-1);
watch.Stop();
//计时器结束。
for (int i = z - 1000; i < z;i++ )
{
Console.WriteLine(a[i]);
}
string time= watch.ElapsedMilliseconds.ToString();
Console.Write("排序所用时间 :" +" "+ time+" ms");
Console.Write("\r\n");
}
private static void QuickSort(int[] a, int low, int high)
{
if (low >= high)
{
return;
}
int pivot = QuickSortOnce(a,low,high);
//输出每一次排序。
//对枢轴左端进行排序。
QuickSort(a, low, pivot - 1);
//对枢轴右端进行排序。
QuickSort(a, pivot + 1, high);
}
private static int QuickSortOnce(int[] a, int low, int high)
{
//将首个元素作为枢轴。
int pivot = a[low];
int i = low, j = high;
while (i < j)
{
//从右往左,寻找首个小于povit的元素。
while (a[j] >= pivot && i < j)
{
j--;
}
//执行到此,j一定指向从右端起首个小于或等于povit的元素。执行替换。
a[i] = a[j];
//从左往右,寻找首个大于povit的元素。
while (a[i] <= pivot && i < j)
{
i++;
}
////执行到此,j一定指向从右端起首个大于或等于povit的元素。执行替换。
a[j] = a[i];
}
//退出while循环,执行至此,必定是i==j的情况。i(或j)指向的既是枢轴的位置,定位该趟排序的枢轴并将该位置返回。
a[i] = pivot;
return i;
}
}
}
for (int i = 0; i < z; i++)
{
a[i] = z - i;
}
应写解决好本身的算法,再去下结论a = fastCSharp.algorithm.quickSort.getTop(a, 1000).GetArray();
这个仅仅是top 1000,并没有对这1000个数排序,应该修改为a = fastCSharp.subArrayExtension.sort(fastCSharp.algorithm.quickSort.getTop(a, 1000)).sort().GetArray();
不过测试时间没有差别,1000个数的排序相对与1000W个数的扫描根本不算什么。 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
const int z = 10000000;
//随机生成一千万个数。
int[] a, b = new int[z];
Random random = new Random();
for (int i = 0; i < z; i++)
{
b[i] = random.Next(0, z);
}
do
{
Array.Copy(b, a = new int[z], z);
watch.Restart();
//计时器开始。
QuickSort(a, 0, z - 1);
watch.Stop();
//计时器结束。
//for (int i = z - 1000; i < z; i++)
//{
// Console.WriteLine(a[i]);
//}
string time = watch.ElapsedMilliseconds.ToString();
Console.Write("QuickSort 排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
Array.Copy(b, a = new int[z], z);
watch.Restart();
//计时器开始。
Array.Sort(a);
watch.Stop();
//计时器结束。
//for (int i = z - 1000; i < z; i++)
//{
// Console.WriteLine(a[i]);
//}
time = watch.ElapsedMilliseconds.ToString();
Console.Write("Array.Sort 排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
Array.Copy(b, a = new int[z], z);
watch.Restart();
int[] c = new int[z];
foreach (int v in a) ++c[v];
int ai = 0, n = 0;
foreach (int v in c)
{
for (int j = Math.Min(ai + v, 1000); ai != j; a[ai++] = n) ;
if (ai == 1000) break;
++n;
}
//计时器开始。
watch.Stop();
//计时器结束。
//for (int i = z - 1000; i < z; i++)
//{
// Console.WriteLine(a[i]);
//}
time = watch.ElapsedMilliseconds.ToString();
Console.Write("计数排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
Array.Copy(b, a = new int[z], z);
watch.Restart();
//计时器开始。
fastCSharp.arrayExtension.sort(a);
watch.Stop();
//计时器结束。
//for (int i = z - 1000; i < z; i++)
//{
// Console.WriteLine(a[i]);
//}
time = watch.ElapsedMilliseconds.ToString();
Console.Write("fastCSharp.arrayExtension.sort 排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
Array.Copy(b, a = new int[z], z);
watch.Restart();
//计时器开始。
fastCSharp.arrayExtension.rangeSort(a, 0, 1000);
watch.Stop();
//计时器结束。
//for (int i = z - 1000; i < z; i++)
//{
// Console.WriteLine(a[i]);
//}
time = watch.ElapsedMilliseconds.ToString();
Console.Write("fastCSharp.arrayExtension.rangeSort 排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
Array.Copy(b, a = new int[z], z);
watch.Restart();
//计时器开始。
a = fastCSharp.algorithm.quickSort.getTop(a, 1000).GetArray();//未对外开放
watch.Stop();
//计时器结束。
//for (int i = z - 1000; i < z; i++)
//{
// Console.WriteLine(a[i]);
//}
time = watch.ElapsedMilliseconds.ToString();
Console.Write("fastCSharp_getTop 排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
Console.Write("\r\n");
Console.ReadKey();
}
while (true);
QuickSort 排序所用时间 : 1075 ms
Array.Sort 排序所用时间 : 1082 ms
计数排序所用时间 : 146 ms
fastCSharp.arrayExtension.sort 排序所用时间 : 161 ms
fastCSharp.arrayExtension.rangeSort 排序所用时间 : 89 ms
fastCSharp_getTop 排序所用时间 : 7 ms
QuickSort 排序所用时间 : 1053 ms
Array.Sort 排序所用时间 : 1068 ms
计数排序所用时间 : 145 ms
fastCSharp.arrayExtension.sort 排序所用时间 : 132 ms
fastCSharp.arrayExtension.rangeSort 排序所用时间 : 88 ms
fastCSharp_getTop 排序所用时间 : 6 ms
random.Next(0, z);
这行代码意味着应该考虑计数排序。watch.Start();
var k = a.OrderBy(n => n);
foreach(var item in k) { }
watch.Stop();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
const int z = 10000000;
//随机生成一千万个数。
int[] a = new int[z];
Random random = new Random();
for (int i = 0; i < z; i++)
{
a[i] = random.Next(0, z);
}
watch.Start();
//计时器开始。
a.OrderBy(n => n);
watch.Stop();
string time = watch.ElapsedMilliseconds.ToString();
Console.Write("Linq排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
for (int i = 0; i < z; i++)
{
a[i] = random.Next(0, z);
}
watch = new System.Diagnostics.Stopwatch();
watch.Start();
Array.Sort(a);
watch.Stop();
//计时器结束。
//for (int i = z - 1000; i < z; i++)
//{
// Console.WriteLine(a[i]);
//}
time = watch.ElapsedMilliseconds.ToString();
Console.Write("Array.Sort排序所用时间 :" + " " + time + " ms");
Console.Write("\r\n");
Console.ReadKey();
}
}
}
Linq排序所用时间 : 1 ms
Array.Sort排序所用时间 : 1461 ms