Skip to content

Commit

Permalink
Merge pull request #575 from S-ahil-Saxen-a/custom
Browse files Browse the repository at this point in the history
#573 Added Dutch National Flag Algo for arrays in C++
  • Loading branch information
siddharth25pandey authored Oct 11, 2022
2 parents 7d70bad + 8e788fe commit 0ddba0b
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Arrays/dutchNationalFlag.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// GFG Question: https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1
// Sort an array of 0's, 1's and 2's


/* Dutch National Flag Algorithm:
-----------------------------------
Take three-pointers, namely - low, mid, high.
We use low and mid pointers at the start, and the high pointer will point at the end of the given array.
*/

#include<bits/stdc++.h>
using namespace std;

class Solution
{
public:
void sort012(int a[], int n)
{
// code here

int low = 0, mid = 0, high = n-1;

while(high >= mid){

switch(a[mid]){

case 0:
swap(a[low], a[mid]);
low++;
mid++;
break;

case 1:
mid++;
break;

case 2:
swap(a[mid], a[high]);
high--;
break;
}
}
}

};

int main() {
int t;
cin >> t;
while(t--){
int n;
cin >>n;
int a[n];
for (int i=0;i<n;i++){
cin >> a[i];
}
Solution ob;
ob. sort012(a, n);
for(int i=0; i<n; i++){
cout << a[i] <<"";
}
cout << endl;
return 0;
}

0 comments on commit 0ddba0b

Please sign in to comment.