 <p>In your case, control will go to the while-condition after sorting all the elements.so no need to add do-while.Only set size you want. int i,j,n=4,k,temp; int arr[]={3,5,1,7,9,1,2};</p> <pre><code> for(i=0; i&lt;n; i++){ for(j=i+1; j&lt;n; j++){ if(arr[j] &lt; arr[i]){ temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } </code></pre>