- /*冒泡排序
- *@arr int* 一个字符数组指针
- *@n int 数组长度
- *@is_asc bool 是否升序
- */
- void bubble_sort(int* arr, int n,bool is_asc)
- {
- //升序,还是降序?
- if (is_asc)
- {//升序
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n-i-1; j++)
- {
- if (arr[j] > arr[j+1]) {
- int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- }
- else
- {//降序
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n-i-1; j++)
- {
- if (arr[j] < arr[j+1]) {
- int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0810201410532.html
来源: http://www.codesnippet.cn/detail/0810201410532.html