最开始的代码

我采用的是我原来进行快速排序所用的方法,一直做不出来。

为什么我会采用原来快速排序的方法?因为我的记忆中好像就是这样的,因此我根据记忆中的快速排序在进行改变,然而,却无法真正的写出双冒泡排序算法,所以,真正的学习是,即使随着时间的流逝,你已经不记得某些东西了,但是,你可以凭借理解在重新写出来。

public static void sort(int[] num, int l, int r) {
int i = l;
int j = l + 1;
int key = num[l];
while () {
while (num[j] > key)
j++;
if (j < r)
num[i] = num[j];
while (i < j && num[i] < key)
i++;
if (i < j)
num[j++] = num[i];
}
num[i] = key;
for (int n : num)
System.out.print(n + " ");
}

改良后的

快速排序法(双冒泡排序法)

冒泡排序 - 维基百科,自由的百科全书

    public static void sort(int[] num, int l, int r) {
if (l < r) {
int i = l;
int j = l + 1;
while (j < r) {
if (num[j] < num[l]) {
i++;
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
j++;
}
//System.out.println(i);
int temp = num[i];
num[i] = num[l];
num[l] = temp;
sort(num, l, i);
sort(num, i + 1, r);
}
}

大数据排序

当我将数组大小增大到3000000后,出现异常。

十道海量数据处理面试题与十个方法大总结

交换两个数

java 没有指针,所以不可以使用swap函数进行交换,采用下面的方式

 int temp = num[i];
num[i] = num[j];
num[j] = temp;

也有其他的方式:

交换两个整数的三种实现方法(C/C++)

单链表的排序

通过尾插法建立一个单链表,然后进行双冒泡排序

class Student {
int num;
Student next;
} public class LinkedList {
public static void main(String[] args) {
Student head = null;
for (int i = 1; i <= 50; i++) {
head = addBack(head, (int) (Math.random() * 100));
}
show(head);
sort(head, null);
System.out.println();
show(head);
} public static Student addBack(Student head, int num) {
Student newStudent = new Student();
newStudent.num = num;
newStudent.next = null;
Student student = head;
if (head == null) {
head = newStudent;
return head;
}
while (student.next != null) {
student = student.next;
}
student.next = newStudent; return head;
} public static void show(Student student) {
while (student != null) {
System.out.println(student.num);
student = student.next;
} } public static void sort(Student start, Student end) {
if (start != end) {
Student p1 = start;
Student p2 = p1.next;
while (p2 != end) {
if (p2.num < start.num) {
p1 = p1.next;
int temp = p1.num;
p1.num = p2.num;
p2.num = temp;
}
p2 = p2.next;
}
int temp = p1.num ;
p1.num =start.num ;
start.num =temp;
sort(start, p1);
sort(p1.next, null);
} } }

Java - 双冒泡法排序的更多相关文章

  1. Java温故而知新-冒泡法排序

    冒泡法排序是各种初学者在学习数组与循环结构时都会练习的一种简单排序算法. 冒泡法的精髓在于比较相邻的两个元素,较大的元素会不断的排到队伍后面去,就像水里的泡泡一样不断向上跑. 想像一下倒在一个透明玻璃 ...

  2. 【Java基础】选择排序、冒泡法排序、二分法查找

    1.选择排序: //改进后的选择排序,减少交换的次数 public static void sortSelect(int arr[]) { //用于存放最小数的下标 int s; for (int i ...

  3. [python,2018-01-15] 冒泡法排序

    想写一个冒泡法排序,没什么思路,就先写了个java的 public static void main(String[] args) { int array[] = {88,2,43,12,34,8,6 ...

  4. 8. 冒泡法排序和快速排序(基于openCV)

    一.前言 主要讲述冒泡法排序和快速排序的基本流程,并给出代码实现,亲测可用. 二.冒泡法排序 冒泡法排序主要是将相邻两个值比较,把小的向前冒泡,大的向后沉淀,时间复杂度为O(n2).主要思想如下: 分 ...

  5. Python 冒泡法排序

    def sequence(disorder='', separators=''): arrays = disorder.split(separators) def desc(): for i in r ...

  6. C#冒泡法排序源码

    如下内容内容是关于C#冒泡法排序的内容,应该对码农有一些用途. int[] myArray = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 }; for( int j ...

  7. C语言 · 冒泡法排序

    算法提高 冒泡法排序   时间限制:1.0s   内存限制:512.0MB      输入10个数,用“冒泡法”对10个数排序(由小到大)这10个数字在100以内. 样例输入 1 3 6 8 2 7 ...

  8. C程序数组算法 — 冒泡法排序【前冒 || 后冒】

    第一种写法(前冒泡): /* C程序数组算法 - 冒泡法排序 * 此例子按照 大 -> 小 排序 * 原理:两两相比较,然后进行大小对调 * 比较次数: n^2 次 * 说明:冒泡排序是相对稳定 ...

  9. 冒泡法排序_c++实现

    看完了郝斌老师的c语言视频,冒泡法排序,就试着写了.我觉得学习算法最重要的不是代码,而是它的原理. 代码: /** 2 * Copyright (c) 1991 - 2016 Arvin Tang. ...

随机推荐

  1. BZOJ 2463: [中山市选2009]谁能赢呢?(新生必做的水题)

    2463: [中山市选2009]谁能赢呢? Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2372  Solved: 1750[Submit][Sta ...

  2. ZOJ 1002 DFS

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  3. More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

  4. MongoDB基本命令操作

    在上一篇随笔中记录了如何在Centos7上安装MongoDB数据库,这一篇我们就一起来学学基本的操作命令. 安装完成后,shell交互式下输入mongo就可以直接无密码登录到数据库. show dbs ...

  5. sass 安装

    最近在安装sass的过程中遇到 了一下问题,总结一下安装过程. windows下sass的安装是依赖于ruby的,所以要先安装rubyinstaller,下载地址:https://rubyinstal ...

  6. volatile 与 synchronized 区别

    在Java中,为了保证多线程读写数据时保证数据的一致性,可以采用两种方式: 同步 如用synchronized关键字,或者使用锁对象. volatile 使用volatile关键字用一句话概括vola ...

  7. JXLS 2.4.0系列教程(五)——更进一步的应用和页面边距bug修复

    注:本文代码建立于前面写的代码.不过不看也不要紧. 前面的文章把JXLS 2.4.0 的基本使用写了一遍,现在讲讲一些更进一步的使用方法.我只写一些我用到过的方法,更多的高级使用方法请参考官网. ht ...

  8. 【django基础补充之URL,视图,模版】

    一.url路由配置 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个URL调用这段代 ...

  9. for循环去重排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Win7如何解决telnet不是内部或外部命令的方案!听语音

    Telnet用于远程操作互联网中的设备或终端计算机服务器,可以有效的减少现场操作的麻烦.因为设备或终端是遍布整个省或市,有的甚至是国外,如何高效的处理问题是当务之急,除了telnet还可以ssh使用c ...