• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Test Cases
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Different Sorting

Different Sorting

Last Updated on: September 30, 2018 By Softwaretestingo Editorial Board

Different Sorting


  • 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*/

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2022 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers