快速排序
# 快速排序
void QuickSort(int *array, int start, int end){
if(start > end || array == NULL)
return;
int pivot = array[start];
int left = start;
int right = end;
while(right > left){
while(right > left && array[right] >= pivot)
--right;
array[left] = array[right];
while(right > left && pivot >= array[left])
++left;
array[right] = array[left];
array[left] = pivot;
}
QuickSort(array, start, left-1);
QuickSort(array, left+1, end);
}
二分查找
# 二分查找递归
int BinarySearchRecursion(int *arr, int value, int start, int end) {
if (start > end || arr == NULL)
return -1;
int mid = (start + end) >> 1;
if (arr[mid] == value) {
return mid;
} else if (value > arr[mid]) {
return BinarySearchRecursion(arr, value, mid + 1, end);
} else {
return BinarySearchRecursion(arr, value, start, mid - 1);
}
}
# 二分查找
int BinarySearch(int arr[], int len, int value) {
if (arr == NULL || len < 1)
return -1;
int start = 0;
int end = len - 1;
while (start < end) {
int mid = (start + end) >> 1;
if (arr[mid] == value)
return mid;
else if (arr[mid] > value)
end = mid - 1;
else
start = mid + 1;
}
return -2;
}
冒泡排序
# 冒泡排序
void BubbleSort(int *arr, int len) {
bool flag = true;
int length = len;
while(flag){
flag = false;
for (int i = 1; i < length; ++i) {
if(arr[i-1]>arr[i]){
swap(arr[i-1], arr[i]);
flag = true;
}
}
--length;
}
}
归并算法
void MergeSort(int array[], int TempArray[], int left, int right) {
if (right > left) {
int middle = (left + right) >>1;
MergeSort(array, TempArray, left, middle);
MergeSort(array, TempArray, middle + 1, right);
Merge(array, TempArray, left, middle, right);
}
}
void MergeArray(int array[], int TempArray[], int left, int middle, int right) {
int first = left;
int end = right;
int mid = middle;
int mid_right = middle + 1;
int key = 0;
while (first <= mid && mid_right <= end) {
if (array[first] < array[mid_right]) {
TempArray[key++] = array[first++];
} else {
TempArray[key++] = array[mid_right++];
}
}
while (first <= mid) {
TempArray[key++] = array[first++];
}
while (mid_right <= end) {
TempArray[key++] = array[mid_right++];
}
for (int i = 0; i < key; ++i) {
array[left + i] = TempArray[i];
}
}
void mergeSort(int array[]){
int length = sizeof(array) / sizeof(int);
int temp[length];
MergeSort(array, temp, 0, length-1);
}
选择排序
# 选择排序
void SelectSort(int a[], int len) {
int temp;
int nIndex;
for (int i = 0; i < len - 1; i++) {
nIndex = i;
for (int j = i + 1; j < len; j++) {
if (a[j] < a[nIndex])
nIndex = j;
}
if (nIndex != i) {
temp = a[i];
a[i] = a[nIndex];
a[nIndex] = temp;
}
}
}
插入排序
# 插入排序
void InsertSort(int arr[], int len) {
for (int i = 1; i < len; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
Shell排序
# Shell排序
void ShellSort(int *number, int len) {
int gap = len >> 1;
while (gap > 0) {
for (int k = 0; k < gap; k++) {
for (int i = k + gap; i < len; i += gap) {
for (int j = i - gap; j >= k; j -= gap) {
if (number[j] > number[j + gap])
swap(number[j], number[j + gap]);
else
break;
}
}
}
gap >>= 2;
}
}