怪哉?C#用thread写的并行快排比普通的慢
using System;using System.Threading;
namespace VcQuickSort
{
/// <summary>
/// ClassQuickSort 快速排序。
/// 范维肖
/// </summary>
public class QuickSort1 //普通快排
{
public QuickSort1()
{
}
private void Swap(ref int i, ref int j)
//swap two integer
{
int t;
t = i;
i = j;
j = t;
}
public void myQuickSort1(int[] list, int low, int high)
{
if (low < high)
{
int pivot = Partition(list, low, high);
myQuickSort1(list, low, pivot - 1);
myQuickSort1(list, pivot + 1, high);
}
}
private int Partition(int[] list, int low, int high)
{
//get the pivot of the arrary list
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;
}
}
public class QuickSort //并行快排
{
public QuickSort(int[] vlist, int vlow, int vhigh)
{
this.list = vlist;
this.high = vhigh;
this.low = vlow;
}
private void Swap(ref int i, ref int j)
//swap two integer
{
int t;
t = i;
i = j;
j = t;
}
int[] list;
int low, high;
public void myQuickSort()
{
if (low < high)
{
int pivot = Partition(list, low, high);
QuickSort qs1 = new QuickSort(list,low,pivot-1);
QuickSort qs2 = new QuickSort(list, pivot + 1, high);
Thread t1 = new Thread(qs1.myQuickSort);
Thread t2 = new Thread(qs2.myQuickSort);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
//while (t1.IsAlive || t2.IsAlive) continue;
//myQuickSort(list, pivot + 1, high);
}
}
private int Partition(int[] list, int low, int high)
{
//get the pivot of the arrary list
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;
}
}
class Program
{
static void showArray(int[] A)
{
foreach (int num in A)
{
System.Console.Write(num.ToString() + " ");
}
System.Console.WriteLine();
}
static void produceRandomArray(ref int []A,int count)
{
A = new int[count];
Random r = new Random(System.DateTime.Now.Second);
int i = 0;
while (i<count)
{
A[i] = r.Next(2000);
i++;
}
}
static void Main(string []args)
{
//int[] v = new int[] {-10,8,20,5,7,9,1,6 };
Console = new ("out.txt");
int[] v = null;
int count=2000;
produceRandomArray(ref v, count);
int[] v1 = new int[v.Length];
v.CopyTo(v1, 0);
Console.WriteLine("The size of initial Array is {0}, and the array is ",count);
//showArray(v);
DateTime dt;// = DateTime.Now;
//Console.WriteLine("{0}:{1}:{2}.{3}", dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
QuickSort qs = new QuickSort(v,0,v.Length-1);
Console.WriteLine("QuickSort with thread started!!!");
dt = DateTime.Now;
Console.WriteLine("{0}:{1}:{2}.{3}", dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
qs.myQuickSort();
dt = DateTime.Now;
Console.WriteLine("{0}:{1}:{2}.{3}", dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
Console.WriteLine("QuickSort with thread ended!!!");
// showArray(v);
QuickSort1 qs1 = new QuickSort1();
Console.WriteLine("normal QuickSort1 started!!!");
dt = DateTime.Now;
Console.WriteLine("{0}:{1}:{2}.{3}", dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
qs1.myQuickSort1(v1,0,v1.Length-1);
dt = DateTime.Now;
Console.WriteLine("{0}:{1}:{2}.{3}", dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
Console.WriteLine("normal QuickSort1 ended!!!");
//showArray(v);
Console.Close();
}
}
}
out.txt中的结果
The size of initial Array is 2000, and the array is
QuickSort with thread started!!!
12:16:36.359
12:16:37.484
QuickSort with thread ended!!!
normal QuickSort1 started!!!
12:16:37.484
12:16:37.484
normal QuickSort1 ended!!!