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 1
  5. Discussions

Insertion Sort - Part 1

Problem
Submissions
Leaderboard
Discussions

Sort 805 Discussions, By:

recency

Please Login in order to post a comment

  • ngvanbinh1905
    6 days ago+ 0 comments

    Javascript:

    function insertionSort1(n, arr) {
        // Write your code here
        const point = arr[n-1]
        for (let i = n-1; i>= 0; i--) {
            if(arr[i-1] > point) {
                arr[i] = arr[i-1];
                console.log(...arr);
            }
            else {
                arr[i] = point;
                console.log(...arr);
                break;
            }
        }
    }
    
    0|
    Permalink
  • andrey1981spb
    2 weeks ago+ 0 comments

    Java 8 solution

         public static void insertionSort1(int n, List<Integer> arr) {
    
               int cur = arr.get(n - 1);
    
             int i = n - 1;
    
            while (i >= 0) {
                int next = (i != 0) ? arr.get(i - 1) : cur;
                if (cur < next) {
                    arr.set(i, next);
                    System.out.println(arr.toString().replaceAll("[\\[\\],]", ""));
                    i--;
                } else {
                    arr.set(i, cur);
                    break;
                }
            }
            System.out.println(arr.toString().replaceAll("[\\[\\],]", ""));
    }
    
    0|
    Permalink
  • shreyasmohite786
    2 weeks ago+ 0 comments

    Python Insertion Sort

        for i in range(n):
            key=arr[i]
            j=i-1
            while j>=0 and key<arr[j]:
                arr[j+1]=arr[j]
                print(*arr)
                j-=1
            arr[j+1]=key
        print(*arr)
    
    0|
    Permalink
  • nur_sherimbek
    2 weeks ago+ 0 comments
    def insertionSort1(n, arr):
        # Write your code here
        stored = arr[-1]
        for i in range(len(arr)-1,-1,-1):
            if arr[i-1] > stored and i != 0:
                arr[i] = arr[i-1]
                print(*arr)
            if arr[i-1] < stored:
                arr[i] = stored
                print(*arr)
                break  
            if i == 0:
                arr[i]=stored
                print(*arr)
                break
    
    0|
    Permalink
  • mikbolkahhori
    2 weeks ago+ 0 comments

    Insertion Sort Pyton 3

        num_to_insert = arr[-1]
        i = n-2
        while (i >= 0) and (arr[i] > num_to_insert):
            arr[i+1] = arr[i]
            i -= 1
            print(*arr)
        arr[i+1] = num_to_insert
        print(*arr)
    

    This code implements an insertion sort algorithm to sort an array of numbers in ascending order. It initializes a variable num_to_insert to be the last element of the array, i to the second to last index, and arr[i+1] = arr[i] shifts each element at an index greater than i one position to the right. The line print(*arr) prints out the array after each shift operation to visualize the sorting process.

    0|
    Permalink
Load more conversations

Need Help?


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