fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. void threeWayPartition(int data[], int size, int low, int high) {
  8. int p = -1;
  9. int q = size;
  10. for (int i = 0; i < q;) {
  11. if (data[i] == low) {
  12. swap(data[i], data[++p]);
  13. ++i;
  14. } else if (data[i] >= high) {
  15. swap(data[i], data[--q]);
  16. } else {
  17. ++i;
  18. }
  19. }
  20. }
  21.  
  22. string printArray(int arr[], int size) {
  23. ostringstream result;
  24. for (int i = 0; i < size; ++i) {
  25. result << arr[i] << " ";
  26. }
  27. return result.str();
  28. }
  29.  
  30. int main() {
  31. int arr[] = {5, 5, 5, 1, 1, 1, 8, 8, 8};
  32. int arr_size = sizeof(arr)/sizeof(arr[0]);
  33. cout << "array before sorting: " << printArray(arr, arr_size) << endl;
  34. threeWayPartition(arr, arr_size, 3, 7);
  35. cout << "array after sorting using ==: " << printArray(arr, arr_size) << endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
array before sorting: 5 5 5 1 1 1 8 8 8 
array after sorting using ==: 5 5 5 1 1 1 8 8 8