31. Data displacement

Data shifting refers to the action of moving data from one location in memory to another. It is an operation that is performed on many occasions, for example, in sorting algorithms, which we will see in the following units.

Note

In the Python language there are efficient methods to carry out data movement. These methods are based on the use of slices, tuple assignment, and insertion and deletion functions in lists. However, in this and subsequent units, we will not use these efficient methods of moving data in Python. The purpose is to gain a lower-level understanding of how these operations are performed, which can be applicable in any other programming language.

Data exchange

It is common for a program to have to exchange the data of two variables or two positions in a list. In order to exchange the data we will use an intermediate temporary variable:

bajo = 200
alto = 100

# Intercambiamos los datos entre sí.
temp = bajo
bajo = alto
alto = temp

Another example can be given with data lists. In the following case we are going to sort a list of numbers that is not sorted, exchanging the two numbers in the middle of the list:

lista = [ 1, 3, 2, 4 ]

temp = lista[1]
lista[1] = lista[2]
lista[2] = temp

Shift right

If we need to move many numbers from a list, the procedure will be similar to that of exchanging data, but somewhat longer.

In the following example we need to move the last element of a list to the beginning of the list to sort all the numbers:

lista = [ 1, 2, 3, 4, 5, 0 ]
#        ^---------------┘

This is an operation similar to that of exchange. It involves moving all the numbers to the right to leave a free space at the beginning of the list, which is where we will eventually place the number 0.

To begin we will save the value of the last element in an intermediate variable:

temp = lista[5]

Now we move all the elements, except the last one, to the right. To avoid stepping on the values ​​you will have to start on the right:

lista[5] = lista[4]
#   [ 1, 2, 3, 4, 5, 5 ]

and continue to the left:

lista[4] = lista[3]
#   [ 1, 2, 3, 4, 4, 5 ]

lista[3] = lista[2]
#   [ 1, 2, 3, 3, 4, 5 ]

lista[2] = lista[1]
#   [ 1, 2, 2, 3, 4, 5 ]

lista[1] = lista[0]
#   [ 1, 1, 2, 3, 4, 5 ]

Finally we copy the value of the temporary variable in the first position of the list to the far left, to obtain the completely ordered list:

lista[0] = temp
#   [ 0, 1, 2, 3, 4, 5 ]

These operations can be speeded up by means of a for loop, so the final program would look like this:

lista = [ 1, 2, 3, 4, 5, 0 ]

final = len(lista) - 1
temp = lista[final]
for i in range(final, 0, -1):
    lista[i] = lista[i - 1]
lista[0] = temp

print(lista)

The output of the program will be the ordered list:

[0, 1, 2, 3, 4, 5]

Shift left

This case is similar to the previous one, but now we want to move the data in the opposite direction.

In the following example we need to move the first element of a list to the end of the list to sort all the numbers:

lista = [ 5, 0, 1, 2, 3, 4 ]
#         └---------------^

This operation involves moving all the numbers to the left to leave a free space at the end of the list, which is where we will place the number 5.

The final program will be the following:

lista = [ 5, 0, 1, 2, 3, 4 ]

final = len(lista) - 1
temp = lista[0]
for i in range(final):
    lista[i] = lista[i + 1]
lista[final] = temp

print(lista)

The output of the program will be the ordered list:

[0, 1, 2, 3, 4, 5]

Exercises

  1. Write a program that exchanges the data of the following variables so that each variable contains the number that corresponds to its name. Remember to use a temporary variable:

    uno = 2
    dos = 1
    ...
    ...
    
    print(uno, dos)
    

    Exit:

    1 2
    
  2. Write a program that exchanges the data of the following variables so that each variable contains the number that corresponds to its name. Remember to use a temporary variable:

    uno = 3
    dos = 1
    tres = 2
    
    ...
    ...
    
    print(uno, dos, tres)
    

    Exit:

    1 2 3
    
  3. Write a function that shifts all elements of a list to the left. The first element should disappear and the last element will be assigned the second parameter of the function.

    Example:

    lista = [0, 1, 2, 3, 4, 5]
    desplaza_izquierda(lista, 7)
    print(lista)
    

    Exit:

    [1, 2, 3, 4, 5, 7]
    
  4. Write a function that shifts the elements of a list to the right, but only to a position given by its second parameter, the variable min. The last element in the list must be placed at the min position. Check that the function works correctly with the following examples.

    Example 1:

    lista = [0, 1, 2, 3, 4, 5]
    desplaza_derecha(lista, 3)
    print(lista)
    

    Exit:

    [0, 1, 2, 5, 3, 4]
    

    Example 2:

    lista = [0, 1, 2, 3, 4, 5]
    desplaza_derecha(lista, 1)
    print(lista)
    

    Exit:

    [0, 5, 1, 2, 3, 4]
    
  5. Write a function that shifts the elements of a list to the left, to the element given by its second parameter, the variable min. Once the move is made, the last element in the list must be assigned to the value that was at the min position.

    Example:

    lista = ['a', 'b', 'c', 'd', 'e', 'f']
    desplaza_izquierda(lista, 1)
    print(lista)
    

    Exit:

    ['a', 'c', 'd', 'e', 'f', 'b']