import java.util.Random;
/**
*
* 排序算法的分類如下:
* 1.插入排序(直接插入排序、折半插入排序、希爾排序);
* 2.交換排序(冒泡泡排序、快速排序);
* 3.選擇排序(直接選擇排序、堆排序);
* 4.歸併排序;
* 5.基數排序。
*
* 關於排序方法的選擇:
* (1)若n較小(如n≤50),可採用直接插入或直接選擇排序。
* 當記錄規模較小時,直接插入排序較好;否則因爲直接選擇移動的記錄數少於直接插人,應選直接選擇排序爲宜。
* (2)若文件初始狀態基本有序(指正序),則應選用直接插人、冒泡或隨機的快速排序爲宜;
* (3)若n較大,則應採用時間複雜度爲O(nlgn)的排序方法:快速排序、堆排序或歸併排序。
*
* */
public class SortTest {
/**
* 初始化測試數組的方法
* @return 一個初始化好的數組
*/
public int[] createArray() {
Random random = new Random();
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
array[i] = random.nextInt(100) - random.nextInt(100);//生成兩個隨機數相減,保證生成的數中有負數
}
System.out.println("-----------原始序列-----------------");
printArray(array);
return array;
}
/**
* 打印數組中的元素到控制檯
*
*/
public void printArray(int[] source) {
for (int i : source) {
System.out.print(i + " ");
}
System.out.println();
}
/**
* 交換數組中指定的兩元素的位置
*
*
*
*/
private void swap(int[] source, int x, int y) {
int temp = source[x];
source[x] = source[y];
source[y] = temp;
}
public static void main(String[] args) {
SortTest sortTest = new SortTest();
int[] array = sortTest.createArray();
System.out.println("==========冒泡排序後(正序)==========");
sortTest.bubbleSort(array, "asc");
System.out.println("==========冒泡排序後(倒序)==========");
sortTest.bubbleSort(array, "desc");
array = sortTest.createArray();
System.out.println("==========倒轉數組後==========");
sortTest.reverse(array);
array = sortTest.createArray();
System.out.println("==========選擇排序後(正序)==========");
sortTest.selectSort(array, "asc");
System.out.println("==========選擇排序後(倒序)==========");
sortTest.selectSort(array, "desc");
array = sortTest.createArray();
System.out.println("==========插入排序後(正序)==========");
sortTest.insertSort(array, "asc");
System.out.println("==========插入排序後(倒序)==========");
sortTest.insertSort(array, "desc");
array = sortTest.createArray();
System.out.println("==========快速排序後(正序)==========");
sortTest.quickSort(array, "asc");
sortTest.printArray(array);
System.out.println("==========快速排序後(倒序)==========");
sortTest.quickSort(array, "desc");
sortTest.printArray(array);
System.out.println("==========數組二分查找==========");
System.out.println("您要找的數在第" + sortTest.binarySearch(array, 74) + "個位子。(下標從0計算)");
}
}
/**
* 冒泡排序----交換排序的一種
* 方法:相鄰兩元素進行比較,如有需要則進行交換,每完成一次循環就將最大元素排在最後(如從小到大排序),下一次循環是將其他的數進行類似操作。
* 性能:比較次數O(n^2),n^2/2;交換次數O(n^2),n^2/4
*
* @param source 要排序的數組
* @param sortType 排序類型
* @return
*/
public void bubbleSort(int[] source, String sortType) {
if (sortType.equals("asc")) { //正排序,從小排到大
for (int i = source.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (source[j] > source[j + 1]) {
swap(source, j, j + 1);
}
}
}
} else if (sortType.equals("desc")) { //倒排序,從大排到小
for (int i = source.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (source[j] < source[j + 1]) {
swap(source, j, j + 1);
}
}
}
} else {
System.out.println("您輸入的排序類型錯誤!");
}
printArray(source);//輸出冒泡排序後的數組值
}
/**
* 直接選擇排序法----選擇排序的一種
* 方法:每一趟從待排序的數據元素中選出最小(或最大)的一個元素, 順序放在已排好序的數列的最後,直到全部待排序的數據元素排完。
* 性能:比較次數O(n^2),n^2/2
* 交換次數O(n),n
* 交換次數比冒泡排序少多了,由於交換所需CPU時間比比較所需的CUP時間多,所以選擇排序比冒泡排序快。
* 但是N比較大時,比較所需的CPU時間佔主要地位,所以這時的性能和冒泡排序差不太多,但毫無疑問肯定要快些。
*
* @param source 要排序的數組
* @param sortType 排序類型
* @return
*/
public void selectSort(int[] source, String sortType) {
if (sortType.equals("asc")) { //正排序,從小排到大
for (int i = 0; i < source.length; i++) {
for (int j = i + 1; j < source.length; j++) {
if (source[i] > source[j]) {
swap(source, i, j);
}
}
}
} else if (sortType.equals("desc")) { //倒排序,從大排到小
for (int i = 0; i < source.length; i++) {
for (int j = i + 1; j < source.length; j++) {
if (source[i] < source[j]) {
swap(source, i, j);
}
}
}
} else {
System.out.println("您輸入的排序類型錯誤!");
}
printArray(source);//輸出直接選擇排序後的數組值
}
/**
* 插入排序
* 方法:將一個記錄插入到已排好序的有序表(有可能是空表)中,從而得到一個新的記錄數增1的有序表。
* 性能:比較次數O(n^2),n^2/4
* 複製次數O(n),n^2/4
* 比較次數是前兩者的一般,而複製所需的CPU時間較交換少,所以性能上比冒泡排序提高一倍多,而比選擇排序也要快。
*
* @param source 要排序的數組
* @param sortType 排序類型
*/
public void insertSort(int[] source, String sortType) {
if (sortType.equals("asc")) { //正排序,從小排到大
for (int i = 1; i < source.length; i++) {
for (int j = i; (j > 0) && (source[j] < source[j - 1]); j--) {
swap(source, j, j - 1);
}
}
} else if (sortType.equals("desc")) { //倒排序,從大排到小
for (int i = 1; i < source.length; i++) {
for (int j = i; (j > 0) && (source[j] > source[j - 1]); j--) {
swap(source, j, j - 1);
}
}
} else {
System.out.println("您輸入的排序類型錯誤!");
}
printArray(source);//輸出插入排序後的數組值
}
/**
* 反轉數組的方法
* @param source 源數組
*/
public void reverse(int[] source) {
int length = source.length;
int temp = 0;//臨時變量
for (int i = 0; i < length / 2; i++) {
temp = source[i];
source[i] = source[length - 1 - i];
source[length - 1 - i] = temp;
}
printArray(source);//輸出到轉後數組的值
}
/**
* 快速排序
* 快速排序使用分治法(Divide and conquer)策略來把一個序列(list)分爲兩個子序列(sub-lists)。
* 步驟爲:
* 1. 從數列中挑出一個元素,稱爲 "基準"(pivot),
* 2. 重新排序數列,所有元素比基準值小的擺放在基準前面,所有元素比基準值大的擺在基準的後面(相同的數可以到任一邊)。在這個分割之後,該基準是它的最後位置。這個稱爲分割(partition)操作。
* 3. 遞歸地(recursive)把小於基準值元素的子數列和大於基準值元素的子數列排序。
* 遞迴的最底部情形,是數列的大小是零或一,也就是永遠都已經被排序好了。雖然一直遞迴下去,但是這個算法總會結束,因爲在每次的迭代(iteration)中,它至少會把一個元素擺到它最後的位置去。
* @param source 待排序的數組
* @param low
* @param high
* @see SortTest#qsort(int[], int, int)
* @see SortTest#qsort_desc(int[], int, int)
*/
public void quickSort(int[] source, String sortType) {
if (sortType.equals("asc")) { //正排序,從小排到大
qsort_asc(source, 0, source.length - 1);
} else if (sortType.equals("desc")) { //倒排序,從大排到小
qsort_desc(source, 0, source.length - 1);
} else {
System.out.println("您輸入的排序類型錯誤!");
}
}
/**
* 快速排序的具體實現,排正序
* @param source
* @param low
* @param high
*/
private void qsort_asc(int source[], int low, int high) {
int i, j, x;
if (low < high) { //這個條件用來結束遞歸
i = low;
j = high;
x = source[i];
while (i < j) {
while (i < j && source[j] > x) {
j--; //從右向左找第一個小於x的數
}
if (i < j) {
source[i] = source[j];
i++;
}
while (i < j && source[i] < x) {
i++; //從左向右找第一個大於x的數
}
if (i < j) {
source[j] = source[i];
j--;
}
}
source[i] = x;
qsort_asc(source, low, i - 1);
qsort_asc(source, i + 1, high);
}
}
/**
* 快速排序的具體實現,排倒序
* @param source
* @param low
* @param high
*/
private void qsort_desc(int source[], int low, int high) {
int i, j, x;
if (low < high) { //這個條件用來結束遞歸
i = low;
j = high;
x = source[i];
while (i < j) {
while (i < j && source[j] < x) {
j--; //從右向左找第一個小於x的數
}
if (i < j) {
source[i] = source[j];
i++;
}
while (i < j && source[i] > x) {
i++; //從左向右找第一個大於x的數
}
if (i < j) {
source[j] = source[i];
j--;
}
}
source[i] = x;
qsort_desc(source, low, i - 1);
qsort_desc(source, i + 1, high);
}
}
/**
* 二分法查找
* 查找線性表必須是有序列表
*
* @param source
* @param key
* @return
*/
public int binarySearch(int[] source, int key) {
int low = 0, high = source.length - 1, mid;
while (low <= high) {
mid = (low + high) >>> 1; //相當於mid = (low + high) / 2,但是效率會高些
if (key == source[mid]) {
return mid;
} else if (key < source[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
字符串排序:
public class StringSort {
public static void main(String []args) {
String[] s={"a","b","c","d","m","f"};
for(int i=s.length-1;i>=1;i--){
for(int j=0;j<=i-1;j++) {
if(s[j].compareTo(s[j+1])<0) {
String temp=null;
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
for(String a:s){
System.out.print(a+" ");
}
}
}