常用排序算法二
SELECTION SORT:选择排序算法,每次从未完成排序的部分选出最小的插入未完成排序元素的最前面
代码实现比较好写:
import java.util.*;
public class SelectSort
{
public static void main(String[] args)
{
System.out.println("Hello World!");
int [] a = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
selectionSort(a);
}
public static void selectionSort(int [] a)
{
int length = a.length;
//int min = -1;
int minPos = -1;//记录当前最小的值在数组中的位置
for(int i = 0; i < length-1; i++)//需要从左开始循环length-1次
{
minPos = i;//循环前,将最前面没有排序的值作为最小值记录下来其位置
for( int j = i + 1; j < length; j++)
{
if(a[minPos] > a[j])//从左到右开始比较还未完成排序的数字,保存最小数字的位置
{
minPos = j;
}
}
System.out.print(a[i]+":"+a[minPos]);
int tem = a[i];//交换最小数字和刚开始排序开始的位置
a[i] = a[minPos];
a[minPos] = tem;
System.out.println(Arrays.toString(a));
}
}
}
运行结果:
E:\java\java_test\sortprogram>java SelectSort
Hello World!
3:2[2, 44, 38, 5, 47, 15, 36, 26, 27, 3, 46, 4, 19, 50, 48]
44:3[2, 3, 38, 5, 47, 15, 36, 26, 27, 44, 46, 4, 19, 50, 48]
38:4[2, 3, 4, 5, 47, 15, 36, 26, 27, 44, 46, 38, 19, 50, 48]
5:5[2, 3, 4, 5, 47, 15, 36, 26, 27, 44, 46, 38, 19, 50, 48]
47:15[2, 3, 4, 5, 15, 47, 36, 26, 27, 44, 46, 38, 19, 50, 48]
47:19[2, 3, 4, 5, 15, 19, 36, 26, 27, 44, 46, 38, 47, 50, 48]
36:26[2, 3, 4, 5, 15, 19, 26, 36, 27, 44, 46, 38, 47, 50, 48]
36:27[2, 3, 4, 5, 15, 19, 26, 27, 36, 44, 46, 38, 47, 50, 48]
36:36[2, 3, 4, 5, 15, 19, 26, 27, 36, 44, 46, 38, 47, 50, 48]
44:38[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 46, 44, 47, 50, 48]
46:44[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
46:46[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
47:47[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
50:48[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
E:\java\java_test\sortprogram>
INSERECTION SORT:插入排序-----代码加了注释,感觉很详细了。
import java.util.*;
public class InsertSort
{
public static void main(String[] args)
{
System.out.println("Hello World!");
int [] a = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
insertionSort(a);
}
public static void insertionSort(int a[])
{
int length = a.length;
for(int i = 1; i < length; i++)//从第二个元素开始,第一元素默认以排好
{
int insert = a[i];//准备要插入的元素,把要插入元素的位置空出来,为了能移动元素
for(int j = i-1; j >= 0; j--)//从要插入元素位置往前查,以便确认插入元素要插入的位置
{
if(a[j] > insert)//如果当前元素大于要准备插入的元素,则将该元素往后移动一位
{
int tem = a[j];
a[j] = a[j + 1];
a[j+1] = tem;
}
else//如果当前元素小要准备插入的元素,则将要准备插入的元素插入该元素的后面,同时退出这次循环,开始排下一个元素
{
int tem1 = a[j+1];
a[j+1] = insert;
break;
}
}
System.out.println(Arrays.toString(a));
}
}
}
运行结果:
Hello World!
[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 38, 44, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 38, 44, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 38, 44, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 38, 44, 47, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 36, 38, 44, 47, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 26, 36, 38, 44, 47, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 26, 27, 36, 38, 44, 47, 2, 46, 4, 19, 50, 48]
[2, 3, 5, 15, 26, 27, 36, 38, 44, 47, 46, 4, 19, 50, 48]
[2, 3, 5, 15, 26, 27, 36, 38, 44, 46, 47, 4, 19, 50, 48]
[2, 3, 4, 5, 15, 26, 27, 36, 38, 44, 46, 47, 19, 50, 48]
[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
E:\java\java_test\sortprogram>
公众号:笑谭枫昇
![](https://note.ztxzy.top/assets/qrcode_for_gh_d93a9264b4c4_258 1-20240521113700-6vqy8ha.jpg)
博客地址:
https://www.ztxzy.top本文来自博客园,作者:笑谭枫昇,转载请注明原文链接:https://www.cnblogs.com/xiaochenztx/p/8635536.html