ef bubbleSort(arr): n = len(arr) # 遍历所有数组元素 for i in range(n): # Last i elements are already in place #**********begin1********** for i in arr(0, n-i-1): #**********end1********** if arr[j] > arr[j+1] : arr[j], arr[j+1] = arr[j+1], arr[j] arr = [64, 34, 25, 12, 22, 11, 90] #**********begin2********** bubbleSort(j) #**********end2********** print ("排序后的数组:") for i in range(len(arr)): print ("%d" %arr[i]),
时间: 2023-07-05 09:33:55 浏览: 48
这段代码存在一些问题:
1. 在第一个 for 循环中,循环变量 i 已经被使用,但在内层的 for 循环中,又使用了 i ,这会导致内层循环覆盖了外层循环的 i,导致程序出错。
2. 在第二个 for 循环中,应该是 range(0, n-i-1),而不是 arr(0, n-i-1)。
3. 在主程序中,应该传入数组 arr,而不是变量 j。
下面是修改后的代码:
```
def bubbleSort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print("排序后的数组:")
for i in range(len(arr)):
print("%d" % arr[i], end=" ")
```
输出结果为:
```
排序后的数组:
11 12 22 25 34 64 90
```
相关问题
向数组中添加一个数据 删除数组中一个元素 修改数组中的某个元素 查询数组中元素 遍历数组 数组排序:冒泡排序
1. 向数组中添加一个数据:
可以使用数组的push方法将一个元素添加到数组的末尾:
```
var myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray); // [1, 2, 3, 4]
```
2. 删除数组中一个元素:
可以使用数组的splice方法删除数组中的一个元素:
```
var myArray = [1, 2, 3, 4];
myArray.splice(2, 1); // 从第2个位置开始删除1个元素
console.log(myArray); // [1, 2, 4]
```
3. 修改数组中的某个元素:
可以直接通过下标来修改数组中的一个元素:
```
var myArray = [1, 2, 3];
myArray[1] = 4;
console.log(myArray); // [1, 4, 3]
```
4. 查询数组中元素:
可以使用数组的indexOf方法来查询数组中某个元素的位置,如果不存在则返回-1:
```
var myArray = [1, 2, 3];
console.log(myArray.indexOf(2)); // 1
console.log(myArray.indexOf(4)); // -1
```
5. 遍历数组:
可以使用for循环或者forEach方法遍历数组:
```
var myArray = [1, 2, 3];
for (var i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
myArray.forEach(function(item) {
console.log(item);
});
```
6. 数组排序:冒泡排序
冒泡排序是一种简单的排序算法,它重复地遍历数组,每次比较相邻的两个元素,如果它们的顺序错误就交换它们的位置。这样每次遍历都会将一个最大的元素排到最后。
```
function bubbleSort(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
for (var j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
var myArray = [3, 2, 1, 4];
console.log(bubbleSort(myArray)); // [1, 2, 3, 4]
```
优化这段代码#include <iostream> void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { // 如果当前元素为偶数,且下一个元素为奇数,则交换它们 if (arr[j] % 2 == 0 && arr[j + 1] % 2 != 0) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {9, 4, 7, 2, 5, 8, 1, 6, 3}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); std::cout << "排序后的数组: "; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
这段代码可以进行一些优化,以提高性能和减少不必要的交换操作。以下是优化后的代码:
```cpp
#include <iostream>
void bubbleSort(int arr[], int n) {
bool swapped; // 添加一个标志位,用于判断是否发生交换
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
// 如果当前元素为偶数,且下一个元素为奇数,则交换它们
if (arr[j] % 2 == 0 && arr[j + 1] % 2 != 0) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break; // 如果没有发生交换,则数组已经有序,提前退出循环
}
}
}
int main() {
int arr[] = {9, 4, 7, 2, 5, 8, 1, 6, 3};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
std::cout << "排序后的数组: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
在优化后的代码中,添加了一个标志位 `swapped`,用于判断在一轮遍历中是否发生了交换。如果没有发生交换,则说明数组已经有序,可以提前退出外层循环。这样可以减少不必要的比较和交换操作,提高性能。
请注意,冒泡排序算法的时间复杂度为 O(n^2),在处理大规模数据时可能不是最优选择。如果对算法性能有更高要求,可以考虑其他排序算法,如快速排序、归并排序等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)