Skip to content

Instantly share code, notes, and snippets.

@XcqRomance
Created November 13, 2018 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XcqRomance/30c7e0a842f19062deb141274a2c3f6c to your computer and use it in GitHub Desktop.
Save XcqRomance/30c7e0a842f19062deb141274a2c3f6c to your computer and use it in GitHub Desktop.
冒泡排序 时间复杂度:O(n^2) 空间复杂度:O(1),原地排序 稳定排序
// 冒泡排序 反复交换相邻的未按照次序排列元素
// 一组数中,相邻的两个数进行比较、交换,将最大(小)数交换至尾(首)部,即完成了一次冒泡排序
void bubbleSort(int *arr,int arrSize) {
for (int i = 0 ; i < arrSize - 1; i++) {
for (int j = 1; j < arrSize - i; j++) {
if (arr[j-1] > arr[j]) {
swap(arr, j-1, j);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment