2 Star 0 Fork 0

CS-IMIS-23 / 20172311hai

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Sorting2.java 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
package chap10;
//********************************************************************
// Sorting.java Author: Lewis/Loftus
//
// Demonstrates the selection sort and insertion sort algorithms.
//********************************************************************
public class Sorting2
{
//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort(Comparable[] list)
{
int max;
Comparable temp;
for (int index = 0; index < list.length-1; index++)
{
max = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[max]) >0)
max = scan;
// Swap the values
temp = list[max];
list[max] = 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;
}
}
}
Java
1
https://gitee.com/CS-IMIS-23/20172311hai.git
git@gitee.com:CS-IMIS-23/20172311hai.git
CS-IMIS-23
20172311hai
20172311hai
master

搜索帮助