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个随机数到数组中
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("输出前是十个数:" + string.Join(",", result.Take(10).ToList()));
watch.Start();
result = BubbleSort(list);
watch.Stop();
Console.WriteLine("\n冒泡排序耗费时间:" + watch.ElapsedMilliseconds);
Console.WriteLine("输出前是十个数:" + string.Join(",", result.Take(10).ToList()));
}
}
//冒泡排序算法
static Listint> BubbleSort(Listint> list)
{
int temp;
//第一层循环: 表明要比较的次数,比如list.count个数,肯定要比较count-1次
for (int i = 0; i list.Count - 1; i++)
{
//list.count-1:取数据最后一个数下标,
//j>i: 从后往前的的下标一定大于从前往后的下标,否则就超越了。
for (int j = list.Count - 1; j > i; j--)
{
//如果前面一个数大于后面一个数则交换
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个随机数到数组中
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系统定义的快速排序耗费时间:" + watch.ElapsedMilliseconds);
Console.WriteLine("输出前是十个数:" + 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("输出前是十个数:" + string.Join(",", list.Take(10).ToList()));
}
}
}
public class QuickSortClass
{
///summary>
/// 分割函数
////summary>
///param name="list">待排序的数组/param>
///param name="left">数组的左下标/param>
///param name="right">/param>
///returns>/returns>
public int Division(Listint> list, int left, int right)
{
//首先挑选一个基准元素
int baseNum = list[left];
while (left right)
{
//从数组的右端开始向前找,一直找到比base小的数字为止(包括base同等数)
while (left right list[right] >= baseNum)
right = right - 1;
//最终找到了比baseNum小的元素,要做的事情就是此元素放到base的位置
list[left] = list[right];
//从数组的左端开始向后找,一直找到比base大的数字为止(包括base同等数)
while (left right list[left] = baseNum)
left = left + 1;
//最终找到了比baseNum大的元素,要做的事情就是将此元素放到最后的位置
list[right] = list[left];
}
//最后就是把baseNum放到该left的位置
list[left] = baseNum;
//最终,我们发现left位置的左侧数值部分比left小,left位置右侧数值比left大
//至此,我们完成了第一篇排序
return left;
}
public void QuickSort(Listint> list, int left, int right)
{
//左下标一定小于右下标,否则就超越了
if (left right)
{
//对数组进行分割,取出下次分割的基准标号
int i = Division(list, left, right);
//对“基准标号“左侧的一组数值进行递归的切割,以至于将这些数值完整的排序
QuickSort(list, left, i - 1);
//对“基准标号“右侧的一组数值进行递归的切割,以至于将这些数值完整的排序
QuickSort(list, i + 1, right);
}
}
}
}