Posted By

trusktr on 11/19/10


Tagged

qsort cisp360


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

DrPepper


cisp360 qsort() for string array


 / Published in: C++
 

  1. void qsort(string *list, int start, int finish){
  2. int left = start, right = finish;
  3. char pivot[200];
  4. strcpy( pivot, list[(start+finish)/2].c_str() );
  5. while (left < right) {
  6. while ( strcmp(list[left].c_str(), pivot) < 0 ) left++; // find left candidate
  7. while ( strcmp(list[right].c_str(), pivot) > 0 ) right--; // find right candidate
  8. if (left <= right) {
  9. string temp = list[left];
  10. list[left] = list[right];
  11. list[right] = temp;
  12. left++;
  13. right--;
  14. }
  15. } // while left < right
  16. if (start < right) qsort(list,start,right);
  17. if (left < finish) qsort(list,left,finish);
  18. }

Report this snippet  

You need to login to post a comment.