选择排序及其复杂度
Posted Vincent(朱志强)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了选择排序及其复杂度相关的知识,希望对你有一定的参考价值。
// Selection sort in Java
import java.util.Arrays;
class SelectionSort
void selectionSort(int array[])
int size = array.length;
for (int step = 0; step < size - 1; step++)
int min_idx = step;
for (int i = step + 1; i < size; i++)
// To sort in descending order, change > to < in this line.
// Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
// put min at the correct position
int temp = array[step];
array[step] = array[min_idx];
array[min_idx] = temp;
// driver code
public static void main(String args[])
int[] data = 20, 12, 10, 15, 2 ;
SelectionSort ss = new SelectionSort();
ss.selectionSort(data);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
以上是关于选择排序及其复杂度的主要内容,如果未能解决你的问题,请参考以下文章