Skip to main content

Overview of time and space complexity for sorting algorithm


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).

Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.
     Time Complexity for selection sort also is O(n^2).
     space complexity for bubble sort will be O(1).

Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper position.
   Time Complexity for Insertion sort also is O(n^2).
   space complexity for bubble sort will be O(1).

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.

Quick sort : In quick sort, we will consider a pivot element and divide the array so that all the lesser element will be in left side of pivot element while all the greater element will be in right side.
   This process we will continue recursively until array is fully sorted.
     Time complexity will beO(nlogn) but in worst case when elements are in sorted order time complexity will become O(n^2) as in each call only one element will be consider as sorted and for rest again we need to follow same process.
     space complexity will be O(1)
   
Please have a look to the  source code of all sorting method from below link.

Comments

Popular posts from this blog

JAVA 8 FUNTIONAL INTERFACE

JAVA 8 FUNTIONAL INTERFACE  Funtional Interface :     An interface which has only one abstract method can be called a funtional interface. Comparable , Runnable , Callable all these interfaces has only one abstract method and can be consider as funtional interface.    How it works :        Once we will create a interface with one abstract method then java internally predicates the input type and based on the Interface reference it apply the logic mentioned after lambda expression    lets consider we have created an interface as below @FunctionalInterface public interface FuntionalExample { public int cal ( int a , int b) ; } And below class to test our funtional Interface. public class FuntionalInterfaceExample { public static void main (String args[]) { FuntionalExample addition=( int a , int b) -> a+b ; FuntionalExample subtraction=( int a , int b) -> a-b ; FuntionalExample multipl...

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control Java flight recording(JFR) help us to analyse and find the root cause of any slowness in our program along with CPU usage , hot methods and garbage collection , profiling etc. To visualise we need to feed .jfr file to JDK mission control present in JDK bin folder. After successful compilation , we should run the program with below option which will generate the .jfr and feed to mission control.   command :  j ava -XX:+UnlockCommercialFeatures -XX:+FlightRecorder  -XX:StartFlightRecording=duration=200s,filename=flight.jfr -cp ./out/ path-and-class-name Below are some example to understand how this JFR can be helpful. 1. Lets consider we have created a java program in which we have used LinkList to store the elements and in same program we are using contains method inside a for loop of 1 million , in this case each time this contains method will be called then 1 million records will be sc...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...