简体   繁体   English

排序算法比较

[英]Sorting Algorithms Comparison

I have a project, I need to test 3 sorting algorithms and find out which one of them are Insertion Sort, Bubble Sort, Selection Sort. 我有一个项目,我需要测试3种排序算法,并找出其中的一种是插入排序,冒泡排序,选择排序。 I can't reach the methods so I test by using stopwatch in Java. 我无法达到这些方法,因此我使用Java中的秒表进行测试。 I created 3 arrays. 我创建了3个数组。

int[] arr = new int[100000]; //This array is sorted
int[] randomarr = new int[100000]; //This array is random
int[] reversearr = new int[100000]; //This array is reversed

I tested these algorithms and here are my results : 我测试了这些算法,这是我的结果:

- sort1 

 Sorted array took 11 seconds--Reversed array took 13 seconds--Random array took 24 seconds

- sort2

 Sorted array took 12 second--Reversed array took 12 seconds--Random array took 10 seconds

- sort3

 Sorted array took 1 millisecond--Reversed array took 12 seconds--Random array took 4 seconds

I am pretty sure sort3 is Insertion sort because it was faster than other ones in Sorted array. 我很确定sort3是插入排序,因为它比Sorted数组中的其他排序更快。 But I am confused about sort1 and sort2. 但是我对sort1和sort2感到困惑。 Bubble sort has O(n) in its best case, Insertion sort also have O(n) in its best case but when I check my results, insertion sort best case is 1 millisecond so bubble sort's best case should be 1 millisecond too? 冒泡排序的最佳情况为O(n),插入排序的最佳情况也为O(n),但是当我检查结果时,插入排序的最佳情况为1毫秒,所以冒泡排序的最佳情况也应该为1毫秒? How can I compare them ? 我如何比较它们?

So you clearly have to work out which is which by means of examining the time taken to sort various arrays. 因此,您显然必须通过检查对各种数组进行排序所花费的时间来确定哪个是哪个。

Here are some details on each of the algorithms (columns are best, average, worst, best space); 以下是有关每种算法的一些详细信息(列的最佳,平均,最差,最佳空间);

在此处输入图片说明

So you can quickly work out which one is selection sort by passing sorted lists of varying size to the algorithms, and seeing how long it takes. 因此,您可以通过将大小不同的已排序列表传递给算法,并查看需要多长时间来快速确定哪一个是选择排序。 The one which acts quadratically, is selection sort . 平方作用的是selection sort

Then, to decide between bubble sort and insertion sort , you'll need to read a little more deeply into how the algorithms work. 然后,要在bubble sortinsertion sort之间做出决定,您需要更深入地了解算法的工作原理。 bubble sort has an affectionately named issue; bubble sort有一个亲切的问题; dinosaurs and turtles , which is, large elements at the beginning are quickly dealt with, but small elements at the end are inefficiently dealt with. 恐龙和乌龟 ,一开始是大元素,但很快就被解决了,但结尾的小元素却没有被有效地处理。 You could then try profiling the two remaining algorithms ( bubble and insertion ) with sorted lists with the last element being the smallest, and see how they do. 然后,您可以尝试对剩下的两个算法( bubbleinsertion )进行分析,并使用排序后的列表,最后一个元素最小,然后看看它们是如何工作的。 The one which performs better will be insertion sort. 效果更好的一种是插入排序。

If do exactly these tests on arrays of varying sizes, using just simple versions of these algorithms, i get the following: 如果仅使用这些算法的简单版本对各种大小的数组进行精确的测试,我将得到以下结果:

在此处输入图片说明

so, based on the above info, i know that sort1 is bubble sort, sort3 is selection sort, and sort2 is insertion sort. 因此,根据上述信息,我知道sort1是冒泡排序,sort3是选择排序,sort2是插入排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM