알고리즘

2. 버블정렬

it 킹왕짱 2022. 7. 29. 11:13
728x90
  • 버블정렬: 옆에 있는 값과 비교해서 더 작은 값이 있다면 서로 변경
#define _CRT_SECURE_NO_WARNINGS // scanf 보안 경고로 인한 컴파일 에러 방지
#include <stdio.h>
void bubble(int a[], int num);
int main() {
	int i, j, temp, a[10];
	for (i = 0; i < 10; i++) {
		scanf("%d", &a[i]);
	}
	bubble(a, 10);
	for (i = 0; i < 10; i++) {
		printf("%d ", a[i]);
	}

}

void bubble(int arr[], int count)    {
	int temp;
	for (int i = 0; i < count-1; i++)    
	{
		for (int j = 0; j < count - i - 1; j++)  
		{
			if (arr[j] > arr[j + 1])          
			{                                
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;           
			}
		}
	}
}

 

  • 시간복잡도

728x90
728x90

'알고리즘' 카테고리의 다른 글

3. 삽입정렬  (0) 2022.07.29
1. 선택정렬  (0) 2022.07.29