Insertion Sort | Practice | GeeksforGeeks

The task is to complete the insert() function which is used to implement Insertion Sort. 

Example 1:
Input:
N = 5
arr[] = { 4, 1, 3, 9, 7}
Output:
1 3 4 7 9

Example 2:
Input:
N = 10
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Output:
1 2 3 4 5 6 7 8 9 10

Expected Time Complexity: O(N*N).
Expected Auxiliary Space: O(1).

Constraints:
1 <= N <= 1000
1 <= arr[i] <= 1000
0
koulikmaity1 day ago
void insertionSort(int arr[], int n)   {       for(int i=1; i<n; i++)       {           int key = arr[i];           int j = i-1;           while(arr[j] > key && j>=0)           {               arr[j+1] = arr[j];               j--;           }           arr[j+1] = key;       }   }
0
futureprogrammer283 days ago
public void insertionSort(int arr[], int n)
  {
      //code 
       for(int i=1;i<arr.length;i++){
            int ele=arr[i];
            int j=i-1;
            while(j>=0 && arr[j]>ele){
                arr[j+1]=arr[j];
                j--;
            }
            arr[j+1]=ele;
        }
  }
0
vikaschandra8051101 week ago
 void insertionSort(int arr[], int n)   {       //code here       for(int i=1; i<n; i++){           int current=arr[i];           int j;           for( j=i-1; j>=0; j--){               if(arr[j]>current){                   arr[j+1]=arr[j];               }               else                   break;           }           arr[j+1]=current;       }                 }
0
haulya1 week ago
void insertionSort(int arr[], int n){ //code here  for(int i=1; i < n; i++){     int j = i-1;     int key = arr[i];          while (arr[j] > key && j >= 0){         arr[j+1] = arr[j];         j--;     }     arr[j+1] = key; }}
0
haulya
This comment was deleted.
0
himanshu567843 weeks ago
  void insertionSort(int x[], int n)   {   int i,j,value;   for(i=1;i<n;i++)   {   value=x[i];   for(j=i;j>0 && x[j-1]>value;j--)   {       x[j]=x[j-1];   }   x[j]=value;   }   } 
0
nagapavaneswarreddy27033 weeks ago
class Solution{   public:      void insert(int arr[], int i)   {       cout<<"please enter the no of elements";       cin>>i;       cout<<"please enter the elements";       for(int j=0;j<i;j++)       {cin>>arr[j];}       cout<<"the order of sorted elements are"<<insertionSort(arr,i);   }   //Function to sort the array using insertion sort algorithm.   public:   void insertionSort(int arr[], int n)   { int i;       for(i=2;i<n;i++)       {int j=i-1;          int key;          key=arr[i];          while(j>0&&arr[j]>key)          {arr[j+1]=arr[j];           j=j-1;}           arr[i+1]=key;       }   }; 
0
mdubaid203 weeks ago
class Solution:   def insertionSort(self, arr, n):       #code here       for i in range(n-1):           if arr[i+1]<arr[i]:                              key = arr[i+1]               for j in range(i+1):                   if key < arr[j]:                       min = j                       break                              k = i+1                              while min<k<=i+1:                   arr[k] = arr[k-1]                   k = k-1               arr[min] = key       return arr       
0
hritikpatani3 weeks ago
void insertionSort(int arr[], int n)   {       int temp=0;    for(int i=1;i<n;i++){     int k = i;     for(int j=i-1;j>=0;j--){      if(arr[k] < arr[j]){       temp = arr[k];                   arr[k] = arr[j];                   arr[j] = temp;                   k--;      }     }    }   }
0
demishassabis4 weeks ago
class Solution
{
    public:
    void insert(int arr[], int i)
    {
        //NO NEED TO CODE THIS
    }
    
    public:
    //Function to sort the array using insertion sort algorithm.
    void insertionSort(int arr[], int n)
    {
        for(int j=1; j<n; j++)
        {
            int i = j-1;
            int key = arr[j];
            while(i>=0 and arr[i]>key)
            {
                arr[i+1] = arr[i];
                i--;
            }
            arr[i+1] = key;
        }
    }
};

 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.