Java中的冒泡排序算法

package cn.edu.hactcm;

/**

* 冒泡排序算法

*/

public class BubbleSortDemo {

public static void main(String[] args) {

int[] r = { 22, 12, 34, 123, 65, 34, 65, 34, 567, 3, 65, 546, 4 };

BubbleSort(r);

for (int i : r) {

System.out.print(i + " ");

}

}

private static void BubbleSort(int[] r) {

Boolean exchange;

int tmp;

for (int i = 1; i < r.length;) {

//最多做n-1趟排序

exchange = false;  //本趟排序开始前,交换标志应为假

for (int j = 0; j < r.length - i; j++) {

if (r[j] > r[j+1]) {

//交换记录

tmp = r[j + 1];

r[j + 1] = r[j];

r[j] = tmp;

exchange = true;//当发生了交换,提前终止算法

}

}

if (!exchange) {

return;

}

}

}

}

Java中插入排序算法

package cn.edu.hactcm;

/**

* 插入排序算法

*/

public class InsertSortDemo01 {

public static void main(String[] args) {

int r[] = { 110, 3, 23, 23, 231, 342, 45 };

System.out.println("直接插入排序后的结果为:");

InsertSort(r);

for (int i = 0; i < r.length; i++) {

System.out.print(r[i] + " ");

}

}

public static void InsertSort(int[] r) {

for (int i = 1; i < r.length; i++) { // 注意此处从1开始的

if (r[i] < r[i - 1]) {//如果后者小于前者

int temp = r[i]; // 要排序的数字

int j = 0;

for (j = i - 1; j >= 0 && temp < r[j]; j--) {

r[j + 1] = r[j]; // 将数据后移

}

// 若不符合上面的情况时让数组的i个的值为temp

r[j + 1] = temp;

}

}

}

}

快排:

package cn.edu.hactcm;

/**

* 快排算法

*/

public class QuickSortDemo {

public static void main(String[] args) {

int R[] = { 22, 12, 34, 123, 65, 34, 65, 34, 567, 3, 65, 45, 546, 4 };

quickSort(R, 0, 13);

for (int i = 0; i < R.length; i++) {

System.out.print(R[i] + " ");

}

}

public static void quickSort(int[] R, int low, int high) {

if (low > high)

return;

int pivot = R[low];

int i = low;

int j = high;

while (i < j) {

while (i < j && R[j] >= pivot)

j--;

R[i] = R[j]; // 将后面的值赋给R[i],使小的数值排在前面

while (i < j && R[i] <= pivot)

i++;

R[j] = R[i]; // 当前面的数值中有大于pivot的数值时,将之后排

}

R[i] = pivot; // 将空出来的位置放上pivot

quickSort(R, low, i - 1);

quickSort(R, i + 1, high);

}

}

Java中选择排序

package cn.edu.hactcm;

/**

* 选择排序算法

*/

public class SelectSortDemo {

public static void main(String[] args) {

int r[] = { 22, 12, 34, 123, 65, 34, 65, 34, 567, 3, 65, 45, 546, 4 };

SelectSort(r);

for (int i = 0; i < r.length; i++) {

System.out.print(r[i] + " ");

}

}

/**

* 选择排序

* @param r

*/

public static void SelectSort(int[] r) {

int i, j, k;

int temp;

for (i = 0; i < r.length - 1; i++) {

k = i; // 最开始是将k的值赋给要比较的数字

for (j = i + 1; j < r.length; j++) {

if (r[j] < r[k]) {

k = j; // k记下目前找到的最小关键字所在的位置

}

}

if (k != i) { // 一定要记住是k和i之间的比较

temp = r[i];

r[i] = r[k];

r[k] = temp;

}

}

}

}

package cn.edu.hactcm;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* 查找元素的位置

*/

public class TheFirstOne {

public static void main(String[] args) {

int[] temp = { 10, 20, 32, 2, 565, 232, 54, 67, 34, 0 };

System.out.print("请输入你要找的值:");

BufferedReader buf = new BufferedReader(

new InputStreamReader(System.in));

int key = 0;

try {

key = Integer.parseInt(buf.readLine());

} catch (NumberFormatException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if (buf != null) {

try {

buf.close();

} catch (IOException e) {

throw new RuntimeException(e.getMessage(), e);

}

}

buf = null;

}

System.out.println("您要查找的元素在数组中的位置为:" + seqSearch(temp, key));

}

/**

*查询算法

*/

public static int seqSearch(int temp[], int key) {

int i;

for (i = 0; temp[i] != key; i++);

if (i == temp.length) {

return -1;

} else {

return i;

}

}

}

package cn.edu.hactcm;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* 查找元素的位置

*/

