我正在编写一个程序来计算每种排序方法中的操作。 我在这里算的是数组中2个值之间的比较。那么有人可以检查我是否正确地计算了[] ++?
排序方法:
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.
答案 0 :(得分:2)
由于您的问题没有标记为家庭作业,我假设您主要想知道在排序过程中进行了多少次比较。为此,我会