David
David

Reputation: 47

Getting wrong result in sorting Arrays

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();
        }

    }
}

Upvotes: 2

Views: 2125

Answers (5)

Salah Akbari
Salah Akbari

Reputation: 39946

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] + " , ");
}

Upvotes: 3

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);
//}

Upvotes: 0

Vivek Nuna
Vivek Nuna

Reputation: 1

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

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

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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] + " , ");
}

Upvotes: 0

Charles Mager
Charles Mager

Reputation: 26213

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);

Upvotes: 1

Related Questions