What Is Sorting & Bubble Sort In JAVA

Introduction

 
In today's article, we discuss what sorting is and discuss the bubble sort in detail with an example in Java.
 

What is Sorting?

 
Sorting is the process of putting a list or a group of items in a specific order. Some common sorting criteria are: alphabetical or numerical. Sorting can also be done in ascending order (A-Z) or descending order (Z-A). Sorting refers to ordering data in an increasing or decreasing fashion according to some linear relationship among the data items. Sorting can be done on names, numbers and records. That is, sorting greatly improves the efficiency of searching.
 

Importance of Sorting

 
To look up the word in a dictionary: This data are in a sorted manner. You do a few quick searches until you find the page, and then finally scan the page for the word. Now imagine the words are not sorted. Now you need more time to search the data because the time to search the unsorted data would be prohibitive, and you'd be right!
 
You could apply the same principle to many other types of data in the real world, such as finding names in books on the shelves of a library or the phone book.
 
Sorting is of various types. Some of the main sorting algorithms are:
  1. Insertion sort
  2. Merge Sort
  3. Quick Sort
  4. Shell Sort
  5. Radix Sort
  6. Heap Sort
  7. Bucket Sort
  8. Selection sort
  9. Bubble sort

Bubble Sort

 
Bubble sort is one of the classic sorting algorithms for sorting, taught in various computer and engineering courses. In the Bubble sort algorithm, we sort an unsorted array by starting from the first element and comparing with adjacent elements. If the former is greater than the latter then we swap and by doing this we get the largest number at the end after the first iteration. So in order to sort n elements, you require n-1 iterations and nearly n-1 comparisons. To recap, here is the logic for a bubble sort sorting algorithm. Because of its algorithmic nature and simplicity, it's often used in various Java and C exercises.  Since algorithmic questions are always a tricky question and not easy to code, I have seen many developers fumble if asked to code it on the spot. That's why it's advisable to do logical and algorithmic programming when learning programming and OOP to get this skill of converting logic into code.
 
The following procedure shows how the data is sorted in a Bubble Sort:
1. start comparing i[0] to i[1]
2. if i[0] > i[1] then swap numbers
3. compare i[1] to i[2] and repeat until you have compared the last pair
4. This is referred to as one pass and at the end of the first pass the largest number is at the last
5. repeat this comparison again starting from i[0] but this time proceed until the second to the last pair only
 
Example 
  1. public class BubbleSortEx  
  2. {  
  3.      public static void main(String a[])  
  4.      {  
  5.         //Numbers which are to be sorted  
  6.         int n[] = {124,23,43,12,177,25,2,1,67};  
  7.          //Displays the numbers before sorting  
  8.         System.out.print("Before sorting, numbers are ");  
  9.         for(int i = 0; i < n.length; i++)  
  10.          {  
  11.             System.out.print(n[i]+" ");  
  12.          }  
  13.          System.out.println();  
  14.          //Sorting in ascending order using bubble sort  
  15.         initializebubbleSort(n);  
  16.          //Displaying the numbers after sorting  
  17.         System.out.print("After sorting, numbers are ");  
  18.         for(int i = 0; i < n.length; i++)  
  19.          {  
  20.             System.out.print(n[i]+" ");  
  21.          }  
  22.       }  
  23.       //This method sorts the input array in asecnding order  
  24.      public static void initializebubbleSort( int n[])  
  25.      {  
  26.         int temp,i,j;  
  27.          for(i = 0; i < n.length; i++)  
  28.          {  
  29.             for(j = 1; j < (n.length -i); j++)  
  30.              {  
  31.                 //if n[j-1] > n[j], swap the elements  
  32.                 if(n[j-1] > n[j])  
  33.                  {  
  34.                     temp = n[j-1];  
  35.                     n[j-1]=n[j];  
  36.                     n[j]=temp;  
  37.                  }  
  38.              }  
  39.          }  
  40.      }  
  41.  }   
Output
bubblesort.jpg


Similar Articles