冒泡排序
来源:互联网 发布:阿里云药师招聘 编辑:程序博客网 时间:2024/06/09 23:21
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BubbleSort {
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String []s=in.readLine().trim().split(" ");
int a[]=new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i]=Integer.parseInt(s[i]);
}
a=sortPrint(a);
for (int i :a) {
System.out.print(i+" ");
}
System.out.println();
}
private static int[] sortPrint(int[] a) {
// TODO Auto-generated method stub
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length-1-i; j++) {
if (a[j]>a[j+1]) {
a[j]=a[j]^a[j+1];
a[j+1]=a[j]^a[j+1];
a[j]=a[j]^a[j+1];
}
}
}
return a;
}
import java.io.IOException;
import java.io.InputStreamReader;
public class BubbleSort {
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String []s=in.readLine().trim().split(" ");
int a[]=new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i]=Integer.parseInt(s[i]);
}
a=sortPrint(a);
for (int i :a) {
System.out.print(i+" ");
}
System.out.println();
}
private static int[] sortPrint(int[] a) {
// TODO Auto-generated method stub
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length-1-i; j++) {
if (a[j]>a[j+1]) {
a[j]=a[j]^a[j+1];
a[j+1]=a[j]^a[j+1];
a[j]=a[j]^a[j+1];
}
}
}
return a;
}
}
时间复杂度O(n*n)
空间复杂度O(1)
阅读全文
0 0
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- 冒泡排序
- hibernate查询
- 学生成绩表的常用查询
- 泛型算法(find、count、sort、fill、unique、copy、lambda、迭代器)
- Android攻防五
- 手写二叉搜索树(Java实现)
- 冒泡排序
- solr整合spring
- 计算机核心期刊排行
- Android View 高级框架一 Builder模式打造通用TitleBar
- 隐函数定理
- 机器学习实战-数据探索(变量变换、生成)
- 数据处理--reshape2包(长宽数据)
- centos设置ip地址的几种方式及存在问题解决
- centos7服务器 搭建.net httpd mysql环境