인생마린
어떤 공부 블로거의 금서목록
인생마린
전체 방문자
오늘
어제
  • 전체 (155)
    • C언어 (19)
    • Python (14)
      • Flask (0)
    • Coding Challenge (11)
      • Code Clone & Review (0)
      • Toy Project (0)
      • 오늘의 코드 (5)
    • Algorithm (6)
    • JAVA (8)
    • 웹 (8)
      • Javascript (3)
    • 정보보안 (19)
    • 기타 (21)
    • 일기는일기장에 (2)
    • 리눅스 (4)
    • 철학 (1)
    • 주식 (14)
    • AI (2)
    • 독후감 (13)
    • 프로그래밍 (4)
    • 게임 (1)
    • Devops (2)
      • CI_CD (2)
      • AWS (0)
    • Flutter (3)

블로그 메뉴

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

공지사항

인기 글

태그

  • Python
  • 카카오톡봇
  • Flutter
  • c언어
  • 비트코인
  • 백테스팅
  • best of best
  • 테라
  • Regular Expression
  • Bitcoin
  • Sphinx
  • Java
  • 주린이 #주식
  • Regex
  • 주식 #ETF
  • turtle
  • 코인
  • 주식 #배당주
  • 퀴즈봇
  • 가상화폐
  • 정규표현식
  • TFT
  • 불편한사회
  • 폭락
  • vpn
  • 우영우 #패러디논란
  • 해커톤
  • python #eval #dictionary
  • flask
  • smtplib

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
인생마린

어떤 공부 블로거의 금서목록

Algorithm

[정렬 알고리즘] Bubble Sort(버블정렬, 거품정렬)

2018. 3. 14. 20:46
반응형

§버블 정렬

잘못된 순서로 정렬된 인접한 원소를 반복적으로 교체하는 정렬 알고리즘이다.

 

Example:

First Pass:

( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.

( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap since 5 > 4

( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap since 5 > 2

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

Second Pass:

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )

( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 )

Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

Third Pass:

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

 

Time Complexity: O(n2) as there are two nested loops.

 

구현언어( C )

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

////////////////////////////////

//    Bubble Sort

//    버블 정렬

////////////////////////////////

 

 

#include <stdio.h>

 

void swap(int *xp, int *yp) {

    int tmp = *xp;

    *xp = *yp;

    *yp = tmp;

}

 

void bubbleSort(int arr[], int n) {

    int i, j;

    for (i = 0; i < n - 1; i++)

        for (j = 0; j < n - i - 1; j++)

            if (arr[j] > arr[j + 1])

                swap(&arr[j], &arr[j + 1]);

}

 

void printArray(int arr[], int size) {

    int i;

    for (i = 0; i < size; i++)

        printf("%d ", arr[i]);

    printf("\n");

}

 

int main() {

    int arr[] = { 64, 34, 25, 12, 22, 11, 90 };

    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Unsorted array: ");

    printArray(arr, n);

    bubbleSort(arr, n);

    printf("Sorted array: ");

    printArray(arr, n);

    return 0;

}

 

Colored by Color Scripter

cs

 

 


반응형
저작자표시 비영리 (새창열림)

'Algorithm' 카테고리의 다른 글

[정렬 알고리즘] Merge Sort(병합 정렬)  (1) 2018.03.14
[정렬 알고리즘] Recursive Insertion Sort(재귀 삽입 정렬)  (0) 2018.03.14
[정렬 알고리즘] Insertion Sort(삽입정렬)  (0) 2018.03.14
[정렬 알고리즘] Recursive Bubble Sort(재귀 버블 정렬)  (0) 2018.03.14
[정렬 알고리즘] Seletion Sort (선택 정렬)  (0) 2018.03.14
    'Algorithm' 카테고리의 다른 글
    • [정렬 알고리즘] Recursive Insertion Sort(재귀 삽입 정렬)
    • [정렬 알고리즘] Insertion Sort(삽입정렬)
    • [정렬 알고리즘] Recursive Bubble Sort(재귀 버블 정렬)
    • [정렬 알고리즘] Seletion Sort (선택 정렬)
    인생마린
    인생마린
    즐거운 프로그래밍~♬

    티스토리툴바