944,028 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1412
  • C RSS
Dec 10th, 2005
0

QuickSort Still Not Working??

Expand Post »
Hey after spending a few hours trying to understand why my quick sort is not owrking, i've tried calling the q_sort() and quick_sort() but with no luck......
below is how i had it from the start for less confusion,, can someone tell me why it isnt working...
i've checked the layout in a couple of books also and it seems fine for a structure sort algorithm.

Ancient Dragon u said something about the count in q_sort() not being right but when i change i end up with compile errors.....??..

  1. //(Database Management System(DBMS), user interface and report generator for results.
  2. //Also a diagnostic tool that allows access to the database metrics.
  3. //This is to show the efficiency of the sort algorithms used.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <io.h>
  9. #include <conio.h>
  10. //******************************************************************************************//
  11.  
  12. //Structure
  13.  
  14. struct CdRecords
  15. {
  16. char Artist[20];
  17. char Album[20];
  18. char Year[8];
  19. char Label[20];
  20. char Genre[20];
  21. };
  22. //******************************************************************************************//
  23.  
  24. void naive_sort (struct CdRecords array [], int arraySize, int * count);
  25. void swap (struct CdRecords * v1, struct CdRecords * v2);
  26.  
  27. void write_records(struct CdRecords cdDB[]);
  28. void write_file(struct CdRecords cdDB[]);
  29. void read_file(struct CdRecords cdDB[]);
  30. void delAlbum(struct CdRecords cdDB[]);
  31.  
  32. void sort_critical_count(struct CdRecords cdDB[], int choice);
  33. void record_search(struct CdRecords cdDB[]);
  34.  
  35. void quick_sort (struct CdRecords array [], int left, int right);
  36. void q_sort (struct CdRecords array [], int count);
  37.  
  38. void init_list(struct CdRecords cdDB[]);
  39. int find_free(struct CdRecords cdDB[]);
  40.  
  41.  
  42.  
  43. const int datasize = 20;
  44. // file pointer
  45. FILE * fp;
  46.  
  47.  
  48. //******************************************************************************************//
  49. void init_list(struct CdRecords cdDB [])
  50. {
  51. //int i; //TO CONTROL LOOP FOR CLEARING STRUCTURE
  52. //for (i=0;i<datasize; i++)
  53. // cdDB[i].Artist[0] = '\0'; //SET THE TITLE TO NULL
  54. memset(cdDB,0,datasize*sizeof(struct CdRecords));
  55. //TO CLEARING STRUCTURE
  56. //SET THE TITLE TO NULL
  57. }
  58. //******************************************************************************************//
  59. //FIND FREE SLOT TO ADD NEW ENTRY
  60.  
  61. int find_free(struct CdRecords cdDB[])
  62. {
  63. int i;
  64. char test; //FIELD TO CHECK FOR A VALUE IN TITLE
  65. for(i=0; i<datasize; i++){
  66. test = cdDB[i].Album[0]; //STORE TITLE IN TEST FIELD
  67.  
  68. if (test=='\0'){ //IF TEST FIELD IS NULL THERE IS AN EMPTY SLOT
  69. return i; //RETURN THE SLOT NUMBER
  70. }
  71.  
  72. }
  73.  
  74. return -1; //IF NO FREE SLOTS FOUND RETURN -1
  75. }
  76. //******************************************************************************************//
  77.  
  78. void sort_critical_count(struct CdRecords cdDB[],int ch)
  79. {
  80. system("CLS");
  81. int count = 0;
  82. int nRows = find_free(cdDB);
  83. naive_sort (cdDB, nRows, & count);
  84.  
  85. //naive_sort (cdDB, datasize, & count);
  86.  
  87. //if(ch==3){
  88. //q_sort (cdDB, count);
  89. // }
  90. if(ch == 5)
  91. printf("\nCritical count is : %d\n",count);
  92. else
  93. printf("Array has been sorted");
  94. printf("Press Enter To Continue");
  95. fflush(stdin);
  96. getch();
  97. }
  98.  
  99. //******************************************************************************************//
  100.  
  101. void write_records(struct CdRecords cdDB[])
  102. {
  103. system("CLS");
  104. int i;
  105. char ch;
  106. ch=1;
  107. i=0;
  108. int slot; // INTEGER TO LOCATE FREE SPACE IN STRUCTURE
  109. char s[80]; // CALL THE FIND_FREE FUNCTION AND RETURN VALUE IN FIELD - SLOT
  110. slot = find_free(cdDB);
  111.  
  112. if (slot==-1){ //IF SLOT = -1 THEN THE STRUCTURE IS FULL
  113. printf("Database Full");
  114. return;
  115. }
  116. //input Data from the keyboard
  117. //go through record
  118. //write to disk using formatted output
  119. // for (i=0;i<datasize;i++)
  120.  
  121. // {
  122.  
  123.  
  124. printf("Enter the Artist: \n");
  125. //scanf("%s",cdDB[i].Artist);
  126. gets(cdDB[slot].Artist);
  127. gets(cdDB[slot].Artist);
  128. printf("Enter the Album: \n");
  129. //scanf("%s",cdDB[i].Album);
  130. gets(cdDB[slot].Album);
  131. printf("Enter Label name: \n");
  132. //scanf("%s",cdDB[i].Label);
  133. gets(cdDB[slot].Label);
  134. printf("Enter Year: \n");
  135. //scanf("%d",&cdDB[i].Year);
  136. gets(cdDB[slot].Year);
  137. printf("Enter the Genre of music: \n");
  138. //scanf("%s",cdDB[i].Genre);
  139. gets(cdDB[slot].Genre);
  140. // printf("Enter 1 to continue or 0 To Finish: \n");
  141. // scanf("%d",&ch);
  142. //if(ch==0)
  143. //{
  144. //break;
  145. //}
  146. // }
  147. }
  148.  
  149. //******************************************************************************************//
  150. void delAlbum(struct CdRecords cdDB[])
  151. {
  152. int slot; //INTEGER TO HOLD SLOT NUMBER
  153. char s[80]; //FIELD TO HOLD INPUTTED RECORD NUMBER TO DELETE
  154. printf("Enter Album: "); //PROMPT FOR Album TO DELETE
  155. gets(s);
  156. slot = atoi(s); //CONVERT INPUT VALUE TO A SLOT NUMBER
  157.  
  158. if (slot>=0 && slot < datasize)
  159. cdDB[slot].Artist[0] = '\0'; //CLEAR SELECTED STRUCTURE ENTRY
  160. }
  161. //******************************************************************************************//
  162. void record_search(struct CdRecords cdDB[])
  163. {
  164. system("CLS");
  165. int i;
  166. char name[20];
  167.  
  168. printf("Enter Artist name :");
  169. scanf("%s", name);
  170. for(i = 0;i<datasize;i++)
  171. {
  172. if((strcmp(name,cdDB[i].Artist))==0)
  173. {
  174. printf("\n");
  175. printf("%s\n",cdDB[i].Artist);
  176. printf("%s\n",cdDB[i].Album);
  177. printf("%s\n",cdDB[i].Label);
  178. printf("%s\n",cdDB[i].Year);
  179. printf("%s\n",cdDB[i].Genre);
  180.  
  181. }
  182. }
  183. printf("Press Enter To Continue");
  184. fflush(stdin);
  185. getch();
  186.  
  187. }
  188.  
  189. //******************************************************************************************//
  190.  
  191. void write_file(struct CdRecords cdDB[])
  192. {
  193. system("CLS");
  194. int i;
  195. fp=fopen("CD-Records.txt","w");
  196.  
  197. for (i=0;i<datasize;i++)
  198. {
  199. fprintf(fp, "%s %s %s %s %s\n",cdDB[i].Artist,cdDB[i].Album,cdDB[i].Label,cdDB[i].Year,cdDB[i].Genre);
  200. }
  201.  
  202. printf("Data has been written to file");
  203. fflush(stdin);
  204. getch();
  205. fclose(fp);//Close the file
  206.  
  207. }
  208.  
  209. //******************************************************************************************//
  210.  
  211. void read_file(struct CdRecords cdDB[])
  212. {
  213. system("CLS");
  214. int j;
  215. fp = fopen("CD-Records.txt","r");
  216. //Read the data that was written
  217. printf("reading data...\n");
  218.  
  219. for (j=0; j<datasize; j++)
  220. {
  221. if (cdDB[j].Artist[0]){
  222. fscanf(fp,"%s %s %s %s %s",cdDB[j].Artist, cdDB[j].Album, cdDB[j].Label, &cdDB[j].Year, cdDB[j].Genre);
  223. printf("Record %d\n",j+1," is...\n");
  224. printf("%s\n",cdDB[j].Artist);
  225. printf("%s\n",cdDB[j].Album);
  226. printf("%s\n",cdDB[j].Label);
  227. printf("%s\n",cdDB[j].Year);
  228. printf("%s\n",cdDB[j].Genre);
  229. //printf("\n\n");
  230. }
  231. printf("\n\n");
  232. }
  233.  
  234. fclose(fp);
  235. printf("Press Enter To Continue");
  236. fflush(stdin);
  237. getch();
  238. // close file
  239. }
  240.  
  241. //******************************************************************************************//
  242.  
  243. // Sort an array of integers with inefficient version of bubblesort
  244.  
  245. void naive_sort (struct CdRecords array [], int arraySize, int * count)
  246. {
  247. int find_free();
  248. for (int pass = 0; pass <= arraySize - 2; pass++)
  249. { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
  250. {
  251. *count = *count + 1; // count critical operations
  252. if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
  253. swap (&array[counter], &array[counter+1]);
  254. }
  255. }
  256. }
  257.  
  258. // Exchange a given pair of values in an array
  259. void swap (struct CdRecords * v1, struct CdRecords * v2)
  260. {
  261. struct CdRecords temp;
  262. temp = *v1;
  263. *v1 = *v2;
  264. *v2 = temp;
  265. }
  266.  
  267. //******************************************************************************************//
  268. void q_sort (struct CdRecords array [], int count)
  269. {
  270. quick_sort(array,0,count-1);
  271. }
  272. void quick_sort (struct CdRecords array [], int left, int right)
  273. {
  274. int i, j;
  275. char *x;
  276. struct CdRecords temp;
  277.  
  278. i = left;
  279. j = right;
  280.  
  281. x = array[(left+right)/2].Genre;
  282.  
  283. do {
  284. while(strcmp(array[i].Genre,x)<0 && i<right) i++;
  285. while(strcmp(array[i].Genre,x)>0 && j>left) j--;
  286. if(i<=j) {
  287. temp = array[i];
  288. array[i] = array[j];
  289. array[j] = temp;
  290. i++; j--;
  291.  
  292. }
  293.  
  294. }while(i<=j);
  295.  
  296. if(left<j) quick_sort(array, left, j);
  297. if(i<right)quick_sort(array, i, right);
  298. }
  299. //******************************************************************************************//
  300.  
  301. int main()
  302. {
  303. system("CLS");
  304. int ch = 0;
  305.  
  306. struct CdRecords cdDB[datasize];
  307. init_list(cdDB); //CLEAR THE STRUCTURE
  308. do{
  309. system("CLS");
  310. printf("MAIN MENU\n");
  311. printf("Press 1 To Enter Records\n");
  312. printf("Press 2 To Bubble Sort Records\n");
  313. printf("Press 3 To Quick Sort Records\n");
  314. printf("Press 4 To Save Records To File\n");
  315. printf("Press 5 To Read Records From File\n");
  316. printf("Press 6 To Show Diagnostic Details\n");
  317. printf("Press 7 To Search For A Record\n");
  318. printf("Press 8 To Delete A Record\n");
  319. printf("Press 9 To Quit\n");
  320. printf("Enter Your Choice : ");
  321. scanf("%d",&ch);
  322.  
  323. switch (ch)
  324. {
  325.  
  326. case 1:
  327. write_records(cdDB);
  328. break;
  329. case 2:
  330. sort_critical_count(cdDB,2);
  331. break;
  332. case 3:
  333. quick_sort(cdDB,1,2);
  334. break;
  335. case 4:
  336. write_file(cdDB);
  337. break;
  338. case 5:
  339. read_file(cdDB);
  340. break;
  341. case 6:
  342. sort_critical_count(cdDB,5);
  343. break;
  344. case 7:
  345. record_search(cdDB);
  346. break;
  347. case 8:
  348. delAlbum(cdDB);
  349. break;
  350. case 9:
  351. break;
  352. default:
  353. printf("%d",&ch);
  354. break;
  355. }
  356.  
  357. } while(ch !=9);
  358. }
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Dec 10th, 2005
0

