Swapping variables with/without the temp variable

In this exercise, we will be swapping the location of the elements in a simple array and placing them in ascending order.

To do this, we first need to understand the logic of how it will work. Let's take an example to explain this.

We initiate the a array and declare values in it, as shown in the following code:

int a[]= {2,6,1,4,9};

We can use the bubble-sort mechanism to compare the variables to each other and then place them in the order we want. For the preceding example, the way the logic works will be as follows; we'll compare 2 with 6, 2 with 1, 2 with 4, and 2 with 9. The smallest number after this comparison is 1 and we swap its position with the first index, which is 2. So after swapping, 1 will be the new first index. This means that 1 is the smallest number from the values given in the array. Now we move to the second index, we leave the first index untouched because we have already compared and declared 1 as the fixed first index as it is the smallest number in the array. Now we take the value 6, which is the second index and compare it to other values present in the array. First we compare 6 and 2 and since 2 is smaller than 6 we swap their positions, so 2 is the new first index and 6 is the second index. We then compare 2 with 3; basically we are comparing the first index with all the other values in the array. We then compare 2 to 3, 2 to 4, and 2 to 9; here 2 is the smallest number. So 2 becomes our fixed second index in the array. Now we are left with four values that we need to sort. We again compare 6 with the other values. 6 is smaller than 3, so we swap the positions of 6 and 3. This makes 3 the third index in the array and we compare 3 with the other numbers, 3 is the smallest among all the value given in it. So 3 becomes our fixed third index in the array. Then we perform the same thing for the last three values and conclude that the final arrangement will be 1, 2, 3, 4, 6, 9. Now we need to apply this logic in a Java program and print it.

We will decide on an algorithm for our logic and, based on the algorithm, we will design our code step by step. We will write an outer for loop that moves one index and compares it with the rest.

We write an outer for loop and set the condition to not cross the length of the array; here the array size is 5, so the condition is set to i, which is less than 5. If i is 0, the variable value will compare it to the first, second, third, and fourth variables. If i is 2, the variable will compare it to the third and fourth variables. So whatever the i index is, it should start comparing the value of i with its next index. For this, we will create an inner for loop and we will initialize the j to be always one number more than i, i plus 1, because we will compare it with the next index. So, if i equals 0, j will be 1. So the zero index will start comparing from the first index. And we compare it until the end of the array, so we set the limit for the inner for loop at j, as it's less than the length of the array, which is 5 in this example.

We then add an if loop within the inner for loop. This loop will do the comparison between the indexes and swap the values when the condition is met. Once the first round of comparisons is done, the controller exits the inner for loop and goes back to the outer for loop, which is when the smallest number is picked after the comparison, pushed to the corner, and the index moves to the next value.

Now we go back inside the if loop and write code to swap the values when the comparison condition is true. To swap variable values, we need to declare a temp variable and assign the a[i] number to temp. We add the following code to successfully swap the variables:

temp=a[i];
a[i]=a[j];
a[j]=temp;

And in the end, we add a print statement to display the final array after comparing and rearranging the values.

The final output will be displayed as follows:

1
2
4
6
9
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.144.32