positive values count WHILE heap sort
-
How to count positive values WHILE doing HeapSort? Here is my HeapSort and I count them in a loop at the end of the method, but it needs to be done while sorting. How's that?
void Sift(int arr[], int left, int right)
{
int i, j, x;i = left; j = 2 \* i + 1; x = arr\[i\]; while (j <= right) { if(j < right) if(arr\[j\] < arr\[j + 1\]) j++; if (x >= arr\[j\]) break; arr\[i\] = arr\[j\]; i = j; j = 2 \* i + 1; } arr\[i\] = x;
}
void HeapSort(int arr[], int n)
{
int left, right, temp;left = n / 2 + 1; right = n - 1; while (0 < left) { left--; Sift(arr, left, right); } while (0 < right) { temp = arr\[left\]; arr\[left\] = arr\[right\]; arr\[right\] = temp; right--; Sift(arr, left, right); } // Here I count for(int i = 0; i < n; i++) if(0 < arr\[i\]) PosCount++;
}
-
How to count positive values WHILE doing HeapSort? Here is my HeapSort and I count them in a loop at the end of the method, but it needs to be done while sorting. How's that?
void Sift(int arr[], int left, int right)
{
int i, j, x;i = left; j = 2 \* i + 1; x = arr\[i\]; while (j <= right) { if(j < right) if(arr\[j\] < arr\[j + 1\]) j++; if (x >= arr\[j\]) break; arr\[i\] = arr\[j\]; i = j; j = 2 \* i + 1; } arr\[i\] = x;
}
void HeapSort(int arr[], int n)
{
int left, right, temp;left = n / 2 + 1; right = n - 1; while (0 < left) { left--; Sift(arr, left, right); } while (0 < right) { temp = arr\[left\]; arr\[left\] = arr\[right\]; arr\[right\] = temp; right--; Sift(arr, left, right); } // Here I count for(int i = 0; i < n; i++) if(0 < arr\[i\]) PosCount++;
}
-
Well, any suggestions?
-
Well, any suggestions?
Have you tried placing the counting code within either of those other loops to see if it counts what you expect? At first glance, I'd try:
while (0 < right)
{
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;**if (0 < arr\[right\]) PosCount++;** right--; Sift(arr, left, right);
}
// since the while() loop stops when right==0, you'll need to count one more time here
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Have you tried placing the counting code within either of those other loops to see if it counts what you expect? At first glance, I'd try:
while (0 < right)
{
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;**if (0 < arr\[right\]) PosCount++;** right--; Sift(arr, left, right);
}
// since the while() loop stops when right==0, you'll need to count one more time here
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
Yes, the problem is that I don't understand the HeapSort good enough to reshape it whatever I want. That's why i need help..
-
Yes, the problem is that I don't understand the HeapSort good enough to reshape it whatever I want. That's why i need help..
Code aside, what part of the heap sort do you need help with: building the heap, or adding to the sorted array? I would forgo coding until I could do it all on paper first.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous