title: C++ - program has stopped working
body: <p>The code that I posted below is supposed to work in recursion (the Sort() function) even up to 1kk times. The problem is: when the Sort() function gets into loop number 43385 the console stops working and alerts: "The program has stopped working". Is it a problem with memory? If yes, where is the bad part of the code? Greetings.  </p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;  
  string a, b;  
  int n=0,i=0,counter=0;
  
  int Sort(int i)  
  {  
      int x=0,y=0,tmp0=0;  
      char tmp1;  
  
      for(x=i;x&lt;n;x++) {
          if(a[x]==b[i]){
              tmp0=x;
              tmp1=a[x];
              break;
          }
          else
              continue;
      }
      for(y=tmp0;y&gt;=i;y--)
          y==i ? a[i]=tmp1 : a[y]=a[y-1];
  
      counter+=tmp0-i;
      if(i==n-1)
          return counter;
      else
          Sort(i+1);  
  }  
  int main()  
  {  
      cin &gt;&gt; n &gt;&gt; a &gt;&gt; b;  
      Sort(0);  
      return 0;  
  }
  </code></pre>