Insertion Sort - Part 2

Insertion Sort - Part 2

In Insertion Sort Part 1, you inserted one element into an array at its correct sorted position. Using the same approach repeatedly, can you sort an entire array?

Guideline: You already can place an element into a sorted array. How can you use that code to build up a sorted array, one element at a time? Note that in the first step, when you consider an array with just the first element, it is already sorted since there’s nothing to compare it to.

In this challenge, print the array after each iteration of the insertion sort, i.e., whenever the next element has been inserted at its correct position. Since the array composed of just the first element is already sorted, begin printing after placing the second element.

Example

n = 7

arr = [3,4,7,5,6,2,1]

Working from left to right, we get the following output:

1
2
3
4
5
6
3 4 7 5 6 2 1
3 4 7 5 6 2 1
3 4 5 7 6 2 1
3 4 5 6 7 2 1
2 3 4 5 6 7 1
1 2 3 4 5 6 7

Function Description

Complete the insertionSort2 function in the editor below.

insertionSort2 has the following parameter(s):

  • int n: the length of arr
  • int arr[n]: an array of integers

Prints

At each iteration, print the array as space-separated integers on its own line.

Input Format

The first line contains an integer, n, the size of arr.

The next line contains n space-separated integers arr[i].

Constraints

1 <= n <= 1000

-10000 <= arr[i] <= 10000,0 <= i < n

Output Format

Print the entire array on a new line at every iteration.

Sample Input

1
2
3
4
STDIN           Function
----- --------
6 n = 6
1 4 3 5 6 2 arr = [1, 4, 3, 5, 6, 2]

Sample Output

1
2
3
4
5
1 4 3 5 6 2
1 3 4 5 6 2
1 3 4 5 6 2
1 3 4 5 6 2
1 2 3 4 5 6

Explanation

Skip testing 1 against itself at position 0. It is sorted.

Test position 1 against position 0: 4 > 1, no more to check, no change.

Print arr

Test position 2 against positions 1 and 0:

  • 3 < 4, new position may be 1. Keep checking.
  • 3 > 1, so insert 3 at position 1 and move others to the right.

Print arr

Test position 3 against positions 2,1,0 (as necessary): no change.

Print arr

Test position 4 against positions 3,2,1,0: no change.

Print arr

Test position 5 against positions 4,3,2,1,0, insert 2 at position 1 and move others to the right.

Print arr


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* Complete the 'insertionSort2' function below.
*
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER_ARRAY arr
*/

function insertionSort2(n, arr) {
// Write your code here
for (let i = 1, itotal = arr.length; i < itotal; i++) {
for (let j = i; arr[j] < arr[j - 1]; j--) {
[arr[j], arr[j - 1]] = [arr[j - 1], arr[j]];
}

console.log(arr.join(' '));
}
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×