๐Ÿ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • insertion Sort
  • ์‚ฝ์ž… ์ •๋ ฌ
  • ์ •๋ ฌ ๋ฐฉ๋ฒ•

Was this helpful?

  1. algorithm
  2. sort

insertionSort

PreviousmergeSortNextbubbleSort

Last updated 3 years ago

Was this helpful?

insertion Sort

์‚ฝ์ž… ์ •๋ ฌ

: ์ž๋ฃŒ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋ฃŒ ์ด๋ฏธ ์ •๋ ฌ๋œ ๋ฐฐ์—ด ๋ถ€๋ถ„๊ณผ ๋น„๊ตํ•˜์—ฌ, ์ž์‹ ์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„ ์‚ฝ์ž…

  • ๋‹ค๋ฅธ ์ •๋ ฌ ๋ฐฉ์‹์€ ๋ฌด์กฐ๊ฑด ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹์ด์˜€๋‹ค๋ฉด ์‚ฝ์ž… ์ •๋ ฌ์€ ํ•„์š”ํ•  ๋•Œ๋งŒ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๊ฒŒ ๋œ๋‹ค

  • ์‚ฝ์ž… ์ •๋ ฌ์€ ๋น„๊ต์  ๋А๋ฆฐ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์†ํ•˜๋ฉฐ, ๋ณต์žกํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค

  • ๋งŒ์•ฝ, ์ •๋ ฌ์ด ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์žฅํ•˜๋ฉด ํŠน์ •ํ•œ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋น ๋ฅธ ์†๋„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜๋ฉฐ,

  • ์‚ฝ์ž… ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^2)์ด๋‹ค

์ •๋ ฌ ๋ฐฉ๋ฒ•

  • ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•ž์—์„œ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ •๋ ฌ๋œ ๋ถ€๋ถ„๊ณผ ๋น„๊ตํ•˜์—ฌ, ์ ์ ˆํ•œ ์œ„์น˜๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

  • ํŠน์ง•์œผ๋กœ๋Š” ๊ณ„์† ๋น„๊ตํ•˜๋ฏ€๋กœ ๋ฆฌ์Šคํฌ ํฌ๊ธฐ๊ฐ€ ํฌ๋ฉด ๋ถˆ๋ฆฌํ•˜๋ฉฐ, ์ •๋ ฌ์ด ๋น„๊ต์  ์™„์„ฑ๋œ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ๋” ์œ ๋ฆฌํ•˜๋‹ค

public class InsertionSort {
    public static void main(String[] args) {
        int i, j, temp;
        int[] arr = { 1, 10, 5, 8, 7, 6, 4, 3, 2, 9 };
        for (i = 0; i < arr.length-1 ; i++) { 
            j = i; 
            while (j >= 0 && arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                j--; 
            }
        }
        for ( i = 0; i < arr.length ; i++) {
            System.out.printf("%d ", arr[i]);
        }
    }
}
array = [1, 10, 5, 8, 7, 6, 4, 3, 2, 9]

for i in range(1, len(array)):
    for j in range(i, 0, -1): # ์ธ๋ฑ์Šค i๋ถ€ํ„ฐ 1๊นŒ์ง€ 1์”ฉ ๊ฐ์†Œํ•˜๋ฉฐ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฌธ๋ฒ•
        if array[j] < array[j - 1]: # ํ•œ ์นธ์”ฉ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
            array[j], array[j - 1] = array[j - 1], array[j]
        else: # ์ž๊ธฐ๋ณด๋‹ค ์ž‘์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋‚˜๋ฉด ๊ทธ ์œ„์น˜์—์„œ ๋ฉˆ์ถค
            break

print(array)

Reference : Wikipedia
InsertionSort
InsertionSort