Closed Thread
Results 1 to 4 of 4

Thread: Help regarding the sorting of arrays with flags!

  1. #1
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0

    Angry Help regarding the sorting of arrays with flags!

    Hey everyone. I've been working on a C problem that involved sorting large arrays through the use of flags that indicate whether the array has been sorted or not.

    Here's the original question:

    The bubble sort of dem11-7.c is inefficient for sorting large arrays because it does not recognize when the array is sorted. The program makes "size1" passes on an array with "size" elements even if the array is already sorted. The program will be more efficient if when making a pass over the array, it recognizes if the array is sorted and stops. Recode dem11-7.c to do this.

    Here's a hint that the question gave me:

    Use a flag called "sorted". Set "sorted" equal to zero immediately before making a pass over the array. If the flag equals 1, the array is sorted. In this case, exit the loop. If the flag equals zero, the array is not sorted. In this case, the program must make a pass over the array. In the inner loop of the nested "for" loop, when the function swaps two array elements, set the flag to 1.

    Here's dem11-7.c in all of its unaltered glory:
    Code:
    /* dem11-7.c */
    
    /*
    * This program shows how to sort the elements
    * in an array.
    */
    
    #include <stdio.h>
    
    #define NUM_QUIZZES 10
    
    main()
    {
          /* The array to store the quiz grades */
    
         int grade[NUM_QUIZZES];
    
         int quiz,                         /* Subscript for the array grade[] */
              temp,                       /* For swapping array elements */
              pass,                        /* The number of the pass */
              limit;                        /* Keeps track of how far to go on a pass */
    
         /* Get the grades */
    
         printf("\nPlease enter %d integer quiz grades.\n\n", NUM_QUIZZES);
    
         for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
         {
           printf("\nEnter grade for quiz %d: ", quiz + 1);
           scanf("%d", &grade[quiz]);
         }
    
         /* Display the quiz grades */
    
         printf("\n\nThe grades you entered are as follows:\n");
         for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
          printf("%6d", grade[quiz]);
         printf("\n");
    
         /* Do the bubble sort */
    
         limit = NUM_QUIZZES - 2;
    
         for (pass = 1; pass <= NUM_QUIZZES - 1; ++pass)
         {
           for (quiz = 0; quiz <= limit; ++quiz)
            if (grade[quiz] > grade[quiz + 1])
            {
               temp = grade[quiz];
               grade[quiz] = grade[quiz + 1];
               grade[quiz + 1] = temp;
            }
          --limit;
          }
    
          /* Display the sorted quiz grades */
    
          printf("\n\nThe grades in increasing order are as follows:\n");
    
          for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
           printf("%6d", grade[quiz]);
          printf("\n");
    }
    Now here's the modified version with the "sorted" flag that I put in:
    Code:
    /* Prob 11-10
    *  Created by Yuriy Mokriy
    */
    
    #include <stdio.h>
    
    #define NUM_QUIZZES 10                  /* Total number of quiz grades in the array */
    
    main()
    {
          int grade[NUM_QUIZZES],           /* The array to store the quiz grades */
              temp,                         /* For swapping array elements */
              quiz,                         /* Subscript for the array grade[] */
              pass,                         /* The number of the pass */
              limit,                        /* Keeps track of how far to go on a pass */
              sorted = 1;                   /* A flag that indicates when the array is sorted */
          
          /* Get the grades */
          
          printf("\nPlease enter %d integer quiz grades. \n\n", NUM_QUIZZES);
              
          for (quiz = 0; quiz < NUM_QUIZZES; quiz++)
          {
           printf("\nEnter grade for quiz %d: ", quiz + 1);
           scanf("%d", &grade[quiz]);
          }
           /* Check if grade entered is greater or smaller than the one previous */
          
           if (grade[quiz] > grade[quiz + 1])
            sorted = 0;
           else
            sorted = 1;     
    
          /* Display the quiz grades */
          
          printf("\n\nThe grades you entered are as follows:\n");
          for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
            printf("%6d", grade[quiz]);
          printf("\n");
            
           /* Do the bubble sort if sorted is equal to 0 */
          
           limit = NUM_QUIZZES - 2;
          
           if (sorted != 1)
           {
            for (pass = 1; pass <= NUM_QUIZZES - 1; ++pass)
            {
             for (quiz = 0; quiz <= limit; ++quiz)
              if (grade[quiz] > grade[quiz + 1])
              {
                temp = grade[quiz];
                grade[quiz] = grade[quiz + 1];
                grade[quiz + 1] = temp;
                sorted = 1;
              } 
              --limit;   
            }
           } 
          
          /* Display the sorted quiz grades */
          
          printf("\n\nThe grades in increasing order are as follows:\n");
          
          for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
           printf("%6d", grade[quiz]);
          printf("\n");
    }
    What I'm basically trying to do here is to have each grade read in individually and compared to see if they are already in sorted order. If they are sorted, the flag sorted is set to 1. If not, it is set to 0, the bubble sort is executed and then the flag is set to 1.

    The problem I'm having with the code is that it doesn't seem to recognize the flag and just gives me the same disorganized output. What am I not doing right?
    For $1000: Something that is a miserable pile of secrets.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    felixme86's Avatar
    felixme86 is offline Newbie
    Join Date
    Sep 2007
    Location
    Seattle WA
    Posts
    11
    Rep Power
    0
    Code:
    // Start out by saying that the array is not sorted
    // because we have not gone through the array
    bool sorted=false;
            for (pass = 1; pass <= NUM_QUIZZES - 1&&!sorted; ++pass)
            {
                 // Set sorted to true.  now we will go though the list once
                 // if we find any variable out of place then we set sorted
                 // to false.  If we have made it through without moving
                 // any variables there is no reason to repeat the outer
                 // loop because we will not move any variables on
                 // subsequent checks.
                 sorted=true;
                 for (quiz = 0; quiz <= limit; ++quiz)
                 if (grade[quiz] > grade[quiz + 1])
                 {
                     temp = grade[quiz];
                     grade[quiz] = grade[quiz + 1];
                     grade[quiz + 1] = temp;
                     sorted = false;
                 } 
                 --limit;   
            }
    Hopefully this is what you are looking for.
    Last edited by WingedPanther; 09-27-2007 at 08:51 AM. Reason: Add code tags

  4. #3
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    felixme86, here on CodeCall (and on many other forums) we've a so-called code-tags. It's easier for us to read your code, if you use them. They work in this way:
    Code:
    &#91;code]
    This is where you put your code.
    &#91;/code]

  5. #4
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0
    Code:
    /* Prob 11-10
    *  Created by Yuriy Mokriy
    */
    
    #include <stdio.h>
    
    #define NUM_QUIZZES 10                  /* Total number of quiz grades in the array */
    
    main()
    {
          int grade[NUM_QUIZZES],           /* The array to store the quiz grades */
              temp,                         /* For swapping array elements */
              quiz,                         /* Subscript for the array grade[] */
              pass,                         /* The number of the pass */
              limit,                        /* Keeps track of how far to go on a pass */
              sorted = 1;                   /* A flag that indicates when the array is sorted */
          
          /* Get the grades */
          
          printf("\nPlease enter %d integer quiz grades. \n\n", NUM_QUIZZES);
              
          for (quiz = 0; quiz < NUM_QUIZZES; quiz++)
          {
           printf("\nEnter grade for quiz %d: ", quiz + 1);
           scanf("%d", &grade[quiz]);
          }
           /* Check if grade entered is greater or smaller than the one previous */
          
          if (quiz > 0)
           {
             if (grade[quiz] < grade[quiz - 1])
               sorted = 0;
             else
               sorted = 1;
           }  
    
          /* Display the quiz grades */
          
          printf("\n\nThe grades you entered are as follows:\n");
          for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
            printf("%6d", grade[quiz]);
          printf("\n");
            
           /* Do the bubble sort if sorted is equal to 0 */
          
           limit = NUM_QUIZZES - 2;
          
           if (sorted != 1)
           {
            for (pass = 1; pass <= NUM_QUIZZES - 1; ++pass)
            {
             for (quiz = 0; quiz <= limit; ++quiz)
              if (grade[quiz] > grade[quiz + 1])
              {
                temp = grade[quiz];
                grade[quiz] = grade[quiz + 1];
                grade[quiz + 1] = temp;
                sorted = 1;
              } 
              --limit;   
            }
           } 
          
          /* Display the sorted quiz grades */
          
          printf("\n\nThe grades in increasing order are as follows:\n");
          
          for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
           printf("%6d", grade[quiz]);
          printf("\n");
    }
    Got the problem fixed. All I needed to do was use an if statement to check if the quiz grade entered is greater than 0 and whether the current quiz grade was less than the one before it. After that, the program sorted the grades smoothly.
    For $1000: Something that is a miserable pile of secrets.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate New Sorting - with order arrays
    By fayyazlodhi in forum C Tutorials
    Replies: 2
    Last Post: 11-10-2011, 12:44 AM
  2. Sorting values from arrays
    By An Alien in forum Java Help
    Replies: 22
    Last Post: 02-21-2011, 10:02 AM
  3. Sorting Arrays
    By chili5 in forum Java Tutorials
    Replies: 11
    Last Post: 02-07-2010, 12:45 PM
  4. Need help sorting 2 arrays.
    By posto2012 in forum C and C++
    Replies: 3
    Last Post: 10-11-2009, 04:46 PM
  5. Sorting Arrays
    By falco85 in forum PHP Development
    Replies: 5
    Last Post: 08-23-2006, 07:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts