| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 619 人关注过本帖, 1 人收藏
标题:怪哉?C#用thread写的并行快排比普通的慢
只看楼主 加入收藏
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏(1)
 问题点数:0 回复次数:2 
怪哉?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!!!
搜索更多相关主题的帖子: thread quiksort 并行程序 
2008-08-26 12:30
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏
得分:0 
顶顶 别陈了
顶顶 别陈了
2008-08-26 16:27
mkxzy
Rank: 2
等 级:论坛游民
帖 子:39
专家分:40
注 册:2007-3-26
收藏
得分:0 
线程切换要时间的
2008-08-27 09:10
快速回复:怪哉?C#用thread写的并行快排比普通的慢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018613 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved