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 ...

Get Hands-On Automation Testing with Java for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.