We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Sorting
  4. Insertion Sort - Part 2
  5. Discussions

Insertion Sort - Part 2

Problem
Submissions
Leaderboard
Discussions

Sort 498 Discussions, By:

recency

Please Login in order to post a comment

  • shreyasmohite786
    1 week ago+ 0 comments

    Python Insertion Simple Solution

           for i in range(1,n):
            key=arr[i]
            j=i-1
            while j>=0 and key<arr[j]:
                arr[j+1]=arr[j]
                j-=1
            arr[j+1]=key
            print(*arr)
    
    0|
    Permalink
  • ozgurakduran
    1 month ago+ 0 comments

    Python:

    def insertionSort2(n, arr):
        for i in range(1, n):
            j = i-1
            while j >= 0:    
                if arr[j+1] < arr[j]:
                    temp = arr[j+1]
                    arr[j+1] = arr[j]
                    arr[j] = temp
                j -= 1
            print(*arr)            
    
    0|
    Permalink
  • nisalchandirade
    1 month ago+ 0 comments

    C++ solution

    void insertionSort2(int n, vector arr) {

    for(int i=1; i<n; i++){
    
        for(int j=i-1; j>=0;j--){
    
            if(arr[j+1]<arr[j]){
                int x = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = x;                      
            }
        }
    
        for(int l = 0; l<n ; l++){
            cout<<arr[l]<<" ";
        }
        cout<<endl;
    }
    

    }

    0|
    Permalink
  • rcanbabaoglu
    1 month ago+ 0 comments

    Swift

    func insertionSort2(n: Int, arr: [Int]) -> Void {
        var sortArr: [Int] = arr
        
        for i in 1..<sortArr.count {
            for j in stride(from: i, through: 0, by: -1) {
                if arr[i] < sortArr[j] {
                    sortArr[j+1] = sortArr[j]
                    sortArr[j] = arr[i]
                } else {
                    continue
                }
            } 
            printer(sortArr)
        }
    }
    func printer(_ arr: [Int]) {
        print(arr.map({ String($0)}).joined(separator: " ")) 
    }
    
    0|
    Permalink
  • mallachandu99
    1 month ago+ 1 comment

    JavaScript Solution

    function insertionSort2(n, arr) {
    // Write your code here
    
    for(let i=0; i<n-1; i++){
    // From here, This block of code performs Insertion Sort-1
        if(arr[i+1] < arr[i]){  
    
            var val = arr[i+1],
                j = i;
    
            while(arr[j] > val){
                 arr[j+1] = arr[j];
                j--;
            }
            arr[j+1] = val;
    
        } // End of insertion sort - 1
        console.log(...arr)
    }
    

    }

    0|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy