Java中实现的各种排序算法

简介: 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

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 lowint 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) { // 一定要记住是ki之间的比较

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; // lowhigh时表示查找区间为空,查找失败

}

}

 

目录
相关文章
|
2月前
|
存储 人工智能 算法
【数据结构-算法】:数据结构和算法的一些个人总结(Java实现)
【数据结构-算法】:数据结构和算法的一些个人总结(Java实现)
63 0
|
2月前
|
算法 Java
Java使用Cipher.getInstance(“AES/ECB/PKCS5Padding“);加解密算法工具类实现
Java使用Cipher.getInstance(“AES/ECB/PKCS5Padding“);加解密算法工具类实现
49 0
|
12天前
|
算法 安全 Java
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
【4月更文挑战第28天】性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
28 1
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
|
2月前
|
存储 算法 Java
Java数据结构与算法-java数据结构与算法(二)
Java数据结构与算法-java数据结构与算法
120 1
|
18天前
|
设计模式 算法 Java
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
|
19天前
|
搜索推荐 算法 Java
Java实现的常用八种排序算法
提到数据结构与算法,无法避免的一点就包含排序,熟练的掌握各种排序算法则是一个程序员必备的素质之一,除此之外,排序算法也是当下各大技术公司比较喜欢问的技术点,所以,就这一点JavaBuild整理了常见的8种排序算法
8 0
|
23天前
|
机器学习/深度学习 数据采集 算法
使用 Java 实现机器学习算法
【4月更文挑战第19天】Java在数据驱动时代为机器学习提供支持,具备丰富的数学和数据结构库,适用于实现线性回归、决策树、SVM和随机森林等算法。实现时注意数据预处理、模型选择、评估指标和可视化。利用Java的库和编程能力可构建高效模型,但需按问题需求选择合适技术和优化方法。
|
1月前
|
算法 安全 Java
java代码 实现AES_CMAC 算法测试
该代码实现了一个AES-CMAC算法的简单测试,使用Bouncy Castle作为安全提供者。静态变量K定义了固定密钥。`Aes_Cmac`函数接受密钥和消息,返回AES-CMAC生成的MAC值。在`main`方法中,程序对给定的消息进行AES-CMAC加密,然后模拟接收ECU的加密结果并进行比较。如果两者匹配,输出&quot;验证成功&quot;,否则输出&quot;验证失败&quot;。辅助方法包括将字节转为16进制字符串和将16进制字符串转为字节。
|
1月前
|
搜索推荐 Java
Java排序算法
Java排序算法
20 0
|
1月前
|
搜索推荐 Java
Java基础(快速排序算法)
Java基础(快速排序算法)
25 4