How to Order Vectors in C #?

2

How can I get my vector ordered from lowest to highest, what happens is that I already have my method that I should do that, only that I have a logic problem, since it marks me error when making my if

 public void ordeanar()
        {
            for (int x = 0; x < vector.Length; x++)
            {
                for (int k = 0; k < vector.Length - x;k++ )
                {

                    if (vector[k] < vector[k + 1]) --> ERROR
                    {
                        int aux;
                        aux = vector[k];
                        vector[k] = vector[k + 1];
                        vector[k + 1] = aux;
                    }

                }
            }
        }
    
asked by David 22.09.2016 в 17:46
source

3 answers

2

Apparently it is the bubble algorithm. If so, then the correct thing would be:

public void ordeanar()
    {
        for (int x = 0; x < vector.Length-1; x++)
        {
            for (int k = 0; k < vector.Length-1 - x;k++ )
            {

                if (vector[k] < vector[k + 1])
                {
                    int aux;
                    aux = vector[k];
                    vector[k] = vector[k + 1];
                    vector[k + 1] = aux;
                }

            }
        }
    }

You subtract one from the total length of the vector so that it does not give an error when asking vector[k+1] , being in the last iteration, where k should be equal to vector.length - 2

    
answered by 23.09.2016 / 08:57
source
3

Why not use the benefits of Linq? Unless you are studying algorithms, it is not necessary or recommended to implement these functions manually.

This form generates a second array with the sorted data.

var ordenado = vector.OrderBy(x => x).ToArray();

This form sorts the existing array

Array.Sort(vector);

If you want to work downwards.

var ordenado = vector.OrderByDescending(x => x).ToArray();

Sort the same array in descending order

Array.Sort(vector, delegate(int a, int b) { return b - a; });
    
answered by 22.09.2016 в 18:39
0
for (i = 0; i < maxNumbers; i++)
    { //Con este ciclo (for) obtenemos el primer numero del arreglo numbers
        for (j = 0; j < maxNumbers; j++)
        {// Con este ciclo (for) obtenemos el segundo numero del arreglo numbers
            if (numbers[i] > numbers[j]) {
                /*
                Ejemplo visual del proceso (Ordenar de MAYOR a MENOR):
                Array ejemplo: {1,2,3,4,5,6}
                Obtenemos el valor de posicion i (i, en primera instancia, es igual a 0): 1
                Obtenemos el valor de posicion j (j, en primera instancia, es igual a 1): 2
                Comparamos posicion i < j (1 < 2)
                    Si es cierta:
                        Guardamos posicion i (1) en variable temporal (number)
                        Ponemos valor de posicion j en posicion i (el array quedaria {2,2,3,4,5,6})
                        Ponemos valor number (1) en posicion j (el array quedaria {2,1,3,4,5,6})
                    Si es falsa
                        No se procede ninguna modificacion
                */
                number = numbers[i];
                numbers[i] = numbers[j];
                numbers[j] = number;
            }
        }
    }
    
answered by 03.08.2018 в 06:54