冒泡排序:
相邻数据逐个比较
复杂度:
几种写法:
int[] arr = {5,2,1,3}; //升序 for (int i = 0; i < arr.length-1; i++) { for (int j = i; j < arr.length-1; j++) { if (arr[i] > arr[j+1]) { int temp = arr[i]; arr[i] = arr[j+1]; arr[j+1] = temp; } } } //降序 for (int i = 0; i < arr.length-1; i++) { for (int j = i; j < arr.length-1; j++) { if (arr[i] < arr[j+1]) { int temp = arr[j+1]; arr[j+1] = arr[i]; arr[i] = temp; } } }