Home > database >  How to sort array by first item in string in descending order on C#
How to sort array by first item in string in descending order on C#

Time:11-02

I don't know what to do, my program is not working

I need to sort a two-dimensional array by the first elements of a row in descending order by rearranging the rows (need to sort string without sort item)

Let's say I'm given an array:
1 2 3
4 5 6
7 8 9

As a result of the program, I need to get:
7 8 9
4 5 6
1 2 3 `

using System;

namespace BubbleSort
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Объявление массива и его размерности
            const int n = 3;
            int bubble;
            int[,] A = 
            { 
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 },
            };
            //Алгоритм пузырьковой сортировки
            for (int i = 0; i < n; i  )
            {
                for(int j = 0; j < n - 1; j  )
                {
                    if (A[i, 0] < A[i  , 0])
                    {
                        for(j = 0; j < n-1; j  )
                        {
                            bubble = A[i, j];
                            A[i, j] = A[i, j 1];
                            A[i, j 1] = bubble;
                        }
                    }
                }
            }

            //Вывод массива
            for (int y = 0; y < n; y  )
            {
                for (int x = 0; x < n; x  )
                {
                    Console.Write(A[y, x]   " ");
                }
                Console.WriteLine();
            }
        }
    }
}

`

CodePudding user response:

From the code in your comments, I'm not sure why you have a nested loop for swapping the values since a single loop will do. I've removed that here.

If you refer to the pseudocode implementation on Wikipedia, you'll see that you need to keep running multiple passes across the array until everything is sorted. You can do this like so:


bool swapped;
do
{
    swapped = false; // reset swapped
    for (int i = 0; i < n - 1; i  ) // loop through all but the last row
    {
        if (A[i, 0] < A[i   1, 0]) // determine if this row needs to be swapped with the next row
        {
            swapped = true; // mark swapped
            for (int j = 0; j < n; j  ) // swap each item in row i with each item in row i 1
            {
                int tmp = A[i, j];
                A[i, j] = A[i   1, j];
                A[i   1, j] = tmp;
            }
        }
    }
}
while (swapped); // if we swapped anything, we need to make another pass to ensure the array is sorted

We can also do away with the need for n by using .GetUpperBound(dimension) which returns a value between 0 and n - 1 (where n is the count of items in the array in that dimension). Because the result is effectively n - 1, I've modified the loop conditions slightly:

bool swapped;
do
{
    swapped = false;
    for (int i = 0; i < A.GetUpperBound(0); i  )
    {
        if (A[i, 0] < A[i   1, 0])
        {
            swapped = true;
            for (int j = 0; j <= A.GetUpperBound(1); j  )
            {
                int tmp = A[i, j];
                A[i, j] = A[i   1, j];
                A[i   1, j] = tmp;
            }
        }
    }
}
while (swapped);

We can also refer to the "Optimizing bubble sort" section of the Wikipedia page and implement that instead, which will make our code run more optimally:

int n = A.GetUpperBound(0); // get the initial value of n
do
{
    int newn = 0; // default newn to 0, so if no items are visited, it will remain 0 and the loop will exit
    for (int i = 0; i < n; i  )
    {
        if (A[i, 0] < A[i   1, 0])
        {
            for (int j = 0; j <= A.GetUpperBound(1); j  )
            {
                int tmp = A[i, j];
                A[i, j] = A[i   1, j];
                A[i   1, j] = tmp;
            }
            newn = i; // store the current (highest) value of i swapped
        }
    }
    n = newn; // set the value of n to the highest value of i swapped
}
while (n > 0); // loop until n == 0

The logic here (as explained on Wikipedia) is that by the end of the first pass, the last item is in the correct position. By the end of the second pass, the second-to-last and last items are in the correct position, and so on. So each time, we can visit one less item. When we have 0 items to visit, we have 0 items to swap, and the sort is complete.

You can see this optimised version in this YouTube visualisation.

CodePudding user response:

I think the line

if (A[i, 0] < A[i  , 0]) 

should read

if  (A[i, 0] < A[i  1, 0])
  • Related