Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. positive values count WHILE heap sort

positive values count WHILE heap sort

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmstutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sadas232341s
    wrote on last edited by
    #1

    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++;
    

    }

    A 1 Reply Last reply
    0
    • S sadas232341s

      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++;
      

      }

      A Offline
      A Offline
      Andre Kraak
      wrote on last edited by
      #2

      This question was already posted in Q&A[^]; please post in one location only.

      0100000101101110011001000111001011101001

      S 1 Reply Last reply
      0
      • A Andre Kraak

        This question was already posted in Q&A[^]; please post in one location only.

        0100000101101110011001000111001011101001

        S Offline
        S Offline
        sadas232341s
        wrote on last edited by
        #3

        Well, any suggestions?

        D 1 Reply Last reply
        0
        • S sadas232341s

          Well, any suggestions?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • D David Crow

            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

            S Offline
            S Offline
            sadas232341s
            wrote on last edited by
            #5

            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..

            D 1 Reply Last reply
            0
            • S sadas232341s

              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..

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups