所以我在我的大学里上课,我们做各种各样的事情,现在我们进行递归排序,也就是quickSort。哪里好,你们都知道它做了什么,将数组分成两部分,依此类推,直到它以1个元素结束,然后对它们进行排序。
因此,我们讨论哪一个会更快以及为什么这就是所谓的quicksort,它最终表明quickSort的复杂性是n.log2(n),而例如冒泡排序是n ^ 2。好的,所以我在c#中编写了bouth代码并使用c#i calcualte hte ms的秒表来执行bouth alogirithyms,其中我生成-65000,65000之间的随机数和长度为50 000个元素的数组。
所以bubblesort第二次做第23次排序29(因为它们是random ..),即使我用第一个元素n-i制作一个带有50k元素的数组。 Aka它需要对所有东西进行排序,它会持续27秒(如此接近随机)如果我从i开始制作一个数组,它需要17秒来对它进行排序。
所以我运行了quickSort,并且已经过了5分钟仍然没有结果......如果我用arra [i] = i运行它;或者n-i;它给了我stackoverflow异常。
qSort更快的唯一地方是拥有一个包含100或200等元素的数组,并且它们已经被排序(也就是数组[i] = i),它的速度更快,如0.1-0.2秒。 buble排序可以排序真正庞大的数组,而qSort给我stackoverflow。
回到复杂性,qSort为50 000个元素= 780482 而bublesort = 2 500 000 000 它明确的一天qSort需要是什么?快99.99%。但它不是吗?为什么我真的好奇这个,因为我的Lector说qSort要快得多。但经过各种测试后,它似乎比bubblesort更快(稍微)一点,而且它只与没有那么多元素的数组一起工作。(10k randoms元素qSort 17秒,而bublesort 7)。
我的电脑是i7 4cores,每个都是2.6 ghz,我有16 GB RAM DDR4。
如果有人清楚这件事,我会很高兴,因为我的导师似乎给了我一个虚假的信息。
编辑口号: 的qsort ..................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QuickSort
{
class QuickSort
{
static public int Partition(int[] numbers, int left, int right)
{
int pivot = numbers[left];
while (true)
{
while (numbers[left] < pivot)
left++;
while (numbers[right] > pivot)
right--;
if (left < right)
{
int temp = numbers[right];
numbers[right] = numbers[left];
numbers[left] = temp;
}
else
{
return right;
}
}
}
static public void SortQuick(int[] arr, int left, int right)
{
// For Recusrion
if (left < right)
{
int pivot = Partition(arr, left, right);
if (pivot > 1)
SortQuick(arr, left, pivot - 1);
if (pivot + 1 < right)
SortQuick(arr, pivot + 1, right);
}
}
static void Main(string[] args)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
Random rnd = new Random();
int max = Convert.ToInt32(Console.ReadLine());
int[] numbers = new int[max];
for (int i = 0; i < max; i++)
{
numbers[i] = rnd.Next(-65000, 65000);
}
// the code that you want to measure comes here
SortQuick(numbers, 0, max - 1);
for (int i = 0; i < max; i++)
Console.WriteLine(numbers[i]);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine(elapsedMs);
Console.ReadLine();
}
}
}
Bublesort ................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bublesort
{
class Program
{
static void Main(string[] args)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
Random rnd = new Random();
int temp = 0;
for (int i = 0; i < n; i++)
{
arr[i] = rnd.Next(-65000,65000);
}
for (int write = 0; write < arr.Length; write++)
{
for (int sort = 0; sort < arr.Length - 1; sort++)
{
if (arr[sort] > arr[sort + 1])
{
temp = arr[sort + 1];
arr[sort + 1] = arr[sort];
arr[sort] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
Console.WriteLine(arr[i]);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine(elapsedMs);
}
}
}
答案 0 :(得分:0)
如果数组中有重复项,则会发生两个while循环以数组[left] = array [right] = pivot结束。交换什么都不做,因为你没有增加左右减少,你的快速排序将永远停留。
尝试使用长度为2和两个相等元素的数组。它会立即挂起。
此外,通过选择数组[left]作为数据透视表,一旦修复了错误,就可以保证排序数组的最坏情况(二次)行为。
答案 1 :(得分:0)
static public void Quicksort(int[] numbers, int left, int right)
{
int i = left, j = right;
int pivot = numbers[(left+right)/2];
while (i <= j)
{
while (numbers[i] < pivot)
{
i++;
}
while (numbers[j] > pivot)
{
j--;
}
if (i <=j)
{
// Swap
int tmp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = tmp;
i++;
j--;
}
}
// Recursive calls
if (left < j)
{
Quicksort(numbers, left, j);
}
if (i < right)
{
Quicksort(numbers, i, right);
}
}
是的问题解决了这个问题,当我把枢轴作为中间元素时,我现在在间隔[-65000到65000]中排序10万个随机元素,持续8秒。布雷排序需要71秒。或Q排序比bublesort快90%;)。