基本思想
将指定排序位置元素与其他数组元素分别对比,如果满足条件就交换元素。与冒泡排序的区别在于不是交换相邻元素,而是把满足条件的元素与指定排序位置进行交换,这样排序好的位置被逐渐扩大,直至整个数组都变成已排序好的格式。
算法实现
package com.edu.jsu;
import java.util.Arrays;
public class Text {
public static void main(String[] args) {
int[] a = new int[]{6,7,4,7,8,9,2,1,3};
Text sorter = new Text();
sorter.sort(a);
for(int i : a)
{
System.out.print(i + " ");
}
}
public void sort(int[] a)
{
for(int i = 0; i < a.length; 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;
}
}
}
}
}