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.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Sorting
  4. Insertion Sort - Part 2
  5. Discussions

Insertion Sort - Part 2

Problem
Submissions
Leaderboard
Discussions

Sort 392 Discussions, By:

votes

Please Login in order to post a comment

  • cdima 6 years ago+ 0 comments

    Pretty badly described algorithm, needs a better description of the steps.

    143|
    Permalink
  • vshantam 4 years ago+ 0 comments
    Python 3
    b=int(input())
    a=[int(x) for x in input().strip().split(' ')]
    for i in range(1,len(a)):
        key=a[i]
        j=i-1
        while j>=0 and a[j]>key:
            a[j+1]=a[j]
            j=j-1
        a[j+1]=key
        print(*a)
    

    Do let me know if there is more finer solution in python.

    22|
    Permalink
  • Octowl 5 years ago+ 0 comments

    For those trying this in javaScript, remember that the input is an array of strings.

    You need to map the elements to integers using:

    arr.map(function(x) { return parseInt(x, 10); });

    14|
    Permalink
  • RodneyShag 4 years ago+ 0 comments

    Java solution - passes 100% of test cases

    public static void insertionSortPart2(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int j = i;
            int value = array[i];
            while (j >= 1 && array[j-1] > value) {
                array[j] = array[j-1];
                j--;
            }
            array[j] = value;
            printArray(array);
        }
    }
    

    Full solution available in my HackerRank solutions.

    Let me know if you have any questions.

    8|
    Permalink
  • briancmpbll 5 years ago+ 0 comments

    In the C++ code, why switch from vectors in Part 1 to C arrays in Part 2??

    6|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature