Sorting array or vectors in c # [closed]

4

Good, I would appreciate if someone can take this apart and explain it to "fools". I understand things, that is why I have reached that point of the course, but I do not understand several things. Someone with some clear concept? I know I have to compare the values of the vector, but I do not understand how it does ... Thanks!

public void Ordenar()
    {
        for (int k = 0; k < 4; k++)
        {
            for (int f = 0; f < 4 - k; f++)
            {
                if (sueldos[f] > sueldos[f + 1])
                {
                    int aux;
                    aux = sueldos[f];
                    sueldos[f] = sueldos[f + 1];
                    sueldos[f + 1] = aux;
                }
            }
        }
    }
    
asked by Agustin Gimenez Rojas 19.04.2017 в 23:58
source

2 answers

3

I will try to explain it as you ask, I hope not to make any big mistake in the process to simplify ect

  • what I do not see is because we use that for with k,

that for with k is responsible for iterating as many times as the array has the confusing data is that it uses a magic number 1 and does not take the size of the array with size lenght or anything.

1 magic number I realized after rereading the answer thinking that maybe it was confusing for you, for this particular case: magic number, I mean what to put or use a number that may seem randomly placed in this case the 4 that probably represents the size of the array, instead of using something more expressive like using something like this mi_array.Lenght or anything, be it property or method, or whatever it is that returns a value or from which you obtain a value.

  • because he makes 2 trips, and then in the for f, I find f < 4-k.

In this part if (sueldos[f] > sueldos[f + 1]){//.. takes the elements and compares them, swaping (exchanging) the positions, if this is true and so with all the items, in the first iteration of for (int k = 0; k < 4; k++) because 4-k is 4 Now at the end of the first iteration of the for k already has the most one position at the end and made up to compare with all I mean the 4, but the next iteration "back" and we have the largest position 4, from there to that use 4-k because the last one is already the largest according to the condition of the if, but the rest is still unordered (or not but we do not know) so we check and move it, but we know that 4 is the biggest one so we stopped before getting to your position, as mentioned here 4-k and so on until you finish with all the data.

  • I do not understand that [f + 1] that you do, if you assign the other position in the array or you increase the value by 1. I think the value that we have to move is stored in the aux, but I do not see as position 0 ends up being another
if (sueldos[f] > sueldos[f + 1]){ //sueldo 0 > sueldo 0+1  sueldo = 1  para el siguiente parametro

      int aux;
      aux = sueldos[f]; // aux almcena el valor de sueldo en la posicion 0
      sueldos[f] = sueldos[f + 1]; //ahora a sueldo en la posicion 0 se le asigna la de sueldo f+1 como dijimos antes sueldo en la posicion 1 porque f = 0 para la primera iteracion 
      sueldos[f + 1] = aux; ahora en la variable aux tenemos el valor inicial de sueldo en la posicion 0 que es la de f para esa iteracion en se momento pues ahora mismo sueldo [0] tiene el valor de sueldo[f + 1], osea el mismo valor tanto para sueldo[0] como para sueldo[f + 1]y no pueden tener el mismo valor por ser movidos porque entonces nos estamos cargando un valor que estaba en sueldo[0] asi que tomamos el valor que "copiamos" al principio en aux y se lo asiganamos a sueldo[f + 1] intente imaginarselo mentalmente y vera como el dato avanza de posicion. Entonces al ser sueldo[0] diferente al valor inicial tenemos que volver a comprobarlo/comparalo en la siguiente iteracion de 'for (int k = 0; k < 4; k++)' pero solo hasta '4-k'.

}

would end with the fok f and then again the for k until the end of k, but in each iteration k is worth a number more so the for f only iterates until f < 4-k (where k would be 0 then 1 and so) with what each time is left without "checking" more values than we have at the end because they are already ordered and that is determined by 4-k

to be read better

if (sueldos[f] > sueldos[f + 1]){ salary 0 > salary 0 + 1 salary = 1 for the next parameter

aux = sueldos[f]; auxcena the salary value in the 0 position

sueldos[f] = sueldos[f + 1]; // now salaried in position 0 is assigned the salary f + 1 as we said before salary in position 1 because f = 0 for the first iteration

sueldos[f + 1] = aux; now in the aux variable we have the initial value of salary in the position 0 that is the one of f for that iteration in now moment because right now sueldo [0] has the value of sueldo[f + 1] , that is the same value both for sueldo[0] as for sueldo[f + 1] and can not have the same value for being moved because then we are loading a value that was in sueldo[0] so we take the value that we "copy" at the beginning in aux and we assign it to sueldo[f + 1] try to imagine it mentally and you will see how the data advances position . Then to be sueldo[0] different from the initial value we have to check it again / compare it in the next iteration of for (int k = 0; k < 4; k++) but only until 4-k .

public void Ordenar()
    {
        for (int k = 0; k < 4; k++)
        {
            for (int f = 0; f < 4 - k; f++)
            {
                if (sueldos[f] > sueldos[f + 1])
                {
                    int aux;
                    aux = sueldos[f];
                    sueldos[f] = sueldos[f + 1];
                    sueldos[f + 1] = aux;
                }
            }
        }
    }

It is being formatted publicly so that the answer is not deleted

    
answered by 20.04.2017 в 01:32
0
     public void Ordenar(){ // metodo que ordena de menor a mayor un arreglo de unica y exclusivamente 5 elementos.
         for (int k = 0; k < 4; k++){ //Hace un recorrido de 0 a 4 
             for (int f = 0; f < 4 - k; f++){ //en el primer intento quiere recorrer hasta cuatro luego hasta 3 y asi baja a 0
                 if (sueldos[f] > sueldos[f + 1]){//si el elemento en la posicion f el mayor al siguiente hace un intercambio
                    int aux;
                    aux = sueldos[f];//guarda el elemento en posicion f
                    sueldos[f] = sueldos[f + 1]; //pasa el elemento f+1 a la posicion f
                    sueldos[f + 1] = aux;// en la posicion f+1 pone el elemento que estaba en posicion f
                 } 
             }
         }
    }

I'll give you a tour of the for ... [5,4,8,2,0]

k = 0, f = 0, f < 4-0? YES, 5 & 4? YES, aux = 5, salaries [f] = 4, salary [f + 1] = 5, [4,5,8,2,0]

k = 0, f = 1, f < 4-0? YES, 5 & 8? DO NOT, [4,5,8,2,0]

k = 0, f = 2, f < 4-0? YES, 8 & 2? YES, aux = 8, salaries [f] = 2, salary [f + 1] = 8, [4,5,2,8,0]

k = 0, f = 3, f < 4-0? YES, 8> 0? YES, aux = 8, salaries [f] = 0, salaries [f + 1] = 8, [4,5,2,0,8]  with this it is assumed that the last element is already ordered, so for the next path it only goes from 0 to 3 (4-k, k = 1).

    
answered by 20.04.2017 в 00:26