From 2ed64df693b72a394fc25d06b23d9e21d6c5997c Mon Sep 17 00:00:00 2001 From: Shiwani <97833337+Shiwani-sahu@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:10:08 +0530 Subject: [PATCH] Create sort-colors.cpp --- sort-colors.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 sort-colors.cpp diff --git a/sort-colors.cpp b/sort-colors.cpp new file mode 100644 index 0000000..ac29fab --- /dev/null +++ b/sort-colors.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + void sortColors(vector& nums) { + int low=0; + int high= nums.size()-1; + int mid=0; + while(mid<=high){ + if(nums[mid]==0){ + swap(nums[low],nums[mid]); + low++; + mid++; + } + else if(nums[mid]==1){ + mid++; + } + else if(nums[mid]==2){ + swap(nums[mid],nums[high]); + high--; + } + } + } +}; + +