目錄插入排序
選擇排序
歸併排序
非比較類排序
快速排序優化
氣泡排序
void bubble_sort(int arr, int low, int high) }}
}
快速排序void quick_sort(int arr, int low, int high)
arr[i] = tmp;
quick_sort(arr, low, i-1);
quick_sort(arr, i+1, high);
}
簡單插入排序void insert_sort(int arr, int low, int high)
arr[j+1] = tmp;
}}
希爾排序void shell_sort(int arr, int low, int high)
arr[j+t] = tmp;}}
}
簡單選擇排序void select_sort(int arr, int low, int high)
}swap(arr[min_index],arr[i]);
}}
堆排序void heapify(int arr, int root, int low, int len)
}void build_max_heap(int arr, int low, int len)
}void heap_sort(int arr, int low, int high)
}
void merge(int arr, int tmp, int low, int high)
while (i <= mid) tmp[k++] = arr[i++];
while (j <= high) tmp[k++] = arr[j++];
for (k = low; k <= high; ++k) arr[k] = tmp[k];
}void merge_sort(int arr, int low, int high)
void counting_sort(int arr, int low, int high, int max_num)
int j = low;
for (int i = 0; i <= max_num; ++i)
}
void bucket_sort(int arr, int low, int high)
// 計算桶的數量
int len = high-low+1;
int bucket_num = (max_num-min_num)/len + 1;
vectorbucket_arr[bucket_num];
// 將每個元素放入桶
for (int i = low; i <= high; ++i)
// 對每個桶進行排序,可以使用快排
for (int i = 0; i < bucket_num; ++i)
// 將桶中的元素賦值到原序列
int k = low;
for (int i = 0; i < bucket_num; ++i)
}}
void radix_sort(int arr, int low, int high, int max_digit)
int pos = low;
for (int j = 0; j < 10; ++j) }}
}
#include using namespace std;
int arr[1000005];
// 插入排序
void insert_sort(int arr, int low, int high)
arr[j+1] = tmp;
}}// 快速排序
void quick_sort(int arr, int low, int high)
// 三數取中,並將中間值放在最左邊
int mid = low + (high-low)/2;
if (arr[mid] > arr[high]) swap(arr[mid],arr[high]);
if (arr[low] > arr[high]) swap(arr[low],arr[high]);
if (arr[mid] > arr[low]) swap(arr[low],arr[mid]);
// 快排
int i = low, j = high, tmp = arr[low];
while (i < j)
arr[i] = tmp;
quick_sort(arr, low, i-1);
quick_sort(arr, i+1, high);
}int main()
C 排序演算法
最基本的 氣泡排序 c code using system namespace bubblesorter j public class mainclass bubblesorter sh new bubblesorter sh.sort iarrary for intm 0 m iarrary.le...
C 排序演算法
protected int bubbleup int array 氣泡排序 return array public int selectionsorter int list 選擇排序 int temp list min list min list i list i temp return list ...
C 演算法排序
不廢話,上 using system using system.collections.generic using system.linq using system.text using system.threading.tasks namespace 排序演算法 陣列 氣泡排序 選則排序 arra...