Java 3 5 4 9生成一个冒泡排序
时间: 2023-04-01 11:00:33 浏览: 59
可以使用以下代码实现冒泡排序:
```
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = ; i < n - 1; i++) {
for (j = ; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {3, 5, 4, 9};
int n = sizeof(arr) / sizeof(arr[]);
bubbleSort(arr, n);
printf("排序后的数组:\n");
for (int i = ; i < n; i++) {
printf("%d ", arr[i]);
}
return ;
}
```
相关问题
用java生成一个冒泡排序
的实现代码。
好的,以下是Java中的冒泡排序实现代码:
```java
public class BubbleSort {
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] array = {7, 9, 5, 1, 3};
bubbleSort(array);
System.out.println(Arrays.toString(array));
}
}
```
以上即为用Java生成一个冒泡排序的实现代码,希望可以帮助到您。
用Java生成一个冒泡排序
的代码。
以下是用Java生成一个冒泡排序的代码:
```
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 3, 7, 1, 8, 4, 6};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; 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;
}
}
}
}
}
```
这段代码可以对一个整型数组进行冒泡排序,将数组中的元素从小到大排序。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)