java 之选择排序与冒号排序的详解
選擇排序:
核心思想:直接從待排數(shù)據(jù)中"選出"最小,或最大的數(shù)據(jù),直接放在序列的前列位置,直到所有待排元素全部排列完畢
java代碼的實現(xiàn):
public static void selectSort(int[] a)
{
? ? int minIndex = 0;
? ? int temp = 0;
? ? if((a == null)) ||(a.length-1;i++){
????? ? return;
? ? for(int i = 0; i<a.length-1;i++){
????? ? minIndex = i;//無序區(qū)的最小數(shù)據(jù)數(shù)組下標(biāo)
????? ? for(int j = j+1;j<a.length;j++){
????????????//在無序區(qū)中找到最小數(shù)據(jù)并保存其數(shù)組下標(biāo)? ??
????????? ? if(a[j]<a[minIndex]){
????????????? ? minIndex = j;
????????? ? }
????? ? }
????? ? //將最小元素放到本次循環(huán)的前端
????? ? temp = a[i];
????? ? a[i] = a[minIndex];
????? ? a[minIndex] = temp;
????? ? }
}
冒號排序:
核心思想:通過比較相鄰的兩個數(shù)的大小,按照我們需要的排序規(guī)則使其排序,
java代碼的實現(xiàn):
public static void bubbleSort(int []arr){
? ? for(int i = 0;i<arr.length-1;i++){
? ? ????for(int j = 0;j<arr.length-i-1;j++){
????????? ? if(arr[j] > arr[j+1]){
????????????? ? int temp = arr[j];
????????????? ? arr[j] = arr[j+1];
????????????? ? arr[j+1] = temp;
????????????????}
????????? ? }
????? ? }
}
友情鏈接:點(diǎn)擊打開鏈接
總結(jié)
以上是生活随笔為你收集整理的java 之选择排序与冒号排序的详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 的基本语法
- 下一篇: java 抽象类,接口,object类详