๐Ÿ“—
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
  • Bubble Sort
  • ์ •๋ ฌ ๋ฐฉ๋ฒ•

Was this helpful?

  1. algorithm
  2. sort

bubbleSort

PreviousinsertionSortNextheapSort

Last updated 3 years ago

Was this helpful?

Bubble Sort

: ์ธ์ ‘ํ•œ ๋‘ ์ˆซ์ž๋ผ๋ฆฌ ๋น„๊ต ํ•ด์„œ ๋” ์ž‘์€ ์ˆซ์ž๋ฅผ ์•ž์œผ๋กœ ๋ณด๋‚ด์ฃผ๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณต

  • ์ธ์ ‘ํ•œ ๊ฒƒ์„ ๋น„๊ตํ•ด์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด ๋งจ ๋’ค๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค

  • ์ œ์ผ ํฐ ๊ฐ’์ด ๋’ค๋กœ ๊ฐ€๊ฒŒ ๋˜๋ฉด ๋งจ ๋’ค์˜ ๊ฐ’์„ ์ œ์™ธํ•˜๋ฉด์„œ ๋น„๊ต๋ฅผ ํ•˜๋ฉด์„œ ์ œ์ผ ํฐ ๊ฐ’์„ ๋’ค๋กœ ๋ณด๋‚ธ๋‹ค

  • ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์—์„œ ๊ตฌํ˜„์€ ๊ฐ€์žฅ ์‰ฝ์ง€๋งŒ ๊ฐ€์žฅ ๋น„ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

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

์ •๋ ฌ ๋ฐฉ๋ฒ•

public class BubbleSort {
    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; i++) {
            for (j = 0; j < arr.length-1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for ( i = 0; i < arr.length ; i++) {
            System.out.printf("%d ", arr[i]);
        }
    }
}
bubbleSort