2

I'm practicing C# and wrote the code below excluding the line beginning "Array.Sort....". The output was as expected (i.e. "5,7,2,").

When I included the line "Array.Sort..." I was expecting to get "2,5,7," but got the output "5,5,7,", i.e. the 2 had disappeared and had been replaced by a 5 somehow. Could anyone help explain why (to a beginner)?

namespace ConsoleApplication33
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[3] { 5, 7, 2 };

            for (int i = 0; i< numbers.Length; i++)
            {
                Console.Write(numbers[i] + " , ");
                Array.Sort(numbers);

            }
            Console.ReadKey();
        }

    }
}
0

5 Answers 5

3

You don't need a for loop to sort. Just Array.Sort should sorts your array:

int[] numbers = new int[3] { 5, 7, 2 };
Array.Sort(numbers);

After you sorted the array, you can print the sorted array like this:

for (int i = 0; i< numbers.Length; i++) {
    Console.Write(numbers[i] + " , ");
}
Sign up to request clarification or add additional context in comments.

Comments

1

You sort in a loop, writing the i value before. So your code works like this

  • Write first item (5)
  • Sort the array
  • Write the second item (now 5)
  • Sort the array
  • Write the third item (now 7)

You probably wanted to sort the array, then write it out:

int[] numbers = new int[3] { 5, 7, 2 };

Array.Sort(numbers);

for (int i = 0; i< numbers.Length; i++)
{
    Console.Write(numbers[i] + " , ");
}

As an aside, you could also use string.Join and avoid the loop (and trailing comma):

var commaSeparatedNums = string.Join(", ", numbers);
Console.Write(commaSeparatedNums);

Comments

0

Array.sort is a method that doesn't require any kind of loop iteration,, just invoke it is more than enough

your issue is caused because you are looping AND sorting 3 times which is completely unnecessary... now, you are taking the element at position i, then sorting and doing this 3 times... look at the animation for more illustrative explanation

enter image description here you are sorting correct but are reading the elements of the array index by index which is not correct and not necessary

remove the hole for loop and just call Array.Sort(numbers);

//for (int i = 0; i< numbers.Length; i++)
//{
     //Console.Write(numbers[i] + " , ");
     Array.Sort(numbers);
//}

Comments

0

You are sorting the array in the loop. The sort changes the array only the first time that it is applied; second and third invocations of the sort make no changes to the array.

The first printout happens on the unsorted array; the second printout happens on the sorted array. The first time around 5 is the initial number; the second time, 5 is in the middle. That is why you see 5 printed twice.

You need to sort the array once before going into the printing loop in order to fix this problem:

Array.Sort(numbers);
for (int i = 0; i< numbers.Length; i++) {
    Console.Write(numbers[i] + " , ");
}

1 Comment

Thanks everyone. I get it now following your explanations. Thanks for the help :)
0

Use Array.Sort without loop. If you wan to sort an array.

int[] numbers = new int[3] { 5, 7, 2 };
Array.Sort(numbers);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.