/**
* 选择排序
* <p>
* 思想:
* 1、每次循环都从还未排序的部分中找出最小(最大)元素;
* 2、然后放到已排序部分的末尾;
* 3、重复上述步骤直至所有元素均被处理
*/
public class SelectionSort implements Sortable
{
@Override
public void sort(int[] sourceArray)
{
if (sourceArray == null || sourceArray.length < 1)
{
return;
}
selectionSort(sourceArray);
}
/**
* @param arr
*/
public void selectionSort(int[] arr)
{
int minIndex = 0;
for (int i = 0; i < arr.length; i++)
{
minIndex = i;
// 从还未排序的部分中找出最小元素
for (int j = i + 1; j < arr.length; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
// 若该最小元素不在已排序部分的末尾,则交换位置
if (minIndex != i)
{
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
}
选择排序-SelectionSort
最新推荐文章于 2025-09-13 00:00:00 发布
本文详细介绍了一种简单直观的排序算法——选择排序。该算法通过反复查找未排序部分的最小值并将其放置到已排序部分的末尾来实现排序。文章提供了完整的Java实现代码,并解释了其基本思想和实现步骤。
166

被折叠的 条评论
为什么被折叠?



