Like Tree3Likes

Need help to display the process of partitioning and sorting of quick sorting

This is a discussion on Need help to display the process of partitioning and sorting of quick sorting within the C Programming forums, part of the General Programming Boards category; i still get errors: line 54 printIt undeclared, line 67 void printIt used prior to declaration, line 69 expected ';' ...

  1. #16
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    i still get errors: line 54 printIt undeclared, line 67 void printIt used prior to declaration, line 69 expected ';' before ')' token


    Code:
    #include <stdio.h>
    #include <conio.h>  
     #define cutoff 3 
    #define N 12 
     void swap(int *a,int *b)  
     {  
        int temp=*a;  
        *a=*b;  
        *b=temp;  
     }  
     int median(int a[],int left,int right)  
     {  
       int center=(left+right)/2;  
       if(a[left]>a[center])  
       swap(&a[left],&a[center]);  
       if(a[left]>a[right])  
       swap(&a[left],&a[right]);  
       if(a[right]<a[center])  
       swap(&a[right],&a[center]);  
       swap(&a[center],&a[right-1]);  
       return a[right-1]; 
     }  
     void insertionsort(int A[],int n)  
     {  
     int j,p,temp;  
     for(p=1;p<n;p++)  
     {  
     temp=A[p];  
     for(j=p;j>0&&A[j-1]>temp;j--)  
     A[j]=A[j-1];  
     A[j]=temp;
     }  
     }  
     void qsort(int a[],int left,int right)  
     {    
        int i,j,pivot;  
        if(left+cutoff<=right)  
        {  
         pivot=median(a,left,right);  
         i=left;  
         j=right-1;  
         while(1)  
         {  
             while(a[++i]<pivot){}  
             while(a[--j]>pivot){}  
             if(i<j)  
             swap(&a[i],&a[j]);  
             else  
             break;
             
         }  
         swap(&a[i],&a[right-1]);  
         qsort(a,left,i-1);
         printIt(a, left, i-1);    
         qsort(a,i+1,right);     
         printIt(a, i+1, right);     
        }  
        else  
        insertionsort(a+left,right-left+1); 
       
       
     }
     
    
     
     void printIt(int a[], int left, int right)
     {
       int i;
       for(i=left, i<right;i++)
          printf("%2d ",a[i]);
       printf("\n");
    }
     
       
     void quicksort(int a[],int n)  
     {  
        qsort(a,0,n-1);  
     }   
     int main()  
     {  
       printf("Unsorted array:\n8,5,2,13,1,17,6,4,9,15,7,3\n");
       printf("Sorted array: \n");
       int a[]={8,5,2,13,1,17,6,4,9,15,7,3};  
       quicksort(a,12);  
       
       int i;  
       for(i=0;i<12;i++)  
       
        {
         printIt(a, 0, N);  
        }  
        getch();  
     }
    Last edited by Low Chee Kwan; 07-14-2012 at 01:25 AM.

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Do you want to become an expert copy/paster or do you really want to learn programming?
    You are already perfect in the former, but far away from the latter

    Quote Originally Posted by Low Chee Kwan View Post
    i still get errors: line 54 printIt undeclared, line 67 void printIt used prior to declaration,
    You use printIt it on line 54, but declare and define it not until line 67 thus the compiler doesn't know in line 54 about it. The order is important and that's why you should use prototypes when you use many functions which call each other.

    line 69 expected ';' before ')' token
    Look carefully at each character of line 69 and you will find Adak's typo. If not I recommend spending some time learning C basics instead of desperately searching for someone who spoonfeeds you a solution for your homework.

    Bye, Andreas
    Last edited by AndiPersti; 07-14-2012 at 02:28 AM.
    rags_to_riches likes this.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks for noticing that, Andreas. Line 69 should have no commas in it, and have two semi-colons in it.


    Since the original program didn't have prototypes for the functions, I'll share what we're talking about. These are the prototypes I'm using for my version of your program - They're very similar but perhaps not the exact same, as what you are using.

    Code:
    #include <stdio.h>
    
    #define SIZE 10
    
    /* These are the function prototypes */
    void quickSort2(int a[], int n);
    void printIt(int a[], int left, int right);
    void swap(int a[], int left, int right);
    
    int main(void) {
       int a[SIZE]={9,8,7,6,5,4,3,2,1,0};
       printIt(a, 0, SIZE);
       quickSort2(a, SIZE);
    
       //etc.
       return 0;
    }
    Note that the above is from my program, and you may need to change some particulars for yours, since I'm doing what I described for you, on a completely different Quicksort program, that is not compatible with yours. This is for general programming info ONLY, not specific code for your program. That should be an easy job, however.
    Last edited by Adak; 07-14-2012 at 03:02 AM.

  4. #19
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    i modified to this and not sure whether it gotten messy and i still don't understand how to print the process.. how to print the pivot in yellow colour and the swapped numbers with green colour?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>  
     #define cutoff 3 
     #define N 12 
     
           void swap(int *a,int *b)                   
     {  
        int temp=*a;  
        *a=*b;  
        *b=temp;  
     } 
     
     int median(int a[],int left,int right);        // function prototype
     void printIt(int a[], int left, int right);    // function prototype
     void insertionsort(int A[],int n);             // function prototype
     void qsort(int a[],int left,int right);        // function prototype
     void quicksort(int a[],int n);                 // function prototype
     
     
     
     int main()
    {
         int a[]={8,5,2,13,1,17,6,4,9,15,7,3};
         printf("Unsorted array: \n8,5,2,13,1,17,6,4,9,15,7,3\n");
         printIt(a, 0 , N);
        quicksort(a,12);  
       
       int i;  
       for(i=0;i<12;i++)  
       
        {
         printf("%d ",a[i]);  
        }  
        getch();  
      
         
          int median(int a[],int left,int right);     // calling user-defined function
          void printIt(int a[], int left, int right); // calling user-defined function
          void insertionsort(int A[],int n);          // calling user-defined function
          void qsort(int a[],int left,int right);     // calling user-defined function
          void quicksort(int a[],int n);              // calling user-defined function
          
          
          
          printf("\n\t");
          system("pause");
          return 0;
    
    
          int median(int a[],int left,int right);     // function call
          void printIt(int a[], int left, int right); // function call
          void insertionsort(int A[],int n);          // function call
          void qsort(int a[],int left,int right);     // function call
          void quicksort(int a[],int n);              // function call
          
          return 0;
    }
     
      
     
     
     int median(int a[],int left,int right)            /* User-defined function definition for median operation */
     {  
       int center=(left+right)/2;  
       if(a[left]>a[center])  
       swap(&a[left],&a[center]);  
       if(a[left]>a[right])  
       swap(&a[left],&a[right]);  
       if(a[right]<a[center])  
       swap(&a[right],&a[center]);  
       swap(&a[center],&a[right-1]);  
       return a[right-1]; 
     }
     
     
       void printIt(int a[], int left, int right)       /* User-defined function definition for printIt operation */
     {
       int i;
       for(i=left; i<right;i++)
          printf("%2d ",a[i]);
       printf("\n");
    }
     
     
     void insertionsort(int A[],int n)        /* User-defined function definition for partition operation */
     {  
     int j,p,temp;  
     for(p=1;p<n;p++)  
     {  
     temp=A[p];  
     for(j=p;j>0&&A[j-1]>temp;j--)  
     A[j]=A[j-1];  
     A[j]=temp;
     } 
    }
    
    void qsort(int a[],int left,int right)  /* User-defined function definition for qsort operation */
     {    
        int i,j,pivot;  
        if(left+cutoff<=right)  
        {  
         pivot=median(a,left,right);  
         i=left;  
         j=right-1;  
         while(1)  
         {  
             while(a[++i]<pivot){}  
             while(a[--j]>pivot){}  
             if(i<j)  
             swap(&a[i],&a[j]);  
             else  
             break;
             
         }  
         swap(&a[i],&a[right-1]);  
         qsort(a,left,i-1);
         printIt(a, left, i-1);
         qsort(a,i+1,right);
         printIt(a, i+1, right);      
        }  
        else  
        insertionsort(a+left,right-left+1); 
          
     }
      
       
     void quicksort(int a[],int n)          /* User-defined function definition for quicksort operation */
     {  
        qsort(a,0,n-1);  
     }

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll answer you right after the Tour de France stage is over.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Adak View Post
    I'll answer you right after the Tour de France stage is over.
    Unfortunately, I dozed off to sleep while the Tour was still riding the rather boring flat part of the stage, but this is the basics for using conio.h to write out colored text (foreground only).

    Code:
    /* 
    I created an int *a1, above main() (outside any other function), to help with the printing display that I wanted. 
    It works fine with the initial calls, but doesn't work well with the latter calls (yet), where the larger half of the 
    array values get sorted. 
    
    int *a1 = &a[0];
    
    */
    
    void printIt(int a[], int left, int right, int pivot) {
       int i;
    
       for(i=0;i<SIZE && a[i] >-1;i++) {
          if(*(a1+i)==pivot) 
             _textcolor(14);  //14 is yellow
             
          else if(i<left || i>right) 
             _textcolor(2);   //2 is dull gray
          else
             _textcolor(10);
          _cprintf("%2d",*(a1+i)); //10 is bright green
       }
       putchar('\n');
       _textcolor(7);
    
    }
    If you are using Turbo C, you won't need the underline before these functions. My compiler requires them, because they are non-standard.

    The console text (foreground) colors are:

    0 = black
    1 = blue
    2 = green
    3 = cyan
    4 = red
    5 = magenta
    6 = brown
    7 = white
    8 = gray //these are repeated colors, but more intense
    9 = bright blue
    10 = bright green
    11 = bright cyan
    12 = bright red
    13 = bright magenta
    14 = yellow
    15 = bright white

    Note that you can do this and more, in console text colors, using a modern compiler, by including windows.h.
    Last edited by Adak; 07-15-2012 at 01:39 PM.

  7. #22
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    if you are using turbo c, you should download a more modern compiler that complies with current c standards and isn't designed for ~30-yr-old computer architecture.
    ftfy.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 02-20-2012, 09:38 PM
  2. Quick/Merge sorting floating point numbers
    By stupid_engineer in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 10:34 AM
  3. Quick Sorting ?!?!?
    By Moni in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2003, 04:14 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. Quick question on sorting a singly linked list
    By Ham in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 11:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21