::public/알고리즘
버블 정렬(Bubble Sort)
해맑은욱
2019. 6. 18. 02:44
시간복잡도
O(n^2)
// 인접한 두 수를 비교하여 큰 수를 뒤로 보낸다.
void bubbleSort(int arr[], int n)
{
int temp;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "========== bubble sort ==========" << endl;
}
|
cs |