Java 在选择排序、插入排序、合并排序和快速排序中计算比较?
我正在编写一个程序,计算每个排序方法中的操作数。 这里我计算的是数组中两个值之间的比较。有人能检查一下我的计数[]++是否正确吗 排序方法:Java 在选择排序、插入排序、合并排序和快速排序中计算比较?,java,sorting,data-structures,Java,Sorting,Data Structures,我正在编写一个程序,计算每个排序方法中的操作数。 这里我计算的是数组中两个值之间的比较。有人能检查一下我的计数[]++是否正确吗 排序方法: public void selectionSort(int[]数据){ int mindex, tmp; 对于(intx=0;xtmp;y--,count[1]+){//count[1]统计insertionSort方法的操作。 数据[y]=数据[y-1]; }//结束于(y=x;…)。 数据[y]=tmp; }//结束于(x=1;…)。 }//结束ins
public void selectionSort(int[]数据){
int mindex,
tmp;
对于(intx=0;xtmp;y--,count[1]+){//count[1]统计insertionSort方法的操作。
数据[y]=数据[y-1];
}//结束于(y=x;…)。
数据[y]=tmp;
}//结束于(x=1;…)。
}//结束insertionSort方法。
/*
*mergeSort方法,该方法使用mergeSort算法对数组进行排序。
*/
公共void合并排序(int[]数据,int-first,int-n){
int-n1;
int n2;
如果(n>1){
n1=n/2;
n2=n-n1;
合并排序(数据,第一,n1);
合并排序(数据,第一个+n1,n2);
合并(数据,第一,n1,n2);
}
}//结束合并排序方法。
/*
*merge方法,该方法由mergeSort方法调用,以对数组进行排序并对操作进行计数。
*/
公共无效合并(int[]数据,int-first,int-n1,int-n2){
int[]温度=新的int[n1+n2];
int copied=0,copied1=0,copied2=0,i;
虽然((copied1因为你的问题没有标记为家庭作业,我假设你主要想知道在排序过程中进行了多少比较。对于这一点,我会
- 不是重新发明排序,而是使用Java库中的高效排序
- 使用自己的比较器装饰现有的compare()或compareTo()来计算比较次数
这本质上是一个代码审查请求。更合适的请求是我真的很喜欢-compareTo()的装饰。
idea.+1
public void selectionSort (int [] data) {
int mindex,
tmp;
for (int x = 0; x <= data.length-2; x++) {
mindex = x;
for (int y = x+1; y <= data.length-1; y++) {
if (data [y] < data[mindex]){
mindex = y;
}
count[0]++;// counts the operations for selectionSort method.
}//ends for (int y=x+1;...).
tmp = data[x];
data[x] = data[mindex];
data[mindex] = tmp;
}//ends for (int x = 0;...).
}//ends selectionSort method.
/*
* insertionSort method that sorts the array by using the insertionsort algorithm and counts the operations.
*/
public void insertionSort(int [] data){
int x,
y,
tmp;
for (x = 1; x < data.length; x++){
tmp = data[x];
for (y = x; y > 0 && data[y-1] > tmp; y--,count[1]++){ // count[1] counts the operations for insertionSort method.
data[y] = data[y-1];
}//ends for (y=x;...).
data[y] = tmp;
}//ends for(x=1;...).
}//ends insertionSort method.
/*
* mergeSort method that sorts the array by using the mergesort algorithm.
*/
public void mergeSort(int [] data, int first, int n){
int n1;
int n2;
if (n>1) {
n1 = n/2;
n2 = n-n1;
mergeSort(data,first,n1);
mergeSort(data,first+n1,n2);
merge(data,first,n1,n2);
}
}//ends mergeSort method.
/*
* merge method that is called by mergeSort method to sort the array and counts the operations.
*/
public void merge(int [] data, int first, int n1, int n2){
int [] temp = new int[n1+n2];
int copied = 0, copied1 = 0, copied2 = 0, i;
while((copied1<n1) && (copied2<n2)){
if(data[first+copied1] < data[first + n1 + copied2]){
temp[copied++] = data[first + (copied1++)];
count[2]++;//counts the operations for mergeSort method.
} else {
temp[copied++] = data[first + n1 + (copied2++)];
count[2]++; // counts the operations for mergeSort method.
}
}// ends while.
while (copied1<n1)
temp[copied++] = data[first + (copied1++)];
while(copied2<n2)
temp[copied++] = data[first + n1 + (copied2++)];
for (i=0;i<n1+n2;i++)
data[first+i] = temp[i];
}//ends merge method.
/*
* quickSort method that sorts the array by using the quicksort algorithm.
*/
public void quickSort(int data[], int left, int right)
{
int index = partition(data, left, right);// Creates an integer variable (index) and calls partition method to find the index value.
if (left < index - 1){
quickSort(data, left, index - 1);
}
if (index < right){
quickSort(data, index, right);
}
}//ends quickSort method.
/*
* partition method that is called by quickSort to sort the array and counts the operations.
*/
int partition(int data[], int left, int right){
int i = left, j = right;
int tmp;
int pivot = data[(left + right) / 2];
while (i <= j)
{
while (data[i] < pivot) {
i++;
count[3]++;// counts the operations for quickSort method.
}// ends while loop.
while (data[j] > pivot) {
j--;
count[3]++;// counts the operations for quickSort method.
}// ends while loop.
if (i <= j)
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
i++;
j--;
}// ends if.
}// ends while loop.
return i;// returns i value to the method.
}//ends partition method.