冒泡排序优于选择排序?
Bubble Sort Outperforming Selection Sort?
根据我的阅读,即使它们的大 Oh 表示法相同,当冒泡排序和选择排序数组时,随着数组大小的增长,选择排序应该优于冒泡排序。
但在我的代码中,随着数组变大,冒泡排序的性能始终优于选择排序。
在程序中,用户指定数组中的项数,以及创建和排序包含这么多项的数组的次数(以获得更准确的结果)。
这是我的代码:
import java.util.Scanner;
import java.util.Random;
public class SelectionSorting
{
public static int[] arr;
public static long runningSelectionTime, runningBubbleTime;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number of the items in the array: ");
int i = in.nextInt();
System.out.println("Please enter the number of iterations: ");
int iters = in.nextInt();
arr = new int[i];
for (int x = 0; x < iters; x++)
{
for (int n = 0; n < i; n++)
{
arr[n] = randomness();
}
long startSelectionTime = System.nanoTime();
selectionSort(arr);
long endSelectionTime = System.nanoTime();
runningSelectionTime += endSelectionTime - startSelectionTime;
}
System.out.println("Selection Sort: ");
System.out.println("Total running time: " + runningSelectionTime + " and the average is " + runningSelectionTime/iters);
for (int x = 0; x < iters; x++)
{
for (int n = 0; n < i; n++)
{
arr[n] = randomness();
}
long startBubbleTime = System.nanoTime();
bubbleSort(arr);
long endBubbleTime = System.nanoTime();
runningBubbleTime += endBubbleTime - startBubbleTime;
}
System.out.println("Bubble Sort: ");
System.out.println("Total running time: " + runningBubbleTime + " and the average is " + runningBubbleTime/iters);
}
public static void selectionSort(int[] array)
{
for (int i = 0; i < array.length - 1; i++)
{
int iMin = i;
for (int j = i + 1; j < array.length; j++)
{
if (array[j] < array[iMin])
{
iMin = j;
}
}
if (iMin != i)
{
int temp = array[i];
array[i] = array[iMin];
array[iMin] = temp;
}
}
}
public static void bubbleSort(int[] arr)
{
int unsorted = arr.length;
while (unsorted != 0)
{
int lastSwap = 0;
for (int i = 1; i < unsorted; i++)
{
if (arr[i - 1] > arr[i])
{
swap(arr, i, i - 1);
lastSwap = i;
}
}
unsorted = lastSwap;
}
}
private static void swap(int[] arr, int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static int randomness()
{
Random rand = new Random();
int random = rand.nextInt();
if (random < 0)
{
random = random * -1;
}
do {
random = random / 10;
} while (random > 100);
return random;
}
}
如果你尝试输入 500 和 1000,运行 冒泡排序的时间会比选择排序短。
有趣的是,如果我去掉变量 "iters," 那么结果就符合预期。但是,iters 似乎应该使 运行 时间 更多 准确,而不是更少。
关于这是为什么的任何想法?我做错什么了吗?冒泡排序是否有可能(始终)优于选择排序?
(为了防止混淆,我看到了问题,但它没有解决同样的问题。)
你的冒泡排序更快,因为它不起作用。
arr[i] = temp;
应该是
arr[i-1] = temp;
输出为 10000 * 3
选择排序:
总 运行 时间:216077472 平均为 72025824
冒泡排序:
总 运行 时间:402583050 平均为 134194350
输出为 500 * 1000
选择排序:
总 运行 时间:123621068 平均为 123621
冒泡排序:
总 运行 时间:317450183 平均为 317450
使用 使用 jdk 7
根据我的阅读,即使它们的大 Oh 表示法相同,当冒泡排序和选择排序数组时,随着数组大小的增长,选择排序应该优于冒泡排序。
但在我的代码中,随着数组变大,冒泡排序的性能始终优于选择排序。
在程序中,用户指定数组中的项数,以及创建和排序包含这么多项的数组的次数(以获得更准确的结果)。
这是我的代码:
import java.util.Scanner;
import java.util.Random;
public class SelectionSorting
{
public static int[] arr;
public static long runningSelectionTime, runningBubbleTime;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number of the items in the array: ");
int i = in.nextInt();
System.out.println("Please enter the number of iterations: ");
int iters = in.nextInt();
arr = new int[i];
for (int x = 0; x < iters; x++)
{
for (int n = 0; n < i; n++)
{
arr[n] = randomness();
}
long startSelectionTime = System.nanoTime();
selectionSort(arr);
long endSelectionTime = System.nanoTime();
runningSelectionTime += endSelectionTime - startSelectionTime;
}
System.out.println("Selection Sort: ");
System.out.println("Total running time: " + runningSelectionTime + " and the average is " + runningSelectionTime/iters);
for (int x = 0; x < iters; x++)
{
for (int n = 0; n < i; n++)
{
arr[n] = randomness();
}
long startBubbleTime = System.nanoTime();
bubbleSort(arr);
long endBubbleTime = System.nanoTime();
runningBubbleTime += endBubbleTime - startBubbleTime;
}
System.out.println("Bubble Sort: ");
System.out.println("Total running time: " + runningBubbleTime + " and the average is " + runningBubbleTime/iters);
}
public static void selectionSort(int[] array)
{
for (int i = 0; i < array.length - 1; i++)
{
int iMin = i;
for (int j = i + 1; j < array.length; j++)
{
if (array[j] < array[iMin])
{
iMin = j;
}
}
if (iMin != i)
{
int temp = array[i];
array[i] = array[iMin];
array[iMin] = temp;
}
}
}
public static void bubbleSort(int[] arr)
{
int unsorted = arr.length;
while (unsorted != 0)
{
int lastSwap = 0;
for (int i = 1; i < unsorted; i++)
{
if (arr[i - 1] > arr[i])
{
swap(arr, i, i - 1);
lastSwap = i;
}
}
unsorted = lastSwap;
}
}
private static void swap(int[] arr, int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static int randomness()
{
Random rand = new Random();
int random = rand.nextInt();
if (random < 0)
{
random = random * -1;
}
do {
random = random / 10;
} while (random > 100);
return random;
}
}
如果你尝试输入 500 和 1000,运行 冒泡排序的时间会比选择排序短。
有趣的是,如果我去掉变量 "iters," 那么结果就符合预期。但是,iters 似乎应该使 运行 时间 更多 准确,而不是更少。
关于这是为什么的任何想法?我做错什么了吗?冒泡排序是否有可能(始终)优于选择排序?
(为了防止混淆,我看到了问题
你的冒泡排序更快,因为它不起作用。
arr[i] = temp;
应该是
arr[i-1] = temp;
输出为 10000 * 3
选择排序:
总 运行 时间:216077472 平均为 72025824
冒泡排序:
总 运行 时间:402583050 平均为 134194350
输出为 500 * 1000
选择排序:
总 运行 时间:123621068 平均为 123621
冒泡排序:
总 运行 时间:317450183 平均为 317450
使用 使用 jdk 7