Question: I have written a code for sorting 0s, 1s and 2s in an array in C++. The code is working fine on other online IDE and on VScode but don’t know why it is not working on Replit.
Repl link: https://replit.com/@amanraiii/sortingArray#main.cpp
#include<iostream>
using namespace std;
void printArray(int arr[], int size){
for(int i = 0; i < size; i++){
cout << arr[i] << " ";
}
cout << endl;
}
void sortArray(int arr[], int size){
int s1 = 0, s2 = 0, e1 = size-1, e2;
while(s2 < e2){
if(s1<=e1){
if(arr[s1] != 2){
s1++;
}
else if(arr[e1] == 2){
e1--;
}
else{
swap(arr[s1], arr[e1]);
}
e2 = e1;
}
else{
if(arr[e2] == 1){
e2--;
}
else if(arr[s2] == 0){
s2++;
}
else{
swap(arr[s2], arr[e2]);
}
}
}
}
int main(){
int arr[10] = {1,1,2,2,0,0,1,0,1,2};
int size = 10;
cout << "***Unsorted Array***" << endl;
printArray(arr,size);
sortArray(arr, size);
cout << "***Sorted Array***" << endl;
printArray(arr, size);
}