排序算法有:
冒泡排序、插值排序、選擇排序、HASH排序、快速排序
冒泡排序:
public static void bubbleSort(int[] array) {
for (int i = 1; i < array.length; i++) {
for (int j = 0; j < i; j++) {
if (array[i] < array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
快速排序:
public class QuickSort {
public void quickSort(String[] strDate, int left, int right) {
String middle, tempDate;
int i, j;
i = left;
j = right;
middle = strDate[(i + j) / 2];
do {
while (strDate[i].compareTo(middle) < 0 && i < right)
i++; // 找出左邊比中間值大的數
while (strDate[j].compareTo(middle) > 0 && j > left)
j--; // 找出右邊比中間值小的數
if (i <= j) { // 將左邊大的數和右邊小的數進行替換
tempDate = strDate[i];
strDate[i] = strDate[j];
strDate[j] = tempDate;
i++;
j--;
}
} while (i <= j); // 當兩者交錯時停止
if (i < right) {
quickSort(strDate, i, right);
}
if (j > left) {
quickSort(strDate, left, j);
}
}
public static void main(String[] args) {
String[] strVoid = new String[] { "11", "66", "22", "0", "55", "22", "0", "32" };
QuickSort sort = new QuickSort();
sort.quickSort(strVoid, 0, strVoid.length - 1);
for (int i = 0; i < strVoid.length; i++) {
System.out.println(strVoid[i] + " ");
}
}
}
遠近互聯技術-劉 整理發布,希望能對同是技術的你有所幫助。
遠近互聯專業提供網站建設、APP開發、網站優化、外貿網站SEO、微信運營的品牌整合營銷服務,讓客戶通過網絡品牌建立與網絡傳播提高業績。