public class TheSecondOne {

public static void main(String[] args) {

int[] temp = { 10, 20, 32, 2, 565, 232, 54, 67, 34, 0 };

System.out.print("请输入你要找的值:");

BufferedReader buf = new BufferedReader(

new InputStreamReader(System.in));

int key = 0;

try {

key = Integer.parseInt(buf.readLine());

} catch (NumberFormatException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if (buf != null) {

try {

buf.close();

} catch (IOException e) {

throw new RuntimeException(e.getMessage(), e);

}

}

buf = null;

}

System.out.println("您要查找的元素在数组中的位置为:" + binSearch(temp, key));

}

/**

* 中分查找

* @param temp

* @param key :我们要找的值

* @return

*/

public static int binSearch(int[] temp, int key) {

int low = 0, high = temp.length - 1, mid; // high表示数组最后一个的下标

while (low <= high) {

mid = (low + high) / 2;

//如果中间值恰好为我们要找的值

if (temp[mid] == key)

return mid;

if (temp[mid] > key)

high = mid - 1;

else

low = mid + 1;

}

return -1; // 当low》high时表示查找区间为空,查找失败

}

}

Java中实现的各种排序算法相关推荐

  1. java常用的7大排序算法汇总

    这段时间闲了下来,就抽了点时间总结了下java中常用的七大排序算法,希望以后可以回顾! 1.插入排序算法 插入排序的基本思想是在遍历数组的过程中,假设在序号 i 之前的元素即 [0..i-1] 都已经 ...

  2. Java常用的八种排序算法与代码实现

    在Java的时候,对于排序的应用需要熟练的掌握,这样才能够确保Java学习时候能够有扎实的基础能力.那Java有哪些排序算法呢?本文小千就来详细说说Java经典的8种排序算法. 经典的排序算法有八种, ...

  3. 程序兵法:Java String 源码的排序算法(一)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第103篇原创 <程序兵法:Java Str ...

  4. [转载] java中对数组进行排序_如何在Java中对数组排序

    参考链接: Java中main()的有效变体 java中对数组进行排序 Java Array is like a container that can hold a fixed number of t ...

  5. java中对数组进行排序_如何在Java中对数组排序

    java中对数组进行排序 Java Array is like a container that can hold a fixed number of the same type of items, ...

  6. 【CSWS2014 Summer School】互联网广告中的匹配和排序算法-蒋龙(上)

    Title:互联网广告中的匹配和排序算法 蒋龙博士,通联数据 Abstract:互联网广告是利用互联网提供的基础设施进行产品和服务营销的一种新形式,具有比传统广告方式更精准,成本收益更透明的优势.互联 ...

  7. java中如何对对象排序?

    大家好,我是雄雄. 前言: 我们知道,在平时做项目的过程中,我们总会用到各种各样的排序,或是升序,或是降序.在java中,要实现排序有好多中方式,比如我们耳熟能详的冒泡排序.选择排序等,但是我们一般都 ...

  8. java中的五种排序方法_用Java排序的五种有用方法

    java中的五种排序方法 Java排序快速概述: 正常的列表: private static List VEGETABLES = Arrays.asList("apple", &q ...

  9. php自然排序法的比较过程,PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)...

    本文实例讲述了PHP中strnatcmp()函数"自然排序算法"进行字符串比较用法.分享给大家供大家参考,具体如下: PHP中strnatcmp()函数使用"自然&quo ...

最新文章

  1. 小米盗图迪丽热巴?公关经理:占位示意 不存在故意盗图!
  2. “软件开发教父” Martin Fowler 从业 40 年最想说这两个字!
  3. php一次上传多张图片,html5 multiple的使用。
  4. Linux-c对一个十六进制数的某一位取反
  5. Macbook Pro 201 装Win10 声卡_直播_个人工作室入门_1K-2k价位录音编曲声卡推荐
  6. foxmail超大附件服务器文件怎么删,foxmail邮件太大怎么发?foxmail发送超大附件的方法...
  7. ah、esp、gre协议_AH,ESP的协议号和它们支持的功能有何差异?两种模式下的差异是什么?...
  8. 利用函数wavread对语音信号进行采样_信号时间采样
  9. 【详细】【转】CentOS 7部署ASP.NET Core应用程序
  10. 从0开始学AI-DAY0-写在前面
  11. style标签中的几个属性
  12. Android上又双叒冒出新的恶意软件:ROOT近800万台设备、狂发广告骗推广费
  13. 素材pptuku shutterstock站酷海洛创意海图片代创意矢量图片下载
  14. Android - Adapter 适配器
  15. 【百度地图】仅显示中国边界区域
  16. 异常检测:Towards Total Recall in Industrial Anomaly Detection
  17. 视频时长太长,怎么才能快速去头去尾
  18. 作为程序媛工作一年半感悟与收获
  19. 斜方肌(01):负重耸肩
  20. Git命令的使用记录及常见的错误解决方式

热门文章

  1. LeetCode 392打劫房屋 python
  2. VTK:场景边界SceneBounds用法实战
  3. OpenCASCADE:形状愈合之修复工具
  4. wxWidgets:wxWizard类用法
  5. boost::type_index模块实现一个类型中获得简短的和人类可读的类型名称
  6. boost::local_shared_ptr相关的测试程序
  7. boost::safe_numerics模块实现检测到错误表达式评估的测试程序
  8. 验证 Boost.Optional 复制构造函数不会尝试调用从模板化参数初始化构造函数的元素类型
  9. boost::multiprecision模块gmp相关的测试程序
  10. boost::mp11::mp_bind_back相关用法的测试程序