title: How to determine whether a single swap is sufficient to sort an array of integers.?
body: <p>Here is the code snippet, which is not efficient, please provide an efficient way of solving this:</p>
  
  <p>This method returns true, if array is sorted using just single swap.
  This method returns false, if array requires more than one swap operation to sort.</p>
  
  <pre><code>public boolean sortArrayWithSingleSwapIfPossible() {
          int[] A = {1, 5, 3, 3, 2, 6};
          int temp;
          int swapCount = 0;
          int length = A.length;
          for (int i = 0; i &lt; A.length; i++) {
              if (i == length - 1) {
                  break;
              }
              if (A[i] &gt; A[i + 1]) {
                  if (((i + 2) &lt; length) &amp;&amp; A[i + 1] == A[i + 2]) {
                      temp = A[i];
                      A[i] = A[i + 2];
                      A[i + 2] = temp;
                      ++swapCount;
  
                  } else {
                      temp = A[i];
                      A[i] = A[i + 1];
                      A[i + 1] = temp;
                      ++swapCount;
                  }
              }
  
              if (swapCount &gt; 1) {
                  return false;
              }
          }
          return true;
      }
  </code></pre>