Wrote a sort, but displays wrong: writes (4, 100000000000, 3, 3) .

 array<int> ^name = { 3, 4, 3, 1000000000 }; int b; for (int i = 0; i < name->Length; i++) { for (int j = 0; j < name->Length - 1; j++) { if (name[j] < name[j + 1]) { b = name[j]; name[j] = name[j + 1]; name[j + 1] = b; } } Console::WriteLine(name[i]); } 
  • Called "bubble sort". The algorithm is not very reliable, rarely more effective than the quick sort algorithm, unless, if you want to sort a small sequence, say no more than 30 elements. - Salivan
  • one
    What for the outer loop to twist the name-> Length times? Usually, the bubble stop is made by the absence of movement of elements in the internal cycle. (The fact that sorting is descending is already noted by @Baho) - alexlz

3 answers 3

one)

 if (name[j] < name[j + 1]) { 
  • This is a sort of descending, ascending the opposite

2) Displays incorrectly because the output is in the wrong place.

you need to do something like this at the end (after sorting):

 for (int i = 0; i < name->Length; i++) { Console::WriteLine(name[i]); } 

    Sorting linear arrays is easily done by the built-in .NET Array :: Sort function.

     using namespace System; int main() { array <int>^ x = { 1, 3, 2, 5, 4 }; Array::Sort(x); for (int i = 0; i < x->Length; i++) Console::Write("{0} ", x[i]); } 

      You have an overflow during array initialization. Remove a pair of zeros from a large number, or replace array <int> with array <long long>. Just do not understand why the number is displayed correctly ...

      • The problem is not in the number, there is only 1 billion in the example - Baho
      • And yes, I did not count zeros there. Above he has written 100 billion. - devoln