1 Star 1 Fork 0

CS-IMIS-23/20172313yukunpeng

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Sorting.java 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
package srcNo8;
//********************************************************************
// Sorting.java Author: Lewis/Loftus
//
// Demonstrates the selection sort and insertion sort algorithms.
//********************************************************************
public class Sorting {
//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort(Comparable[] list) {
int min;
Comparable temp;
for (int index = 0; index < list.length - 1; index++) {
min = index;
for (int scan = index + 1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
// Swap the values
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort(Comparable[] list) {
for (int index = 1; index < list.length; index++) {
Comparable key = list[index];
int position = index;
// Shift larger values to the right
while (position > 0 && key.compareTo(list[position - 1]) < 0) {
list[position] = list[position - 1];
position--;
}
list[position] = key;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172313yukunpeng.git
git@gitee.com:CS-IMIS-23/20172313yukunpeng.git
CS-IMIS-23
20172313yukunpeng
20172313yukunpeng
master

搜索帮助