具有2,000个数组元素的插入排序的最坏情况是4,000,000个交换,最好的情况是0个交换。为什么我的随机排序数组的平均情况大约有4,000个交换?它不应该在0到4,000,000之间波动吗?
我正在使用各种排序函数记录算法的比较数和交换次数。
import java.io.*;
import java.util.*;
public class SortAlgorithm
{
public static void main(String[] args) throws IOException
{
int sort1[] = new int[2000];//sorted
int sort2[] = new int[2000];//reverse sorted
int sort3[] = new int[2000];//random sorted
Random rand = new Random();
String str;
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\Colin\\Desktop\\Test.txt"));
for(int i = 0; i < sort1.length; i++) //sorted
{
sort1[i] = i;
}
str = "Bubble Sort in sorted order: ";
str += bubbleSort(sort1);
writer.write(str);
writer.newLine();
for(int i = 0; i < sort1.length; i++) //sorted
{
sort1[i] = i;
}
str = "Selection Sort in sorted order: ";
str += selectionSort(sort1);
writer.write(str);
writer.newLine();
for(int i = 0; i < sort1.length; i++) //sorted
{
sort1[i] = i;
}
str = "Insertion Sort in sorted order: ";
str += insertionSort(sort1);
writer.write(str);
writer.newLine();
writer.newLine();
for(int i = 0, j = sort2.length - 1; i < sort2.length; i++, j--)//reverse sorted
{
sort2[i] = j;
}
str = "Bubble Sort in reverse sorted order: ";
str += bubbleSort(sort2);
writer.write(str);
writer.newLine();
for(int i = 0, j = sort2.length - 1; i < sort2.length; i++, j--)//reverse sorted
{
sort2[i] = j;
}
str = "Selection Sort in reverse sorted order: ";
str += selectionSort(sort2);
writer.write(str);
writer.newLine();
for(int i = 0, j = sort2.length - 1; i < sort2.length; i++, j--)//reverse sorted
{
sort2[i] = j;
}
str = "Insertion Sort in reverse sorted order: ";
str += insertionSort(sort2);
writer.write(str);
writer.newLine();
writer.newLine();
for(int i = 0; i < sort3.length; i++)//random sorted
{
sort3[i] = rand.nextInt(2000) + 1;
}
str = "Bubble Sort in random sorted order: ";
str += bubbleSort(sort3);
writer.write(str);
writer.newLine();
for(int i = 0; i < sort3.length; i++)//random sorted
{
sort3[i] = rand.nextInt(2000) + 1;
}
str = "Selection Sort in random sorted order: ";
str += selectionSort(sort3);
writer.write(str);
writer.newLine();
for(int i = 0; i < sort3.length; i++)//random sorted
{
sort3[i] = rand.nextInt(2000) + 1;
}
str = "Insertion Sort in random sorted order: ";
str += insertionSort(sort3);
writer.write(str);
writer.newLine();
writer.close();
}
static String selectionSort(int sort[])
{
int comparisons = 0, exchanges = 0;
String str;
int n = sort.length;
for(int i = 0; i < n; i++)
{
int small = sort[i];
int pos = i;
comparisons++;
for(int j = i + 1; j < n; j++)
{
comparisons++;
if(sort[j] < small)
{
small = sort[j];
pos = j;
exchanges++;
}
comparisons++;
}
int temp = sort[pos];
sort[pos] = sort[i];
sort[i] = temp;
}
str = "Number of comparisons: " + (comparisons) + " , number of exchanges: " + (exchanges);
return str;
}
static String insertionSort(int sort[])
{
int comparisons = 0, exchanges = 0;
String str = "";
int n = sort.length;
for(int i = 1; i < n; i++)
{
int j = i - 1;
int temp = sort[i];
comparisons++;
while(j >= 0 && temp < sort[j])
{
sort[j + 1] = sort[j];
j--;
exchanges++;
}
sort[j + 1] = temp;
comparisons++;
}
str = "Number of comparisons: " + (comparisons) + " , number of exchanges: " + (exchanges);
return str;
}
static String bubbleSort(int sort[])
{
String str;
int comparisons = 0, exchanges = 0;
int n = sort.length;
for (int i = 0; i < n; i++)
{
comparisons++;
for (int j = 0; j < n - i - 1; j++)
{
comparisons++;
if (sort[j] > sort[j + 1])
{
int temp = sort[j];
sort[j] = sort[j + 1];
sort[j + 1] = temp;
exchanges++;
}
comparisons++;
}
}
str = "Number of comparisons: " + (comparisons) + " , number of exchanges: " + (exchanges);
return str;
}
}
这是输出:
Bubble Sort in sorted order: Number of comparisons: 4000000 , number of exchanges: 0
Selection Sort in sorted order: Number of comparisons: 4000000 , number of exchanges: 0
Insertion Sort in sorted order: Number of comparisons: 3998 , number of exchanges: 0
Bubble Sort in reverse sorted order: Number of comparisons: 4000000 , number of exchanges: 1999000
Selection Sort in reverse sorted order: Number of comparisons: 4000000 , number of exchanges: 1000000
Insertion Sort in reverse sorted order: Number of comparisons: 3998 , number of exchanges: 1999000
Bubble Sort in random sorted order: Number of comparisons: 4000000 , number of exchanges: 1005056
Selection Sort in random sorted order: Number of comparisons: 4000000 , number of exchanges: 11590
Insertion Sort in random sorted order: Number of comparisons: 3998 , number of exchanges: 970272