Skip to main content

Insertion Sort


  Insertion sort is like picking a card from a set of unsorted cards and comparing it
  to all the cards present on the left for higher or smaller values and inserting this
  card into right position. After every pass, the picked value reaches at its right position.
 
  for e.g.
  input : 10,5,3,4,12,1,13
  after First Pass :
  -> 5,10,3,4,12,1,13
  after Second Pass:
  -> 3,5,10,4,12,1,13
  after Third Pass:
  -> 3,4,5,10,12,1,13
  in fourth pass when the pointer is pointing to 12 there is not need to move it since
  it is already greater than the immediate next element i.e. 10:
  -> 3,4,5,10,12,1,13
  after fifth Pass:
  -> 1,3,4,5,10,12,13
  in sixth pass also there is no need to move the elements
  and you get your output
 
  It's time complexity is O(n^2) but still it performance much faster than bubble sort and
  slightly better than selection sort.
 
  Below is the code for same.


public class InsertionSort {

public static void main(String[] args) {
int[] arr = {10,5,3,4,12,1,13,2,50,1,11,6,9,0,12,3,87,27};
int temp = 0;
int k=0;
for(int i=1;i< arr.length;i++){
k= i-1;
if(arr[k]>arr[i]){
temp = arr[i];
while(k>=0 && arr[k]> temp){
arr[k+1] = arr[k];
k--;
}
arr[k+1] = temp;
}

}
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}

}










Comments

Popular posts from this blog

Create Table in Liquibase

For creating table using liquibase you can use below code and add it in your liquibase file. <createTable tableName=“employee”>      <column name="id" type="int">      <constraints primaryKey="true" nullable="false"/>   </column>      <column name="first_name" type="varchar(255)"/>   <column name="last_name" type="varchar(255)"/>   <column name="username" type="varchar(255)">      <constraints unique="true" nullable="false"/>   </column> </createTable> The use is pretty simple it's the way it looks : Tag: <createTable></createTable> This is an opening/ending tag for creating a table. These tags will enclose column sub tags which will define columns for the table. Attribute:   tableName : Name of the table which you want to create. (This is a mandatory  

ReferenceError: dhtmlXGrid is not defined : Resolved

For the errors like : ReferenceError: dhtmlXDataView is not defined ReferenceError: dhtmlXGrid is not defined ReferenceError: dhtmlXTree is not defined ReferenceError: dhtmlXTreeGrid is not defined etc I have been working on dhtmlx for long time now and this is one of the most basic exception faced by the developer and which in fact is very easy to resolve. Reasons: 1. You are actually referring to a wrong location of the JS file.    - This is a very common mistake done and most of the time we are so damn sure that we don't even care about checking the path once. Even though you have copied it from your existing project where it is working, if you face this issue don't forget to check the path once, it won't harm you. 2. Check for relative path.    - So now you have copied it and paste it in your new file where you are going to use the dhtmlx component and when you open your file you get this error and it becomes frustrating knowing that same thing is wo

How databasechangeloglock and databasechangelog table used by liquibase?

Liquibase takes care of executing the query on the database while maintaining the list of queries executed and also maintaining the locks over the tables simultaneously. The two tables that are used by the liquibase for this purpose are : Databasechangeloglock : This table have following columns ID | LOCKED| LOCKGRANTED | LOCKEDBY. This maintains the locks information granted to the user. The primary purpose of this table is to make sure that two machines don't attempt to modify the data at the same time. Databasechangelog : This table have following columns ID | AUTHOR | FILENAME | DATEEXECUTED | ORDEREXECUTED | EXECTYPE | MD5SUM | DESCRIPTION | COMMENTS | TAG | LIQUIBASE This table maintains the list of the statements that are executed on the database.