Sort Data in Java


Bubble Sort in Java

We can create a java program to sort array elements using bubble sort. Bubble sort algorithm is known as the simplest sorting algorithm.
In bubble sort algorithm, array is traversed from first element to last element. Here, current element is compared with the next element. If current element is greater than the next element,
it is swapped.
  1. public class BubbleSortExample {  
  1.     static void bubbleSort(int[] arr) {  
  1.         int n = arr.length;  
  1.         int temp = 0;  
  1.          for(int i=0; i < n; i++){  
  1.                  for(int j=1; j < (n-i); j++){  
  1.                           if(arr[j-1] > arr[j]){  
  1.                                  //swap elements  
  1.                                  temp = arr[j-1];  
  1.                                  arr[j-1] = arr[j];  
  1.                                  arr[j] = temp;  
  1.                          }  
  1.                           
  1.                  }  
  1.          }  
  1.   
  1.     }  
  1.     public static void main(String[] args) {  
  1.                 int arr[] ={3,60,35,2,45,320,5};  
  1.                  
  1.                 System.out.println("Array Before Bubble Sort");  
  1.                 for(int i=0; i < arr.length; i++){  
  1.                         System.out.print(arr[i] + " ");  
  1.                 }  
  1.                 System.out.println();  
  1.                   
  1.                 bubbleSort(arr);//sorting array elements using bubble sort  
  1.                  
  1.                 System.out.println("Array After Bubble Sort");  
  1.                 for(int i=0; i < arr.length; i++){  
  1.                         System.out.print(arr[i] + " ");  
  1.                 }  
  1.    
  1.         }  
  1. }  
Output:

Array Before Bubble Sort
3 60 35 2 45 320 5 Array After Bubble Sort 2 3 5 35 45 60 320  


Selection Sort in Java


  1. public class SelectionSortExample {  
  2.     public static void selectionSort(int[] arr){  
  3.         for (int i = 0; i < arr.length - 1; i++)  
  4.         {  
  5.             int index = i;  
  6.             for (int j = i + 1; j < arr.length; j++){  
  7.                 if (arr[j] < arr[index]){  
  8.                     index = j;//searching for lowest index  
  9.                 }  
  10.             }  
  11.             int smallerNumber = arr[index];   
  12.             arr[index] = arr[i];  
  13.             arr[i] = smallerNumber;  
  14.         }  
  15.     }  
  16.        
  17.     public static void main(String a[]){  
  18.         int[] arr1 = {9,14,3,2,43,11,58,22};  
  19.         System.out.println("Before Selection Sort");  
  20.         for(int i:arr1){  
  21.             System.out.print(i+" ");  
  22.         }  
  23.         System.out.println();  
  24.           
  25.         selectionSort(arr1);//sorting array using selection sort  
  26.          
  27.         System.out.println("After Selection Sort");  
  28.         for(int i:arr1){  
  29.             System.out.print(i+" ");  
  30.         }  
  31.     }  
  32. }  
Output:

Before Selection Sort 9 14 3 2 43 11 58 22 
After Selection Sort 2 3 9 11 14 22 43 58
N

No comments:

Post a Comment

Floyd Triangle

Note- Floyd Triangle is like 1 2 3 4 5 6 7 8 9 10 ------------ import java.util.Scanner; public class FloydTria...

Popular Post