Need help with sorting

My code is:

class Program

{ static void Main(string[] args) { int[] nmbr = new int[3]; for(int i = 0; i < nmbr.Length; i++) { Console.WriteLine("Provide numbers: " + i); nmbr[i] = int.Parse(Console.ReadLine()); } int max = nmbr.Length - 1; for (int j = 0; j < max; j++) { int nrLeft = max - j; for (int k = 0; j < nrLeft; j++) { if(nmbr[k] > nmbr[k++]) { int temp = nmbr[k]; nmbr[k] = nmbr[k++]; nmbr[k++] = temp; } } Console.WriteLine(nmbr[2]); } } } 

The problem is when the program prints out nmbr[2] it is no longer sorted and therefore prints out the last number entered, and I can't figure out why.

by CptArmadillo via /r/csharp

Leave a Reply