Does anyone know the code to show the critical count for a quick sort algorithm.
Basically my cd database is almost finished.
What i need to do is show a critical count for the quicksort


quicksort for my database

void q_sort (struct CdRecords array [], int count)
{
quick_sort(array,0,count-1);
}
void quick_sort (struct CdRecords array [], int left, int right)
{
int i, j;
char *x;
struct CdRecords temp;

i = left; 
j = right;

x = array[(left+right)/2].Genre;

do {
		while(strcmp(array[i].Genre,x)<0 && i<right) i++;
		while(strcmp(array[i].Genre,x)>0 && j>left) j--;
		if(i<=j) {
			temp = array[i];
			array[i] = array[j];
			array[j] = temp;
			i++; j--;
			
			}
			
			}while(i<=j);
			
		if(left<j) quick_sort(array, left, j);
	  	if(i<right)quick_sort(array, i, right);
	}

i used a naive sort and was able to show a critical count for that as shown below:

void naive_sort (struct CdRecords array [], int arraySize, int * count)
 {
   for (int pass = 0; pass <= arraySize - 2; pass++)
    { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
       {
        *count = *count + 1; // count critical operations
        if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
	   swap (&array[counter], &array[counter+1]);
       }
    }
 }

// Exchange a given pair of values in an array
void swap (struct CdRecords * v1, struct CdRecords * v2)
  {
    struct CdRecords temp;
    temp = *v1;
    *v1 = *v2;
    *v2 = temp;
  }

critical count for naive sort

void sort_critical_count(struct CdRecords cdDB[],int choice)
{
     int count = 0;
     naive_sort (cdDB, datasize, & count);
     if(choice == 5)
        printf("\nCritical count is : %d\n",count);
     else
        printf("Array has been sorted");
     printf("Press Enter To Continue");
     fflush(stdin);
     getch();
}

neone know how i can implement this for the quicksort coding shown above !!
Also does neone know how u can output data ontoa excel spreadsheet??

apreciate all help!!
u all been a great help in recent days!!

>Does anyone know the code to show the critical count for a quick sort algorithm.
Use a global variable, and realize that all of the work is done in the partitioning step of the algorithm. Beyond that, it's pretty simple to add a critical counter to quicksort.

>Also does neone know how u can output data ontoa excel spreadsheet?
Well, you could meander over to www.wotsit.org and try to actually output an xls file...or you could take the easier route and output a comma delimited file, then import it into Excel.

Use a global variable, and realize that all of the work is done in the partitioning step of the algorithm.

or you could take the easier route and output a comma delimited file, then import it into Excel.

is there any example i can see for comma delimited file and could you explain the use of the Global Variable and Partitioning Step...
IM in a learning stage of C, but need info on these asap..

Edited 3 Years Ago by Dani: Formatting fixed

sorry mate ignore the partition and global variable quetion in my last post..
i just need to know aboutt comma delimmited files, how do i do it im quite confused on this one....

>i just need to know aboutt comma delimmited files, how do i do it im quite confused on this one....
It's pretty straightforward. Open a file and write the values to it, separated by commas:

FILE *out = fopen ( "somefile", "w" );

while ( !done ) {
  /* Do something with counter */

  fprintf ( out, "%d", counter );

  if ( !done )
    fputc ( ',', out );
}

fclose ( out );

Format the file as necessary to get the spreadsheet that you want.

I've tried that but for some reason it dont work. The program im working on is in the last Post i put. 'Finally Almost Finished 2 Problems!!'
Take a look there , cuz i got two vital problems in there. I've spent days and hours on this project and just these few things are giving me the problem.

I apreciate the help u've already given!!

This article has been dead for over six months. Start a new discussion instead.