From 241ca870a654b5e0da93f9943a03f33afddbc4c7 Mon Sep 17 00:00:00 2001 From: 11-yashasvi <83666990+11-yashasvi@users.noreply.github.com> Date: Thu, 13 Oct 2022 23:08:04 +0530 Subject: [PATCH] Sortcolors.cpp --- Leetcode-Medium/Sortcolors.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Leetcode-Medium/Sortcolors.cpp diff --git a/Leetcode-Medium/Sortcolors.cpp b/Leetcode-Medium/Sortcolors.cpp new file mode 100644 index 0000000..15af961 --- /dev/null +++ b/Leetcode-Medium/Sortcolors.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + void sortColors(vector& nums) { + int n=nums.size(),temp=0; + int low=0; + int mid=0; + int high=n-1; + while(mid<=high) + { + if(nums[mid]==0) + { + temp=nums[low]; + nums[low]=nums[mid]; + nums[mid]=temp; + low++; + mid++; + } + else if(nums[mid]==1) + mid++; + else + { + if(nums[mid]==2) + { + temp=nums[mid]; + nums[mid]=nums[high]; + nums[high]=temp; + high--; + } + } + } + for(auto x : nums) + { + cout<