我正在编写一个计算合并排序和快速排序操作的程序。我不知道我应该把count++放在哪里;(计算操作)。有人可以帮我吗?
以下是合并排序和快速排序的代码。
合并排序:
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.
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++)];
else
temp[copied++] = data[first + n1 + (copied2++)];
}
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.
这是快速排序的代码:
public void quickSort(int data[], int left, int right){
int index = partition(data, left, right);
if (left < index - 1){
quickSort(data, left, index - 1);
}
if (index < right){
quickSort(data, index, right);
}
}//ends quickSort method.
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++;
while (data[j] > pivot)
j--;
if (i <= j)
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
i++;
j--;
}
}
return i;
}//ends partition method.