Skip to main content

Bubble sort Implementation

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.
   So time complexity for bubble sort will be O(n^2).
        space complexity for bubble sort will be O(1).


// Bubble Sort
class BubbleSort {

public static void sort(int[] array) {
int n = array.length;
while (true) {
boolean swapped = false;
for (int i = 0; i < n - 1; i++) {
if (array[i + 1] < array[i]) {
swap(array, i, i + 1);
swapped = true;
}
}
if (!swapped) break;
}
}

private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

private static void print(int[] A) {
System.out.print("[");
for (int i = 0; i < A.length; i++) {
System.out.print(A[i]);
if (i < A.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}

public static void main(String[] args) {
int[] A = {111, 80, 14, 21, 19, 31, 19, 121, 10, 190, 80, 6, 7};
sort(A);
print(A);
}

}

Please have a look to the  source code of all sorting method from below link
  1. BUBBLE SORT PROGRAM
  2. SELECTION SORT PROGRAM
  3. INSERTION SORT PROGRAM
  4. MERGE SORT PROGRAM
  5. QUICK SORT PROGRAM

Comments

Popular posts from this blog

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; Str...

SECURITY IN SPRING BOOT : Authentication and Authorisation in Spring boot Application

SECURITY IN SPRING BOOT : Authentication and Authorisation in Spring boot Application In any application security is the most important aspect without this no application can be considered as standard application as it will have full exposure to hackers. Now to secure our application there are 2 important concept  Authentication and Authorisation  which we should know .     Authentication stands for authentication of the client from your request is coming and to do so we will receive unique username and password for that to validate the authenticity of client. If it passes through our authentication process and identified as valid client then we will proceed and allow to access our URI and resources.   Authorisation comes after authentication in which we will allow to access our resources based on type of client if client is an Admin then he will have full access , if client is Premium user then he will have access to all premium resources if client is...

Merge sort Implementation

  Merge sort   : In merge sort we will divide the array from middle recursively  until we have only one element in sub-array. After that we will merge sub-array in ascending order.     Time complexity for merge sort will be O(nlogn) as we are splitting the array from middle so each time it's getting half so for that  it will be log(n) and while merging we are traversing the whole array so for that it will be O(n).      So after combining both  time complexity will be O(n)+O(logn)= O(nlogn)      space complexity will be O(n) as we need a extra same size array to hold the unsorted array. class MergeSort { public static void sort ( int [] array) { int [] aux = new int [array. length ] ; sort (array , aux , 0 , array. length - 1 ) ; } private static void sort ( int [] array , int [] aux , int lo , int hi) { if (hi <= lo) return; int mid = (lo + hi) / 2 ; sort (arra...