Java 学习排序需要一些帮助吗?
我找到了下面的代码。我试图了解哪种排序方法最快,哪种方法使用最多和最少的比较。有人知道我如何在这里添加一些代码来做到这一点吗?我想统计每种比较的总数Java 学习排序需要一些帮助吗?,java,sorting,Java,Sorting,我找到了下面的代码。我试图了解哪种排序方法最快,哪种方法使用最多和最少的比较。有人知道我如何在这里添加一些代码来做到这一点吗?我想统计每种比较的总数 //*********************************************************************************** // Sorting.java // // Contains various sort algorithms that operate on an array of comp
//***********************************************************************************
// Sorting.java
//
// Contains various sort algorithms that operate on an array of comparable objects.
//
//************************************************************************************
public class Sorting
{
//------------------------------------------------------------------------------------
// Sorts the specified array of integers using the selection sort algorithm.
//------------------------------------------------------------------------------------
public static void selectionSort (Comparable[] data)
{
int min;
for (int index = 0; index < data.length-1; index ++)
{
min = index;
for (int scan = index+1; scan < data.length; scan++)
if (data[scan].compareTo(data[min]) < 0)
min = scan;
swap (data, min, index);
}
}
//---------------------------------------------------------------------------------------
// Swaps to elements in the specified array.
//---------------------------------------------------------------------------------------
private static void swap (Comparable[] data, int index1, int index2)
{
Comparable temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
//---------------------------------------------------------------------------------------
// Sorts the specified array of objects using an insertion sort algorithm.
//---------------------------------------------------------------------------------------
public static void insertionSort (Comparable[] data)
{
for (int index = 1; index < data.length; index++)
{
Comparable key = data[index];
int position = index;
// shift larger values to the right
while (position > 0 && data[position-1].compareTo(key) > 0)
{
data[position] = data[position-1];
position--;
}
data[position] = key;
}
}
//---------------------------------------------------------------------------------------
// Sorts the specified array of objects using a bubble sort algorithm.
//---------------------------------------------------------------------------------------
public static void bubbleSort (Comparable[] data)
{
int position, scan;
for (position = data.length - 1; position >= 0; position--)
{
for (scan = 0; scan <= position - 1; scan ++)
if (data[scan].compareTo(data[scan+1]) >0)
swap (data, scan, scan+1);
}
}
//---------------------------------------------------------------------------------------
// Sorts the specified array of objects using the quick sort algorithm.
//---------------------------------------------------------------------------------------
public static void quickSort (Comparable[] data, int min, int max)
{
int pivot;
if (min < max)
{
pivot = partition (data, min, max); // make partitions
quickSort(data, min, pivot-1); //sort left paritions
quickSort(data, pivot+1, max); //sort right paritions
}
}
//---------------------------------------------------------------------------------------
// Creates the partitions needed for quick sort.
//---------------------------------------------------------------------------------------
public static int partition (Comparable[] data, int min, int max)
{
//Use first element as the partition value
Comparable partitionValue = data[min];
int left = min;
int right = max;
while (left < right)
{
// Search for an element that is greater than the partition element
while (data[left].compareTo(partitionValue) <= 0 && left < right)
left++;
// Search for an element that is less than the partition element
while (data[right].compareTo(partitionValue) > 0)
right--;
if (left < right)
swap (data, left, right);
}
// Move the partition element to its final position
swap (data, min, right);
return right;
}
//---------------------------------------------------------------------------------------
// Sorts the specified array of objects using the merge sort algorithm.
//---------------------------------------------------------------------------------------
public static void mergeSort (Comparable[] data, int min, int max)
{
if (min < max)
{
int mid = (min + max) / 2;
mergeSort(data, min, mid);
mergeSort(data, mid+1, max);
merge (data, min, mid, max);
}
}
//---------------------------------------------------------------------------------------
// Sorts the specified array of objects using the merge sort algorithm.
//---------------------------------------------------------------------------------------
public static void merge (Comparable[] data, int first, int mid, int last)
{
Comparable[] temp = new Comparable[data.length];
int first1 = first, last1 = mid; //endpoints of first subarray
int first2 = mid + 1, last2 = last; //endpoints of second subarray
int index = first1; // next index open in temp array
// Copy smaller item from each subarry into temp until one of the subarrays is exhausted
while (first1 <= last1 && first2 <= last2)
{
if (data[first1].compareTo(data[first2]) < 0)
{
temp[index] = data[first1];
first1++;
}
else
{
temp[index] = data[first2];
first2++;
}
index++;
}
// Copy remaining elements from first subarray, if any
while (first1 <= last1)
{
temp[index] = data[first1];
first1++;
index++;
}
// Copy remaining elements from second subarray, if any
while (first2 <= last2)
{
temp[index] = data[first2];
first2++;
index++;
}
// Copy merged data into original array
for (index = first; index <= last; index++)
data[index] = temp[index];
}
}
//***********************************************************************************
//Sorting.java
//
//包含对可比较对象数组进行操作的各种排序算法。
//
//************************************************************************************
公共类排序
{
//------------------------------------------------------------------------------------
//使用选择排序算法对指定的整数数组进行排序。
//------------------------------------------------------------------------------------
公共静态无效选择排序(可比[]数据)
{
int-min;
对于(int index=0;index0&&数据[position-1]。比较(键)>0)
{
数据[位置]=数据[位置-1];
位置--;
}
数据[位置]=键;
}
}
//---------------------------------------------------------------------------------------
//使用气泡排序算法对指定的对象数组进行排序。
//---------------------------------------------------------------------------------------
公共静态void bubbleSort(可比[]数据)
{
int位置,扫描;
对于(位置=data.length-1;位置>=0;位置--)
{
用于(扫描=0;扫描0)
交换(数据、扫描、扫描+1);
}
}
//---------------------------------------------------------------------------------------
//使用快速排序算法对指定的对象数组进行排序。
//---------------------------------------------------------------------------------------
公共静态void快速排序(可比较[]数据,最小整数,最大整数)
{
int轴;
如果(最小值<最大值)
{
pivot=分区(数据、最小值、最大值);//创建分区
快速排序(数据、最小值、pivot-1);//对左分区进行排序
快速排序(数据,pivot+1,max);//对右分区进行排序
}
}
//---------------------------------------------------------------------------------------
//创建快速排序所需的分区。
//---------------------------------------------------------------------------------------
公共静态整型分区(可比较[]数据,最小整型,最大整型)
{
//使用第一个元素作为分区值
可比分区值=数据[min];
int左=分钟;
int right=max;
while(左<右)
{
//搜索大于分区元素的元素
while(数据[左]。比较到(分区值)0)
对--;
if(左<右)
交换(数据,左,右);
}
//将分区元素移动到其最终位置
交换(数据,最小值,右侧);
返还权;
}
//---------------------------------------------------------------------------------------
//使用合并排序算法对指定的对象数组进行排序。
//---------------------------------------------------------------------------------------
公共静态无效合并排序(可比较[]数据,最小整数,最大整数)
{
如果(最小值<最大值)
{
int mid=(最小+最大)/2;
合并排序(数据、最小值、中间值);
合并排序(数据,mid+1,最大值);
合并(数据、最小值、中间值、最大值);
}
}
//---------------------------------------------------------------------------------------
//使用合并排序算法对指定的对象数组进行排序。
//---------------------------------------------------------------------------------------
公共静态无效合并(可比较[]数据,int-first,int-mid,int-last)
{
可比[]温度=新的可比[数据长度];
int first1=first,last1=mid;//第一个子数组的端点
int first2=mid+1,last2=last;//第二个子数组的端点
int index=first1;//在临时数组中打开下一个索引
//将每个子阵列中较小的项目复制到temp中,直到其中一个子阵列耗尽
如果你对速度感兴趣,请使用。如果你在学术上对各种排序技术所涉及的内容感兴趣,那么使用速度可能更快。如果你想让我们为你做作业……我们不会,对不起
编辑:我想我可以这么说:有什么原因不能在每个方法开始时将整数初始化为0,每次发生有趣的事情时递增,然后在最后打印出来?不如从一本好书中学习O表示法,比如:
您应该定义一个实现计数的抽象类,然后是一些实现算法的派生类。主程序如下所示:
List<String> list = new ArrayList<String>();
// TODO: add some elements to the list
SortingAlgorithm alg = new BubbleSort();
alg.sort(list);
alg.printSummary();
List List=new ArrayList();
//TODO:向列表中添加一些元素
排序算法alg=新的BubbleSort();
alg.排序(列表);
alg.printSummary();
public abstract class SortingAlgorithm<T> {
/* the actual algorithm, which uses the compare and swap methods. */
public abstract void sort(List<T> list);
private long compares = 0;
private long swaps = 0;
protected int compare(T a, T b) {
compares++;
return a.compareTo(b);
}
protected void swap(int index1, int index2) {
swaps++;
// TODO: do the actual swapping
}
public void printSummary() {
// TODO
}
}