Sorting In Data Structure

Sorting In Data Structure


  • Bubble Sort.
  • Insertion Sort.
  • Selection Sort.

Bubble Sort:

// Java program for implementation of Bubble Sort 
class BubbleSort 
{ 
   void bubbleSort(int arr[]) 
   { 
      int n = arr.length; 
      for (int i = 0; i < n-1; i++) 
         for (int j = 0; j < n-i-1; j++) 
            if (arr[j] > arr[j+1]) 
            { 
               // swap arr[j+1] and arr[i] 
               int temp = arr[j]; 
               arr[j] = arr[j+1]; 
               arr[j+1] = temp; 
            } 
   } 

   /* Prints the array */
   void printArray(int arr[]) 
   { 
      int n = arr.length; 
      for (int i=0; i<n; ++i) 
         System.out.print(arr[i] + " "); 
      System.out.println(); 
   } 

   // Driver method to test above 
   public static void main(String args[]) 
   { 
      BubbleSort ob = new BubbleSort(); 
      int arr[] = {64, 34, 25, 12, 22, 11, 90}; 
      ob.bubbleSort(arr); 
      System.out.println("Sorted array"); 
      ob.printArray(arr); 
   } 
} 
/* This code is contributed by Rajat Mishra */

Selection Sort:

// Java program for implementation of Selection Sort 
class SelectionSort 
{ 
   void sort(int arr[]) 
   { 
      int n = arr.length; 

      // One by one move boundary of unsorted subarray 
      for (int i = 0; i < n-1; i++) 
      { 
         // Find the minimum element in unsorted array 
         int min_idx = i; 
         for (int j = i+1; j < n; j++) 
            if (arr[j] < arr[min_idx]) 
               min_idx = j; 

         // Swap the found minimum element with the first 
         // element 
         int temp = arr[min_idx]; 
         arr[min_idx] = arr[i]; 
         arr[i] = temp; 
      } 
   } 

   // Prints the array 
   void printArray(int arr[]) 
   { 
      int n = arr.length; 
      for (int i=0; i<n; ++i) 
         System.out.print(arr[i]+" "); 
      System.out.println(); 
   } 

   // Driver code to test above 
   public static void main(String args[]) 
   { 
      SelectionSort ob = new SelectionSort(); 
      int arr[] = {64,25,12,22,11}; 
      ob.sort(arr); 
      System.out.println("Sorted array"); 
      ob.printArray(arr); 
   } 
} 
/* This code is contributed by Rajat Mishra*/

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment