-
March 16th, 2012, 09:07 AM
#1
need help with priority queue/heapSort
I have been programming in c++ for awhile and now I am learning c# and I am still learning how it works. I have to make a priority queue with heap sort code. I know the code in c++ but I am finding it write to convert it to c#
heres some of the code I am having trouble with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace priority
{
public class HeapSort
{
private int numItems;
private int maxItems;
private ArrayList items;
public HeapSort()
{
int index;
for (index = numItems / 2 - 1; index >= 0; index--)
ReheapDown(items, index, numItems - 1);
}
public bool IsEmpty()
{
return numItems == 0;
}
public bool IsFull()
{
return numItems == maxItems;
}
public void Enqueue(Patient newPatient)
{
items.Add(newPatient);
numItems++;
ReheapUp();
}
public void Dequeue(Patient newPatient)
{
items.Remove(newPatient);
numItems--;
ReheapUp();
}
public void ReheapUp(int root, int bottom)
{
int parent;
if (bottom > root)
{
parent = (bottom - 1) / 2;
if (items[parent] < items[bottom])
{
Swap(items[parent], items[bottom]);
ReheapUp(root, parent);
}
}
}
public void ReheapDown(ArrayList items, int root, int bottom)
{
int maxChild;
int rightChild;
int leftChild;
leftChild = root * 2 + 1;
rightChild = root * 2 + 2;
if (leftChild <= bottom)
{
maxChild = leftChild;
}
else
{
if(items[leftChild] <= items[rightChild])
{
maxChild = rightChild;
}
else
{
maxChild = leftChild;
}
if(items[root] < items[maxChild])
{
Swap(items[root], items[maxChild]);
ReheapDown(items, maxChild, bottom);
}
}
}
public void Swap()
{
}
}
}
I have red underline on the if statements. Could someone please help?
-
March 16th, 2012, 12:20 PM
#2
Re: need help with priority queue/heapSort
Please enclose your code in [ code ] [/ code ] brackets- it improves readability. It's too hard to try to read large code sections without it.
Code:
if (Issue.Resolved)
{
ThreadTools.Click();
MarkThreadResolved();
}
-
March 16th, 2012, 01:33 PM
#3
Re: need help with priority queue/heapSort
Ok here it is in tags sorry about that
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace priority
{
public class HeapSort
{
private int numItems;
private int maxItems;
private ArrayList items;
public HeapSort()
{
int index;
for (index = numItems / 2 - 1; index >= 0; index--)
ReheapDown(items, index, numItems - 1);
}
public bool IsEmpty()
{
return numItems == 0;
}
public bool IsFull()
{
return numItems == maxItems;
}
public void Enqueue(Patient newPatient)
{
items.Add(newPatient);
numItems++;
ReheapUp();
}
public void Dequeue(Patient newPatient)
{
items.Remove(newPatient);
numItems--;
ReheapUp();
}
public void ReheapUp(int root, int bottom)
{
int parent;
if (bottom > root)
{
parent = (bottom - 1) / 2;
if (items[parent] < items[bottom])
{
Swap(items[parent], items[bottom]);
ReheapUp(root, parent);
}
}
}
public void ReheapDown(ArrayList items, int root, int bottom)
{
int maxChild;
int rightChild;
int leftChild;
leftChild = root * 2 + 1;
rightChild = root * 2 + 2;
if (leftChild <= bottom)
{
maxChild = leftChild;
}
else
{
if(items[leftChild] <= items[rightChild])
{
maxChild = rightChild;
}
else
{
maxChild = leftChild;
}
if(items[root] < items[maxChild])
{
Swap(items[root], items[maxChild]);
ReheapDown(items, maxChild, bottom);
}
}
}
public void Swap()
{
}
}
}
Last edited by beginner91; March 16th, 2012 at 02:27 PM.
-
March 18th, 2012, 04:22 PM
#4
Re: need help with priority queue/heapSort
-
March 19th, 2012, 06:13 PM
#5
Re: need help with priority queue/heapSort
It's hard to read code that is not indented. Also, you've not actually told us what your problems is.
People here will help you, but you need to be more specific. You can't just say "My program doesn't work". You need to give an example, like... I entered this data and... this was the output.... but I expected this.... etc.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|