title: Sorting all elements of matrix by ascending order in c
body: <p>I need to sort a given matrix by ascending order (all elements).</p>
  
  <p>Lets assume the give matrix is </p>
  
  <pre><code>2  0  4  -1
  -3 1  5   5
  6  4  4   9
  -5 8  10  12
  </code></pre>
  
  <p>the sorted matrix should be like this:</p>
  
  <pre><code>-5  -3  -1  0 
  1   2   4   4
  4   5   5   6
  8   9   10  12
  </code></pre>
  
  <p>My code gives me bad results. </p>
  
  <p>When 
      k=0 w=0 
  its ok it does good<br>
  it countinues
      k=0 w=1 
  but then 
  it jumps to<br>
      k=1 w=1
  without checking
      k=1 w=0 
  because of the
      w=j
  i need to replace <strong>w</strong> with something to fix it 
  but i dont know with what to replace it </p>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define n 2
  
  int main()
  {
      int arr[n][n],min,i,j,tmp,y,k,w,z=0,q=0;
      for(i=0;i&lt;n;i++)
          for(j=0;j&lt;n;j++)
          {
              printf("Enter number: ");
              scanf("%d",&amp;arr[i][j]);
          }
  
      for(i=0;i&lt;n;i++)
          for(j=0;j&lt;n;j++)
          {
              min=arr[i][j];
              for(k=i;k&lt;n;k++)
              {
                  for(w=j;w&lt;n;w++)
                  if(arr[k][w]&lt;min)
                  {
                      min=arr[k][w];
                      z=k;
                      q=w;
                  }
                  w=0;
              }
              tmp=arr[i][j];
              arr[i][j]=min;
              arr[z][q]=tmp;
          }
  
      for(i=0;i&lt;n;i++)
      {
          for(j=0;j&lt;n;j++)
              printf("%d ",arr[i][j]);
          printf("\n");
      }
  }
  </code></pre>