JAVA51. 冒泡排序
描述
有一个长度为7的无序数组,按照从小到大的顺序排序后输出。输入描述
数组中的数据输出描述
数组中数据排序后输出示例1
输入:
13 11 9 7 5 3 1
输出:
1 3 5 7 9 11 13
C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-01-04
#include <bits/stdc++.h> using namespace std; int main() { int i,j,t; int a[7]; for(i=0;i<7;i++) { cin>>a[i]; } for(i=0;i<6;i++) { for(j=0;j<7-1-i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(i=0;i<7;i++) cout<<a[i]<<' '; }
Java 解法, 执行用时: 26ms, 内存消耗: 10512KB, 提交时间: 2022-02-10
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] arr = new int[7]; for (int i = 0; i < arr.length; i++) { arr[i] = scanner.nextInt(); } scanner.close(); //write your code here...... for(int k=0;k<arr.length-1;k++){ for(int j=0; j<arr.length-1-k;j++){ int temp; if(arr[j]>arr[j+1]){ temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for (int k = 0; k < arr.length; k++) { System.out.print(arr[k]+" "); } } }
Java 解法, 执行用时: 26ms, 内存消耗: 10604KB, 提交时间: 2022-02-10
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] arr = new int[7]; for (int i = 0; i < arr.length; i++) { arr[i] = scanner.nextInt(); } scanner.close(); //write your code here...... for(int i = 0; i < arr.length - 1; i++){ for(int j = 0; j < arr.length - 1 - i; j++){ if(arr[j] > arr[j+1]){ int temp =arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for (int k = 0; k < arr.length; k++) { System.out.print(arr[k]+" "); } } }
Java 解法, 执行用时: 26ms, 内存消耗: 10652KB, 提交时间: 2022-02-10
//import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] arr = new int[7]; for (int i = 0; i < arr.length; i++) { arr[i] = in.nextInt(); } //scanner.close(); for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for (int i = 0;i < arr.length; i++) { System.out.print(arr[i]+" "); } } }
Java 解法, 执行用时: 27ms, 内存消耗: 10596KB, 提交时间: 2022-02-08
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] arr = new int[7]; for (int i = 0; i < arr.length; i++) { arr[i] = scanner.nextInt(); } scanner.close(); //write your code here...... for(int i=0;i<arr.length;i++){ for(int j=0;j<i;j++){ if(arr[i]<arr[j]){ int temp = arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } for (int k = 0; k < arr.length; k++) { System.out.print(arr[k]+" "); } } }