From 6bd1d81f73baaf554f97a030b29825f50f5ea7a4 Mon Sep 17 00:00:00 2001 From: omprakash72810 <91782649+omprakash72810@users.noreply.github.com> Date: Sun, 31 Oct 2021 00:48:08 +0530 Subject: [PATCH] Create cppSolutionSortColour.cpp --- Sort Colour/cppSolutionSortColour.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Sort Colour/cppSolutionSortColour.cpp diff --git a/Sort Colour/cppSolutionSortColour.cpp b/Sort Colour/cppSolutionSortColour.cpp new file mode 100644 index 0000000..c9f0b91 --- /dev/null +++ b/Sort Colour/cppSolutionSortColour.cpp @@ -0,0 +1,20 @@ +//https://leetcode.com/problems/sort-colors/ + +class Solution { +public: + void sortColors(vector& arr) { + int mid=0; + int i=0,j=arr.size()-1; + while(mid<=j){ + if(arr[mid]==0){ + swap(arr[i],arr[mid]); + i++;mid++; + }else if(arr[mid]==1){ + mid++; + }else if(arr[mid]==2){ + swap(arr[j],arr[mid]); + j--; + } + } + } +};