944,117 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2059
  • C RSS
Dec 7th, 2005
0

critical count for quicksort??

Expand Post »
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
  1. void q_sort (struct CdRecords array [], int count)
  2. {
  3. quick_sort(array,0,count-1);
  4. }
  5. void quick_sort (struct CdRecords array [], int left, int right)
  6. {
  7. int i, j;
  8. char *x;
  9. struct CdRecords temp;
  10.  
  11. i = left;
  12. j = right;
  13.  
  14. x = array[(left+right)/2].Genre;
  15.  
  16. do {
  17. while(strcmp(array[i].Genre,x)<0 && i<right) i++;
  18. while(strcmp(array[i].Genre,x)>0 && j>left) j--;
  19. if(i<=j) {
  20. temp = array[i];
  21. array[i] = array[j];
  22. array[j] = temp;
  23. i++; j--;
  24.  
  25. }
  26.  
  27. }while(i<=j);
  28.  
  29. if(left<j) quick_sort(array, left, j);
  30. if(i<right)quick_sort(array, i, right);
  31. }



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

  1. void naive_sort (struct CdRecords array [], int arraySize, int * count)
  2. {
  3. for (int pass = 0; pass <= arraySize - 2; pass++)
  4. { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
  5. {
  6. *count = *count + 1; // count critical operations
  7. if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
  8. swap (&array[counter], &array[counter+1]);
  9. }
  10. }
  11. }
  12.  
  13. // Exchange a given pair of values in an array
  14. void swap (struct CdRecords * v1, struct CdRecords * v2)
  15. {
  16. struct CdRecords temp;
  17. temp = *v1;
  18. *v1 = *v2;
  19. *v2 = temp;
  20. }

critical count for naive sort

  1. void sort_critical_count(struct CdRecords cdDB[],int choice)
  2. {
  3. int count = 0;
  4. naive_sort (cdDB, datasize, & count);
  5. if(choice == 5)
  6. printf("\nCritical count is : %d\n",count);
  7. else
  8. printf("Array has been sorted");
  9. printf("Press Enter To Continue");
  10. fflush(stdin);
  11. getch();
  12. }


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!!
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Dec 8th, 2005
0

Re: critical count for quicksort??

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 8th, 2005
0

Re: critical count for quicksort??

[QUOTE=NarueUse 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.[/QUOTE]

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..
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Dec 8th, 2005
0

Re: critical count for quicksort??

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....
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Dec 8th, 2005
0

Re: critical count for quicksort??

>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:
  1. FILE *out = fopen ( "somefile", "w" );
  2.  
  3. while ( !done ) {
  4. /* Do something with counter */
  5.  
  6. fprintf ( out, "%d", counter );
  7.  
  8. if ( !done )
  9. fputc ( ',', out );
  10. }
  11.  
  12. fclose ( out );
Format the file as necessary to get the spreadsheet that you want.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 8th, 2005
0

Re: critical count for quicksort??

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!!
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: file handling code
Next Thread in C Forum Timeline: Some help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC