冒泡排序的优化算法方式

huangapple go评论66阅读模式
英文:

Algorithm of Bubble sort optimised way

问题

"bubbleSort2" 是我写的,我发现它打印的 "cnt" 值比 "bubbleSort" 函数打印的要多。

下面是数组 int[] bbArr = {28, 6, 4, 2, 24}; 的输出结果:

bubble starts 

    6,4,2,24,28
    4,2,6,24,28
    2,4,6,24,28
    2,4,6,24,28
    Bubble cnt=10

bubble starts 

    2,28,6,4,24
    2,4,28,6,24
    2,4,6,28,24
    2,4,6,24,28
    Bubble cnt=10

我看到一个方法从数组的起始位置开始排序,而另一个方法从数组的最后位置开始排序。在特定的排序算法中,排序的顺序真的重要吗?

英文:

I have 2 different codes for sorting an array:

    static void bubbleSort2(int[] arr){
		System.out.println( "bubble starts ");
		int  tmp =0;int cnt =0;
		for (int i=0; i< arr.length-1 ; i++){
			for (int j=i+1; j< arr.length ; j++){
				if(arr[i] > arr[j]){
					tmp = arr[j];
					arr[j]=arr[i];
					arr[i] = tmp ;
				}
				cnt++;
			}
			printMe(arr);

		}
		System.out.println("Bubble cnt="+cnt);
		printMe(arr);
	}

And:

    static void bubbleSort(int a[])
    {
        int len = a.length; 
        int cnt=0;
		System.out.println( "bubble starts ");

        for (int i = 0; i < len-1; i++){
            for (int j = 0; j < len-i-1; j++) {
                if (a[j] > a[j+1]) 
                {
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            cnt++;
            }
            printMe(a);
        }
		System.out.println("Bubble cnt="+cnt);
		printMe(a);
    }

bubbleSort2 is written by me, and I find it is printing the cnt value more than what the bubbleSort function is printing .

Below is the output for array --> int[] bbArr= { 28,6,4,2,24 };

	bubbleSort(bbArr);
	bubbleSort2(ccArr);
bubble starts 

    6,4,2,24,28
    4,2,6,24,28
    2,4,6,24,28
    2,4,6,24,28
    Bubble cnt=10

bubble starts 

    2,28,6,4,24
    2,4,28,6,24
    2,4,6,28,24
    2,4,6,24,28
    Bubble cnt=10

I see that one method keeps sorting from the starting position, while the other method keeps sorting from the last position of array. Does it really matter the order of sorting in a particular sorting algo?

答案1

得分: 2

你的代码并没有实现冒泡排序,而是一种选择排序的变种。

冒泡排序的一个特点是它只交换相邻的值。而你的算法交换可能相距很远的值。

你说得对,标准的冒泡排序会在数组的右侧增加一个已排序的分区,而标准的选择排序会在数组的左侧增加一个已排序的分区。(显然,这些算法可以反向运行)。

我发现它打印cnt值比bubbleSort函数打印的要多。

不是更多。你的两个函数都执行可预测的比较次数,这仅取决于输入数组的大小,而不取决于实际内容:𝑛(𝑛−1)/2

在特定排序算法中排序的顺序真的很重要吗?

当比较这两种算法时,排序顺序并不重要。但更重要的是,当输入数组很大时,这两种排序算法的性能并不好。对于相对较大的数组,使用平均复杂度为O(𝑛log𝑛)的排序算法,如快速排序、归并排序、堆排序等,会获得更好的运行时间。

英文:

Your code is not implementing bubble sort, but a kind of selection sort.

One characteristic of bubble sort is that it only swaps adjacent values. Your algorithm swaps values that can be far apart.

You are right that standard bubble sort will grow a sorted partition at the right side of the array, while standard selection sort will grow a sorted partition at the left side of the array. (Obviously, these algorithms can be made to run in the opposite direction).

> I find it is printing the cnt value more than what the bubbleSort function is printing.

It is not more. Both of your functions execute a predictable number of comparisons which only depends on the size of the input array, not on the actual contents: 𝑛(𝑛−1)/2

> Does it really matter the order of sorting in a particular sorting algo ?

When comparing these two algorithms, it doesn't matter. But more importantly though, these two sorting algorithms do not perform well when the input array is large. For relatively large arrays you'll get better run times with sorting algorithms that have an average complexity of O(𝑛log𝑛), such as quick sort, merge sort, heap sort,...

huangapple
  • 本文由 发表于 2023年7月14日 04:06:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定