Insertion Sort - Part 1

Sort by

recency

|

888 Discussions

|

  • + 0 comments

    JAVA int key = arr.get(n - 1); int j = n - 2;

        while (j >= 0 && arr.get(j) > key) {
            arr.set(j + 1, arr.get(j)); 
            j--; 
            for (int k = 0; k < arr.size(); k++) {
            System.out.print(arr.get(k) + " ");
        }
        System.out.println();
        }
    
        arr.set(j + 1, key);
    
          for(int i=0;i<arr.size();i++) {
         System.out.print(arr.get(i) + " ");
     }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanatoin here : https://youtu.be/xFqneyHR96g

    void printArr(vector<int> arr){
        for(int i = 0; i < arr.size(); i++){cout << arr[i] << " ";}
        cout << endl;
    }
    void insertionSort1(int n, vector<int> arr) {
        int last = arr[arr.size()-1];
        for(int i = arr.size() - 1; i >= 0; i--){
            if(last < arr[i-1]) {
                arr[i] = arr[i-1];
                printArr(arr);
            }
            else{
                arr[i] = last;
                printArr(arr);
                break;
            }
        }
    }
    
  • + 0 comments

    C# Solution

    public static void insertionSort1(int n, List<int> arr)
    {
        var last = arr[n - 1];
    
        for (int i = n - 1; i >= 0; i--)
        {
            if (i == 0 || last > arr[i - 1])
            {
                arr[i] = last;
                Console.WriteLine(string.Join(" ", arr));
                break;
            }
            else
            {
                arr[i] = arr[i - 1];
                Console.WriteLine(string.Join(" ", arr));
            }
        }
    }
    
  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'insertionSort1' function below.
    #
    # The function accepts following parameters:
    #  1. INTEGER n
    #  2. INTEGER_ARRAY arr
    #
    
    def insertionSort1(n, arr):
        # Write your code here
        for i in range(n):
            j = i
            curr = arr[i]
            if curr < arr[j-1]:
                while j > 0 and curr < arr[j-1]:
                    arr[j]= arr[j-1]
                    j-=1
                    print(*arr)
                
                if j != i:
                    arr[j] = curr
                    print(*arr)
            
                
    
    if __name__ == '__main__':
        n = int(input().strip())
    
        arr = list(map(int, input().rstrip().split()))
    
        insertionSort1(n, arr)
    
  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'insertionSort1' function below.

    #

    The function accepts following parameters:

    1. INTEGER n

    2. INTEGER_ARRAY arr

    #

    def insertionSort1(n, arr): # Write your code here for i in range(n): j = i curr = arr[i] if curr < arr[j-1]: while j > 0 and curr < arr[j-1]: arr[j]= arr[j-1] j-=1 print(*arr)

            if j != i:
                arr[j] = curr
                print(*arr)
    

    if name == 'main': n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))
    
    insertionSort1(n, arr)