Re: QuickSort Still Not Working??

Don't use gets(). It's dangerous. Use fgets() instead. (http://faq.cprogramming.com/cgi-bin/...&id=1043284351)

fflush(stdin) is also wrong. (http://faq.cprogramming.com/cgi-bin/...&id=1043284351)

main() should return a value.
printf("%d",&ch);
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Dec 10th, 2005
0

Re: QuickSort Still Not Working??

It took a little while to work out the algorithm, but below is one way to sort the array indirectly using another integer array. First, initialze an int array with values 0, 1, 2, .... During the sort, swap these numbers instead of members of the original structure array. The algorithm was taken from here which may have been the same one you used.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct str
  5. {
  6. char name[80];
  7. };
  8.  
  9. str s[] = {
  10. {"six"},
  11. {"seven"},
  12. {"eight"},
  13. {"Filex"},
  14. {"Max"},
  15. {"Albert"},
  16. };
  17.  
  18.  
  19. void q_sort(str array[], int numbers[], int left, int right)
  20. {
  21. int pivot, l_hold, r_hold;
  22.  
  23. l_hold = left;
  24. r_hold = right;
  25. pivot = numbers[left];
  26. while (left < right)
  27. {
  28. while (( strcmp(array[numbers[right]].name, array[pivot].name) >= 0) && (left < right))
  29. right--;
  30. if (left != right)
  31. {
  32. numbers[left] = numbers[right];
  33. left++;
  34. }
  35. while ((strcmp(array[numbers[left]].name,array[pivot].name) <= 0) && (left < right))
  36. left++;
  37. if (left != right)
  38. {
  39. numbers[right] = numbers[left];
  40. right--;
  41. }
  42. }
  43. numbers[left] = pivot;
  44. pivot = left;
  45. left = l_hold;
  46. right = r_hold;
  47. if (left < pivot)
  48. q_sort(array,numbers, left, pivot-1);
  49. if (right > pivot)
  50. q_sort(array,numbers, pivot+1, right);
  51. }
  52.  
  53. void quickSort(str array[], int numbers[], int array_size)
  54. {
  55. q_sort(array, numbers, 0, array_size - 1);
  56. }
  57.  
  58. int main(int argc, char* argv[])
  59. {
  60. int i;
  61. int array[] = {0,1,2,3,4,5,6};
  62. quickSort(s,array, 6);
  63. // duplicate the s array so that we can rearrange it
  64. // according to the order of integers in array[].
  65. struct str s1[6];
  66. memcpy(s1,s, sizeof(s1));
  67. for(i = 0; i < 6; i++)
  68. {
  69. s[i] = s1[array[i]];
  70. }
  71. for(i = 0; i < 6; i++)
  72. cout << s[i].name << " ";
  73. cout << endl;
  74. return 0;
  75. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Dec 10th, 2005
0

Re: QuickSort Still Not Working??

Hey thanx for the help..... but when i try to incorporate the coding u wrote to the program u helped me with in the other thread ' Why is qsort not initialised here!!??''

it dont seem to work???
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Dec 10th, 2005
0

Re: QuickSort Still Not Working??

it won't work in your program as I wrote it. You have to make appropriate changes to the structure. If you are using a compiler that has a debugger, learn to use the debugger so that you can single-step through the program and see what is going on.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Dec 10th, 2005
0

Re: QuickSort Still Not Working??

For reference, the other thread is here: http://www.daniweb.com/techtalkforums/thread36544.html
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 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: float pointers
Next Thread in C Forum Timeline: I need ur help please........





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


Follow us on Twitter


© 2011 DaniWeb® LLC