Advertisement
Tuppu

Untitled

Feb 20th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. // another quite typical sorting algorithm implementated in this function,
  2. // you know it
  3. void Datastructure::quickSort(std::vector<Person>& A, unsigned int p,
  4. unsigned int q)
  5. {
  6. unsigned int r;
  7.  
  8. if(p < q)
  9. {
  10. r = partition(A, p,q);
  11. quickSort(A,p,r);
  12. quickSort(A,r+1,q);
  13. }
  14. }
  15.  
  16. unsigned int Datastructure::partition(std::vector<Person>& A,
  17. unsigned int p, unsigned int q)
  18. {
  19. // i integeter can be randomized, but trying to did it made me tweaking.
  20. // I got problem with two mixed conditions, randomizing i wasnt trivial
  21. unsigned int i = p;
  22. unsigned int j;
  23.  
  24. // at()-functions decreases ~3 percentages 84 % -> 81 %, but safer.
  25. // enlistingYear is the primary to sort, when it is have a same value
  26. // then birthYear matters, can be called as older person, when he(or she?)
  27. // has born earlier (smaller value in birthYear).
  28. for(j=p+1; j<q; j++)
  29. {
  30. if(A.at(j).enlistingYear > A.at(p).enlistingYear ||
  31. (A.at(j).birthYear >= A.at(p).birthYear && A.at(j).enlistingYear ==
  32. A.at(p).enlistingYear))
  33. {
  34. i++;
  35.  
  36. swap(&A.at(i),&A.at(j));
  37. }
  38. }
  39.  
  40. swap(&A.at(i),&A.at(p));
  41.  
  42. return i;
  43. }
  44.  
  45. void Datastructure::swap(Person *x, Person *y)
  46. {
  47. // Person structure, using pointing Person and referensing temp to y
  48. // made the program and me crazy
  49. temp = *x;
  50. *x = *y;
  51. *y = temp;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement