cisp360 qsort() for string array


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.