Thread: please help sort array won't work

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    please help sort array won't work

    Hey There. I'm pretty new to C and I was trying to write this sort program. It will display the first line nicely, but it won't display lines pass 1 through pass 7. Can anyone see any errors with anything? should I have the call in the for loop.
    Please help.

    Code:
    #include <stdio.h>
    
    #define SIZE 8
    
    void swap(int*,int*);
    
    void sort(int a[], int n);
    
    
    int main(void)
    { 
           int i,cnt=0,a[SIZE] = {7,3,66,3,-5,22,-77,2};
    
           printf("Unsorted Data:");
           for(i=0; i<SIZE; i++){
                printf("  %i", a[i]);
          }
          putchar('\n');
          sort(a,SIZE);
          for(i=0; i<SIZE; i++){
               ++cnt;
                printf("After Pass:  %d", cnt);
                printf("  %i", a[i]);
           }
           putchar('\n');
           return 0;
    }
    
    
    void sort(int a[], int n)
    {
    
          int i,j;
    
           for(i=0; i<SIZE; ++i)
               for (j = i + 1; j<SIZE; ++j)
    	if (a[i] > a[j])
    	    swap(&a[i], &a[j]);
    	
    	
    }
    
    void swap(int *p, int *q)
    { 
          int tmp;
    	
          tmp = *q;
          *p = *q;
          *q = tmp;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You need braces around those for loops, there are multiple statements that you want to loop.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    around the ones in the sort function or main(void). I'm confused because after it prints out the unsorted data (all the numbers of the array), it will only print out the first number of the array for the remainder of the passes.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, no braces are missing. They just need to indent so you can see it:
    Code:
    void sort(int a[], int n)
    {
        int i,j;
    
        for(i=0; i<SIZE; ++i) /* This one... */
            for (j = i + 1; j<SIZE; ++j) /* grabs this, which... */
                if (a[i] > a[j]) /* grabs this, which... */
                    swap(&a[i], &a[j]); /* grabs this. */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    am I printing out the results of each pass through the sort incorrectly in the main(void)?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suppose that depends on what you mean by "incorrectly". Just copy and paste your above print loop. You aren't printing each pass of the sort. You're printing the sorted version once. Look:
    Code:
    print the array
    sort array
    print the array
    That's what you're doing. Your sort function only runs once, and you're printing the array after you sort it. Not while you are sorting.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    This is what the output looks like as I have it now:
    (I moved the putchar('\n') inside the braces)

    output:

    Code:
    Unsorted Data: 7 3 66 3 -5 22 -77 2
    After pass: 1    -77
    After pass: 2    -77
    After pass: 3    -77
    After pass: 4     -77
    After pass: 5    -77 
    After pass: 6    -77
    After pass: 7    -77
    After pass: 8     2
    I don't understand why it won't print out all the elements of the array.?

  8. #8
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    I just cant seem to figure out a way to print the array as it makes each pass through the sort function. Does anyone have any ideas?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't matter where you move the \n. You're still only displaying it after you call sort. If you want it to print while it sorts, add printing into your sort function.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    old man
    Join Date
    Dec 2005
    Posts
    90
    Like quzah says, you need to put a printf() loop inside the sort function, for example:

    Code:
    #include <stdio.h>
    
    
    void sort (int a[], int n);
    
    int main (void)
    { 
      int a[] = {7, 3, 66, 3, -5, 22, -77, 2};
    
      sort (a, sizeof a / sizeof *a);
      return 0;
    }
    
    
    void sort (int a[], int n)
    {
      int i, j, k, tmp;
    
      for (i = 0; i < n; ++i)
      {
        printf ("Pass %d:", i);
        for (k = 0; k < n; ++k)
          printf ("% 5d", a[k]);
        putchar ('\n');
    
        for (j = i + 1; j < n; ++j)
          if (a[i] > a[j])
          {
            tmp = a[i];  a[i] = a[j];  a[j] = tmp;
          }
      }
    }
    Last edited by eerok; 02-07-2006 at 12:09 AM.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by quzah
    No, no braces are missing. They just need to indent so you can see it:
    Ack, my bad. I didn't read those statements right at all. Looking back I'm not even sure what I read.
    Sent from my iPadŽ

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to drink more. That way you always have an excuse. Either you weren't drinking enough, or you've had too much. See? It's a win-win-win scenario!


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  2. Selection Sort on 2-D character array
    By TankCDR in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2003, 11:59 AM
  3. Recursive Array Sort
    By Nakeerb in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2002, 08:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. the bubble sort doesn't work help
    By Matt in forum C Programming
    Replies: 2
    Last Post: 12-13-2001, 06:14 PM