Sorting is a real-life technique used in various places, whether it is in an online or offline platform. For example, on the Amazon website, when we want to buy the cheapest product, we sort all the options according to the price. Let us know more detail about ‘Sorting An Array In Java’.
Sorting An Array In Java
Sorting is a technique that helps us to arrange things in a particular order (ascending or descending). It depends on the condition. Like in the above example, if we want to buy the most expensive product of that category, we select the price range in descending order. It can be done in any language.
Sorting An Array In Java is of so many types:
- Bubble Sort
- Selection Sort
- Insertion sort
1. Bubble Sort:
Now it’s time to understand this technique with an example. If we see some water bubbles in real life, then the biggest bubble (which contains the maximum amount of water in it) falls down first. The second-largest bubble comes down after that, then the third last, and so on.
So this is the phenomenon on which this sorting technique is based. Basically, the name “Bubble sort” is derived from this thing only.
Here we compare two consecutive numbers with each other, and the largest of those two will go behind, and this phenomenon will go on and on. In the end, we will find a sorted array. This can sound a little bit confusing, so that’s why you can refer to the diagram below:
I took an array with unsorted data.
- Here 3 will be compared to 7
- 7 is bigger than 3, so no change
- Then 7 will be compared to 2
- 2 is shorter than 7 so they will exchange their position
In this way, 8 will go to the end, then comes 7, then 3, then 2, then 1.
You can see the diagram below:
So, as we can see our array is sorted.
2. Selection Sort:
This technique is similar to the bubble sort discussed above, except for a bit of change. We select the shortest number in the array and push it into the forwarding block. Again, we use a loop in this process and use the pneumonia of one sort per iteration.
This can sound confusing, but we can understand it with an example.
Consider the array mentioned above:
- Here what we do is, in one iteration, we compare 3 with 7.
- 3 is not bigger than 7, so there will be no change.
- Now 3 is compared to 2.
- It is bigger than 2.
- So 2 will be considered as smallest in this case.
- Now 2 is compared with 8.
- It is not bigger than 8, so no change.
- Now 2 will be compared to 1.
- It is bigger than 2, so they will swap their positions.
In this way, these iterations will go on ,and the array will be sorted in ascending order.
This is also another type of sorting method.
3. Insertion Sort:
In this technique, we divide the array into two parts.
- Sorted part
- Unsorted part
Now, we pick up one element from the unsorted part and place that element into the sorted part in its right position. Then gradually the whole array will get sorted.
Let’s understand it with the help of an example.
Consider the above array again:
Now let’s assume the sorted part is only of one element whose index number is 0, i.e., 3, and all the other elements are of unsorted parts.
Now we will compare 1st element of the unsorted part, which is 7, with 3, which is the element of the sorted part. As we can see, 7 is bigger than 3, so it will be placed after 3.
Now we have an array that has two elements in the sorted part.
Now we will compare 2 of those elements and put them in the correct position.
Now we will compare 2 with the numbers of sorted part arrays, i.e., 3 and 7. As we can see, 2 is shorter than both these elements, so we will place 2 in front of both 3 and 7.
Now the array will become like this:
As we can see now, the sorted part of the array has 3 elements, and the unsorted part has 2 elements.
If we talk about the other step, then now we have to compare 8 with the three elements (2, 3, 7) of the sorted part of the array.
As we can see, 8 is the biggest of all three, so 8 will be put after 7.
In the end, 1 is left, so when 1 is compared with all the elements of the sorted part of the array, all the elements will be sorted in ascending order.
As we can see here:
This is called insertion sort.
In this way, we will get a sorted array.
Now we have learnt ‘Sorting An Array In Java’, In the end, there are lots of methods that can be discussed but these are the two major techniques that are to be discussed and should be known by all the programmers because these methods are so easy and so much beneficial.
Frequently asked questions in an interview:
Q. What is the procedure of bubble sort?
Ans. You can refer to the method mentioned above.
Q. Which sorting method is the best?
Ans. Bubble Sort is the best technique of all. As we can see above, it is the simplest and easy to code technique.
Q. How can we say that bubble sort is the best method?
Ans. Because in bubble sort, the coding is effortless and short, and time complexity is also less, so it requires less time to execute, and hence our application will execute very fast.