氣泡排序
public class bubblesort }}
}// 改進1
public static void sort2(int array)
}if (issorted) break;}}
// 改進2
public static void sort3(int array)
}sortborder = lastexchangeindex;
if (issorted) break;}}
public static void main(string args) ;
sort(array);
system.out.println(arrays.tostring(array));
}}
選擇排序
public class selectsort
// 將找到的最小值和i位置所在的值進行交換
if (minindex != i) }}
public static void main(string args) ;
sort(array);
system.out.println(arrays.tostring(array));
}}
插入排序
public class insertsort
array[j] = temp;}}
public static void main(string args) ;
sort(array);
system.out.println(arrays.tostring(array));
}}
希爾排序
public class shellsort
array[j + step] = temp;}}
}public static void sort(int array)
array[j] = temp;}}
}}
public static void main(string args) ;
sort2(array);
system.out.println(arrays.tostring(array));
}}
歸併排序
public class mergesort
// 合併
private static void merge(int array, int start, int mid, int end) else
}// 左側小集合還有剩餘,依次放入大集合尾部
while (p1 <= mid)
// 右側小集合還有剩餘,依次放入大集合尾部
while (p2 <= end)
// 把大集合的元素複製回原陣列
/*for (int i = 0; i < len; i++) */
system.arraycopy(temparr, 0, array, start, len);
}public static void main(string args) ;
mergesort(array, 0, array.length - 1);
system.out.println(arrays.tostring(array));
}}
快速排序
public class quicksort
// 棧代替遞迴
public static void quicksort2(int array, int startindex, int endindex)
if (end > pivotindex + 1) }}
// 挖坑法
private static int partition(int array, int startindex, int endindex)
array[pindex] = array[endindex];
pindex = endindex;
// 從左向右進行比較
while (array[startindex] < pivot && startindex < endindex)
array[pindex] = array[startindex];
pindex = startindex;
}array[pindex] = pivot;
return pindex;
}// 指標交換法
private static int partition2(int array, int startindex, int endindex)
while (array[startindex] < pivot && startindex < endindex)
if (startindex < endindex)
}int temp = array[startindex];
array[startindex] = pivot;
array[pindex] = temp;
return startindex;
}public static void main(string args) ;
quicksort2(arr, 0, arr.length-1);
system.out.println(arrays.tostring(arr));
}}
常見排序演算法
一.選擇排序 1.概念 每次從無序的子陣列裡面選擇最小的數,放在有序區的後面 既與無序區的首元素交換 不穩定排序 時間複雜度o n 2 輔助儲存o 1 2.實現 int selection sort int a,int len len為陣列元素個數 二.氣泡排序 1.概念 重複訪問數列n 1次,每次...
常見排序演算法
1 插入排序 直接插入排序,是一種最簡單的排序方法,它的基本操作是將乙個記錄插入到已排好序的有序表中,從而得到乙個新的 記錄數增1的有序表。初始 38 65 27 76 13 i 1 13 選13為監視哨並假設為乙個有序序列 i 2 13 38 待插入元素38 13 i 3 13 38 65 待插入...
常見排序演算法
排序演算法作為常用的基本演算法,今天就來總結一下各種經典排序演算法,這裡只貼出 對演算法的文字描述可以在課本或其它部落格上找到很多詳盡的敘述,這裡直接上 而不是常見演算法書上的偽 希望對正在努力學資料結構與演算法的朋友們有幫助 1 氣泡排序 void bubblesort t a,int n if ...