Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. public void sortColors(int[] nums) {
  2. int low = 0;
  3. int hi = nums.length-1;
  4. int index = 0;
  5. while (index <= hi) {
  6. if (nums[index] == 1) {
  7. index++;
  8. } else if (nums[index] == 0) {
  9. swap(nums, index, low);
  10. low++;
  11. index++;
  12. } else {
  13. swap(nums, index, hi);
  14. hi--;
  15. }
  16. }
  17. }
  18.  
  19. private void swap(int[] nums, int right, int left) {
  20. if (nums[right] == nums[left]) {
  21. return;
  22. }
  23. int temp = nums[left];
  24. nums[left] = nums[right];
  25. nums[right] = temp;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement