Skip to content

What is the role of the temp integer in the bubble sort code

int[] array = {4, 5, 7, 6, 9, 10,67,6,45};
for (int i = 0; i < array.length; i++) {
    for (int j = i+1; j < array.length; j++) {
        if (array[i] > array[j]) {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;

Just wondering what is the role of array[j] = temp; This is the bubble sort sorting algorithm, new to Stack exchange so formatting might not be great



Imagine that you have a red ball in your left hand, and a blue ball your right.

Now switch the balls … without throwing them in the air.

How? Let me lend you a hand!

Explanation: 1) You need 3 hands to do this … and by analogy, 3 variables. 2) “To lend a hand” means “to help”

User contributions licensed under: CC BY-SA
3 People found this is helpful