saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 개발자
  • Python
  • 취준
  • 알고리즘문제해결
  • 백준
  • 알고리즘
  • 리트코드
  • two pointer
  • c++
  • 개발자 취업준비
  • DFS
  • 릿코드
  • 문제해결능력
  • 딥러닝
  • BFS
  • 온라인저지
  • 취업준비
  • 딕셔너리
  • LeetCode
  • 파이썬

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/알고리즘 공부

Insertion Sort 삽입 정렬 Hackerrank

2017. 1. 6. 12:19









Insertion Sort - Part 1 

by HackerRank

Sorting 
One common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.

Insertion Sort 
These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with an already sorted list.

Insert element into sorted list 
Given a sorted list with an unsorted number  in the rightmost cell, can you write some simple code to insert  into the array so that it remains sorted?

Print the array every time a value is shifted in the array until the array is fully sorted. The goal of this challenge is to follow the correct order of insertion sort.

Guideline: You can copy the value of  to a variable and consider its cell "empty". Since this leaves an extra cell empty on the right, you can shift everything over until  can be inserted. This will create a duplicate of each value, but when you reach the right spot, you can replace it with .

Input Format 
There will be two lines of input:

  •  - the size of the array
  •  - the unsorted array of integers

Output Format 
On each line, output the entire array every time an item is shifted in it.

Constraints 
 

Sample Input

5
2 4 6 8 3

Sample Output

2 4 6 8 8 
2 4 6 6 8 
2 4 4 6 8 
2 3 4 6 8 

Explanation

 is removed from the end of the array.
In the st line , so  is shifted one cell to the right. 
In the nd line , so  is shifted one cell to the right. 
In the rd line , so  is shifted one cell to the right. 
In the th line , so  is placed at position .

Task

Complete the method insertionSort which takes in one parameter:

  •  - an array with the value  in the right-most cell.

Next Challenge

In the next Challenge, we will complete the insertion sort itself!
































































































































소스 코드 !



import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;



public class Solution {

    

    

    

    public static void insertIntoSorted(int[] ar) {

        // Fill up this function

        int s = ar.length;

        for(int i=1; i<s; i++){

            int key = ar[i],j=i-1;

            for(; j>=0 && ar[j]>key; j--){

                ar[j+1] = ar[j];

                printArray(ar);

            }

            ar[j+1] = key;

        }

        printArray(ar);

    }

    

    

    /* Tail starts here */

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int s = in.nextInt();

        int[] ar = new int[s];

        for(int i=0;i<s;i++){

            ar[i]=in.nextInt();

        }

        insertIntoSorted(ar);

    }

    

    

    private static void printArray(int[] ar) {

        for(int n: ar){

            System.out.print(n+" ");

        }

        System.out.println("");

    }

}


저작자표시 (새창열림)

'컴퓨터공학 > 알고리즘 공부' 카테고리의 다른 글

[알고리즘 문제 해결 전략] Part 1. 6장 무식하게 풀기 [소풍]  (0) 2018.11.12
[알고리즘 문제 해결 전략] Part 1. 6장 무식하게 풀기  (0) 2018.10.27
Quicksort 2 - Sorting Hackerrank 퀵정렬 퀵소트 문제!  (0) 2017.01.06
Quicksort 1 - Partition Hackerrank 퀵소트 퀵정렬 알고리즘  (0) 2017.01.06
Counting Sort 계수 정렬 10989 백준 수정렬하기 3  (0) 2017.01.02
    '컴퓨터공학/알고리즘 공부' 카테고리의 다른 글
    • [알고리즘 문제 해결 전략] Part 1. 6장 무식하게 풀기
    • Quicksort 2 - Sorting Hackerrank 퀵정렬 퀵소트 문제!
    • Quicksort 1 - Partition Hackerrank 퀵소트 퀵정렬 알고리즘
    • Counting Sort 계수 정렬 10989 백준 수정렬하기 3
    saurus2
    saurus2
    Simple is Best

    티스토리툴바