データ構造の5つの簡単な並べ替え
今日はデータ構造の並べ替えを復習しました.今はバックアップ、バックアップします.
/**
*
* :
* 1:
* 2:
* 3:
*/
public void InsertSort(int[] insertList) {
int j = 0, temp;
for (int i = 1; i < insertList.length; i++) {
if (insertList[i] < insertList[i - 1]) {
temp = insertList[i];
insertList[i] = insertList[i - 1];
for (j = i - 1; j > 0; j--) {
if (insertList[j] > temp) {
insertList[j] = insertList[j - 1];
} else {
break;
}
}
insertList[j] = temp;
}
}
}
/**
*
* : , ,
* , , 。
*/
public void maoPao(int[] maoPaoList) {
int temp, total = maoPaoList.length;
for (int i = 0; i < total - 1; i++) {
for (int j = 0; j < total - 1 - i; j++) {
if (maoPaoList[j] > maoPaoList[j + 1]) {
temp = maoPaoList[j + 1];
maoPaoList[j + 1] = maoPaoList[j];
maoPaoList[j] = temp;
}
}
}
}
/**
*
* :
* , ,
* ,
*
*/
private static int[] quickList = { 5, 3, 6, 2, 7, 1, 9, 54, 23, 1, 3, 5, 7 };
public void quickSort(int low, int high) {
if (low < high) {
int pivotdoc = partition(low, high);
quickSort(low, pivotdoc - 1);
quickSort(pivotdoc + 1, high);
}
}
public int partition(int low, int high) {
int pivot = quickList[low];
quickList[0] = quickList[low];
while (low < high) {
while (low < high && quickList[high] >= pivot)
high--;
quickList[low] = quickList[high];
while (low < high && quickList[low] <= pivot)
low++;
quickList[high] = quickList[low];
}
quickList[low] = quickList[0];
return low;
}
/**
*
* :
* , ,
*
*/
public void selectSort(int[] selectList) {
int count, temp;
for (int i = 0; i < selectList.length; i++) {
count = i;
for (int j = i + 1; j < selectList.length; j++) {
if (selectList[count] > selectList[j]) {
count = j;
}
}
temp = selectList[count];
selectList[count] = selectList[i];
selectList[i] = temp;
}
}
/**
*
* :
*
*
*/
public int[] mergeSort(int[] mergeList, int low, int high) {
int[] mergeCopy = new int[high + 1];
if (low == high) {
mergeCopy[low] = mergeList[high];
} else {
int mid = (low + high) / 2;
int[] sqList1 = new int[high + 1];
int[] sqList2 = new int[high + 1];
int[] sqList = new int[high + 1];
sqList1 = mergeSort(mergeList, low, mid);
sqList2 = mergeSort(mergeList, mid + 1, high);
int m = low, n = mid + 1;
while (m <= mid) {
sqList[m] = sqList1[m];
m++;
}
while (n <= high) {
sqList[n] = sqList2[n];
n++;
}
mergeCopy = merge(sqList, low, mid, high);
}
return mergeCopy;
}
public int[] merge(int[] sqList, int low, int mid, int high) {
int[] mergeList = new int[high + 1];
int i, j, k = low;
for (i = low, j = mid + 1; i <= mid && j <= high;) {
if (sqList[i] < sqList[j]) {
mergeList[k] = sqList[i];
k++;
i++;
} else {
mergeList[k] = sqList[j];
k++;
j++;
}
}
while (i <= mid) {
mergeList[k] = sqList[i];
k++;
i++;
}
while (j <= high) {
mergeList[k] = sqList[j];
k++;
j++;
}
return mergeList;
}