using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace BubbleSort
{
public class Program
{
static void Main(string[] args)
{
//五次比較
for (int i = 1; i = 5; i++)
{
Listint> list = new Listint>();
//插入2k個隨機數(shù)到數(shù)組中
for (int j = 0; j 2000; j++)
{
Thread.Sleep(1);
list.Add(new Random((int)DateTime.Now.Ticks).Next(0, 100000));
}
Console.WriteLine("\n第" + i + "次比較:");
Stopwatch watch = new Stopwatch();
watch.Start();
var result = list.OrderBy(single => single).ToList();
watch.Stop();
Console.WriteLine("\n快速排序耗費時間:" + watch.ElapsedMilliseconds);
Console.WriteLine("輸出前是十個數(shù):" + string.Join(",", result.Take(10).ToList()));
watch.Start();
result = BubbleSort(list);
watch.Stop();
Console.WriteLine("\n冒泡排序耗費時間:" + watch.ElapsedMilliseconds);
Console.WriteLine("輸出前是十個數(shù):" + string.Join(",", result.Take(10).ToList()));
}
}
//冒泡排序算法
static Listint> BubbleSort(Listint> list)
{
int temp;
//第一層循環(huán): 表明要比較的次數(shù),比如list.count個數(shù),肯定要比較count-1次
for (int i = 0; i list.Count - 1; i++)
{
//list.count-1:取數(shù)據(jù)最后一個數(shù)下標(biāo),
//j>i: 從后往前的的下標(biāo)一定大于從前往后的下標(biāo),否則就超越了。
for (int j = list.Count - 1; j > i; j--)
{
//如果前面一個數(shù)大于后面一個數(shù)則交換
if (list[j - 1] > list[j])
{
temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
return list;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace QuickSort
{
public class Program
{
static void Main(string[] args)
{
//5次比較
for (int i = 1; i = 5; i++)
{
Listint> list = new Listint>();
//插入200個隨機數(shù)到數(shù)組中
for (int j = 0; j 200; j++)
{
Thread.Sleep(1);
list.Add(new Random((int)DateTime.Now.Ticks).Next(0, 10000));
}
Console.WriteLine("\n第" + i + "次比較:");
Stopwatch watch = new Stopwatch();
watch.Start();
var result = list.OrderBy(single => single).ToList();
watch.Stop();
Console.WriteLine("\n系統(tǒng)定義的快速排序耗費時間:" + watch.ElapsedMilliseconds);
Console.WriteLine("輸出前是十個數(shù):" + string.Join(",", result.Take(10).ToList()));
watch.Start();
new QuickSortClass().QuickSort(list, 0, list.Count - 1);
watch.Stop();
Console.WriteLine("\n俺自己寫的快速排序耗費時間:" + watch.ElapsedMilliseconds);
Console.WriteLine("輸出前是十個數(shù):" + string.Join(",", list.Take(10).ToList()));
}
}
}
public class QuickSortClass
{
///summary>
/// 分割函數(shù)
////summary>
///param name="list">待排序的數(shù)組/param>
///param name="left">數(shù)組的左下標(biāo)/param>
///param name="right">/param>
///returns>/returns>
public int Division(Listint> list, int left, int right)
{
//首先挑選一個基準(zhǔn)元素
int baseNum = list[left];
while (left right)
{
//從數(shù)組的右端開始向前找,一直找到比base小的數(shù)字為止(包括base同等數(shù))
while (left right list[right] >= baseNum)
right = right - 1;
//最終找到了比baseNum小的元素,要做的事情就是此元素放到base的位置
list[left] = list[right];
//從數(shù)組的左端開始向后找,一直找到比base大的數(shù)字為止(包括base同等數(shù))
while (left right list[left] = baseNum)
left = left + 1;
//最終找到了比baseNum大的元素,要做的事情就是將此元素放到最后的位置
list[right] = list[left];
}
//最后就是把baseNum放到該left的位置
list[left] = baseNum;
//最終,我們發(fā)現(xiàn)left位置的左側(cè)數(shù)值部分比left小,left位置右側(cè)數(shù)值比left大
//至此,我們完成了第一篇排序
return left;
}
public void QuickSort(Listint> list, int left, int right)
{
//左下標(biāo)一定小于右下標(biāo),否則就超越了
if (left right)
{
//對數(shù)組進行分割,取出下次分割的基準(zhǔn)標(biāo)號
int i = Division(list, left, right);
//對“基準(zhǔn)標(biāo)號“左側(cè)的一組數(shù)值進行遞歸的切割,以至于將這些數(shù)值完整的排序
QuickSort(list, left, i - 1);
//對“基準(zhǔn)標(biāo)號“右側(cè)的一組數(shù)值進行遞歸的切割,以至于將這些數(shù)值完整的排序
QuickSort(list, i + 1, right);
}
}
}
}