Reputation: 29
I am writing a program that counts the operations of mergesort and quicksort. I don't know where I should put the count++; (to count the operations) in. Can someone help me with this?
Here are the codes for merge sort and quick sort.
MERGE SORT:
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.
And Here is the code for QUICK SORT:
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.
Upvotes: 2
Views: 3682
Reputation: 216
Assuming that you're doing "big O" analysis, your "count of operations" is the number of iterations of the innermost loop.
while (i <= j)
while
loops (I haven't looked closely at these loops, but they seem to be doing far more than is necessary).This count does not tell you the actual number of operations that you're performing, just the "big O" number of operations. While MergeSort and QuickSort may have the same "big O" characteristics (and therefore should have similar counts), the amount of work done inside the innermost loop will differ.
Upvotes: 1
Reputation: 533710
You should put the ++
whereever you have an "operation". What you call an operation is up to you. I could be a comparison, a swap or every line. You have the decide what is appropriate for you.
Upvotes: 4
Reputation: 213351
In merge sort, below method is exactly the one doing the stuff: -
merge(data,first,n1,n2);
So, put one count++ there..
And in quick sort.. it's : -
partition(data, left, right)
So, add count++ there..
Your other method calls: - mergesort()
and quicksort()
are just recursive calls.. They will finally lead to the above two methods
only..
But ultimately it all depends upon what you mean my 'count the operations'.. What you take as an operation
to be is crucial..
It might be a method execution
, or every statement
can be an operation.. Or your loop.. It can mean anything..
Upvotes: 1