public class Test {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5 };
选择排序(a);
// 插入排序(a); System.out.print("排序后:");
for (int n : a) {
System.out.print(n + " ");
}
} static void 选择排序(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
// 定位到i之后的最值
for (int j = i + 1; j < a.length; j++) {
if (a[i] < a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
} static void 插入排序(int[] arr) { for (int i = 1; i < arr.length; i++) {
System.out.print(i + ":");
// 假定(0->(j-1))是有序的,看j排在哪里
for (int j = i; j > 0; j--) {
if (arr[j] > arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
} else {
// 假定前面是有序的,则只需要交换一次即可
break;
}
}
for (int n : arr) {
System.out.print(n + " ");
}
System.out.println();
} }
}

 

快速排序

package ah.sort;

public class QuickSortMy {
static void printArray(int a[]) {
for (int i : a) {
System.out.print(i + "\t");
}
System.out.println();
} static void qsort(int a[], int left, int right) {
if (left >= right) {
return;
}
printArray(a);
int key = a[left];
int i = left, j = right;
while (i < j) {
while (i < j && a[j] > key) {
j--;
}
if (i < j) {
System.out.printf("a[%d]=%d <- a[%d]=%d\n", i, a[i], j, a[j]);
a[i++] = a[j];
} while (i < j && a[i] < key) {
i++;
}
if (i < j) {
System.out.printf("a[%d]=%d -> a[%d]=%d\n", i, a[i], j, a[j]);
a[j--] = a[i];
} }
a[i] = key;
printArray(a);
qsort(a, left, i - 1);
qsort(a, i + 1, right);
} public static void main(String[] args) {
int a[] = { 3, 4, 5, 1, 2 };
qsort(a, 0, a.length - 1);
} }

最新文章

  1. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
  2. LeetCode - Minimum Depth of Binary Tree
  3. python程序打包成.exe----pyinstaller工具
  4. angularjs 权威指南 版本 1.2.6
  5. .net自动生成数据库表的类
  6. cocos代码研究(1)sprite学习笔记
  7. java 操作数据库
  8. Building Plugins for iOS
  9. hibernate分页实现
  10. 图像载入 imread()[OpenCV 笔记4]
  11. iOS画面模糊
  12. ASP.NET Web API中的JSON和XML序列化
  13. 一个完整的PHP类包含的七种语法说明
  14. Eclipse默认编码设置
  15. 将 C# 枚举反序列化为 JSON 字符串 实践
  16. mysql 免安装与 忘记root密码 密码过期
  17. zuoye
  18. 【计算机网络】OSI七层模型图解
  19. 集合框架_DAY17
  20. 【bzoj5118】Fib数列2 费马小定理+矩阵乘法

热门文章

  1. java替换字符串中的World为Money
  2. Linux目录结构下部
  3. Jsの数组练习-求一组数中的最大值和最小值,以及所在位置
  4. Mac 下配置Lua环境
  5. 蓝牙协议分析(8)_BLE安全机制之白名单
  6. 【PL/SQL基础知识】结构
  7. Linux下初次使用github
  8. Python全栈之路----进制运算
  9. PTA——洗牌
  10. codeforces 804A Find Amir 思维/水题