finding the middle element of the list with 0(n)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 50
Reputation: Iam3R is an unknown quantity at this point 
Solved Threads: 3
Iam3R Iam3R is offline Offline
Junior Poster in Training

finding the middle element of the list with 0(n)

 
0
  #1
Oct 29th, 2009
Hello ,
i have written the code for finding the middle element of the list with
0(n) complexity.

please gurus, verify that and let me know if it needs any modifications

and ofcourse i have question to ask

when the no of elements are odd
we can easily find the middle element
suppose
1 2 3 4 5 the middle is 3
but when the no of elements are even
1 2 3 4 5 6
how can we decide the middle element
is there any most widely used way or experts follow.

please let me know


NOTE : in my previous post i have given the insert function you can have a look at it if you need
insert
  1. int find_mid(struct node *nrml)
  2. {
  3. struct node *adv=nrml;
  4. while(adv) {
  5. if(adv->next !=NULL) {
  6. //if(adv->next->next != NULL)
  7. adv = adv->next->next ;
  8. nrml = nrml -> next;
  9. }
  10. else {
  11. printf("with odd :\n");
  12. return nrml->data;
  13. }
  14. }
  15. printf("with even :\n");
  16. return nrml->data;
  17. }
  18.  
  19.  
  20. Driver code :
  21.  
  22. #define ODD 9
  23. #define EVEN 10
  24. int main()
  25. {
  26. struct node *podd=NULL;
  27. struct node *peven=NULL;
  28. int n,d,i=0,ele;
  29. int odd[ODD]={1,2,3,4,5,6,7,8,9};
  30. int even[EVEN]={1,2,3,4,5,6,7,8,9,10};
  31. while(i<ODD)
  32. {
  33. insert(&podd,odd[i++]);
  34. }
  35.  
  36. i=0;
  37. while(i<EVEN)
  38. {
  39. insert(&peven,even[i++]);
  40. }
  41.  
  42. display(podd);
  43. ele = find_mid(podd);
  44. printf("\n%d",ele);
  45.  
  46. printf("\n");
  47. display(peven);
  48.  
  49. ele = find_mid(peven);
  50. printf("\n%d",ele);
  51.  
  52. return 0;
  53. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 50
Reputation: Iam3R is an unknown quantity at this point 
Solved Threads: 3
Iam3R Iam3R is offline Offline
Junior Poster in Training

Verify It and correct me

 
0
  #2
33 Days Ago
i have posted this 3 days ago but i dint get any reply.
this time hope some people will reply.

thanks in advance,


Originally Posted by Iam3R View Post
Hello ,
i have written the code for finding the middle element of the list with
0(n) complexity.

please gurus, verify that and let me know if it needs any modifications

and ofcourse i have question to ask

when the no of elements are odd
we can easily find the middle element
suppose
1 2 3 4 5 the middle is 3
but when the no of elements are even
1 2 3 4 5 6
how can we decide the middle element
is there any most widely used way or experts follow.

please let me know


NOTE : in my previous post i have given the insert function you can have a look at it if you need
insert
  1. int find_mid(struct node *nrml)
  2. {
  3. struct node *adv=nrml;
  4. while(adv) {
  5. if(adv->next !=NULL) {
  6. //if(adv->next->next != NULL)
  7. adv = adv->next->next ;
  8. nrml = nrml -> next;
  9. }
  10. else {
  11. printf("with odd :\n");
  12. return nrml->data;
  13. }
  14. }
  15. printf("with even :\n");
  16. return nrml->data;
  17. }
  18.  
  19.  
  20. Driver code :
  21.  
  22. #define ODD 9
  23. #define EVEN 10
  24. int main()
  25. {
  26. struct node *podd=NULL;
  27. struct node *peven=NULL;
  28. int n,d,i=0,ele;
  29. int odd[ODD]={1,2,3,4,5,6,7,8,9};
  30. int even[EVEN]={1,2,3,4,5,6,7,8,9,10};
  31. while(i<ODD)
  32. {
  33. insert(&podd,odd[i++]);
  34. }
  35.  
  36. i=0;
  37. while(i<EVEN)
  38. {
  39. insert(&peven,even[i++]);
  40. }
  41.  
  42. display(podd);
  43. ele = find_mid(podd);
  44. printf("\n%d",ele);
  45.  
  46. printf("\n");
  47. display(peven);
  48.  
  49. ele = find_mid(peven);
  50. printf("\n%d",ele);
  51.  
  52. return 0;
  53. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 352
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 53
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #3
32 Days Ago
1> finding the mid:

Your function for finding the mid element is good enough. The complexity is O(n/2) not O(n) but anyway O(n/2) is also considered O(n) only when n is very large.

2> mid of even set of data

Its upto you or upto the requirement which will tell which one to select as the mid-element. Sometimes u may take the n/2 th element or sometimes u can take (n/2 +1)th element.
e.g. in the set {1,2,3,4}
U can either take 2 or 3 as the mid-element.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 50
Reputation: Iam3R is an unknown quantity at this point 
Solved Threads: 3
Iam3R Iam3R is offline Offline
Junior Poster in Training
 
0
  #4
32 Days Ago
thanks for your reply Dkalita, I indeed like your answer.
and i hope you also verify this and guide me rich.


find nthlast using 0(n) complexity
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC