排序算法
冒泡排序
从要排序序列的第一个元素开始,不断比较相邻元素的值,发现逆序则交换,将值较大的元素逐渐从前向后移动。
每找到待排序序列的最大值时,就将该最大值固定在待排序序列的尾部,且每找到一个待排序序列最大值需要循环一次,n 个值则需要循环 n 次,但最后一个值无需比较,则实际需循环 n-1 次,即 i < arr.length - 1 。
void bubbleSort (int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
当然,该方法还可以进行优化。
定义一个布尔变量 flag ,用来标记每轮是否进行了交换。在每轮遍历开始时,将 flag 设置为 false。若当轮没有发生交换,即此时 flag 依然为 false,说明此时数组已经按照升序排列。此时外层循环直接退出,排序结束。
void bubbleSort (int arr[]) {
boolean flag = false;
for (int i = 0; i < arr.length - 1; i++) {
flag = false;
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
if (!flag) {
break;
}
}
}
复杂度分析:
选择排序
选择排序每次会从未排序区间中找到最小的元素,将其放到已排序区间的末尾。同样,选择排序也需要比较 n - 1 轮,最后一轮无需比较。
void selectSort (int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
复杂度分析:
直接插入排序
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
} SqList;
void insertSort(SqList &L)
{
int i, j;
for (i = 2; i <= L.length; i++)
{
if (L.elem[i] < L.elem[i - 1])
{
L.elem[0] = L.elem[i];
L.elem[i] = L.elem[i - 1];
for (j = i - 2; L.elem[0] < L.elem[j]; j--)
{
L.elem[j + 1] = L.elem[j];
}
L.elem[j + 1] = L.elem[0];
}
}
}
希尔排序
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
} SqList;
void shellInsert(SqList &L, int dk)
{
int i, j;
for (i = dk + 1; i <= L.length; i++)
{
if (L.elem[i] < L.elem[i - dk])
{
L.elem[0] = L.elem[i];
for (j = i - dk; j > 0 && L.elem[0] < L.elem[j]; j -= dk)
{
L.elem[j + dk] = L.elem[j];
}
L.elem[j + dk] = L.elem[0];
}
}
}
void shellSort(SqList &L, int dlta[], int t)
{
int k;
for (k = 0; k < t; k++)
{
shellInsert(L, dlta[k]);
}
}
快速排序
int Partition(int rcd[], int low, int high)
{
int pivot = rcd[low];
while (low < high)
{
while (low < high && rcd[high] >= pivot)
high--;
rcd[low] = rcd[high];
while (low < high && rcd[low] <= pivot)
low++;
rcd[high] = rcd[low];
}
rcd[low] = pivot;
return low;
}
void QuickSort(int rcd[], int low, int high)
{
int pivotloc;
if (low < high)
{
pivotloc = Partition(rcd, low, high);
QuickSort(rcd, low, pivotloc - 1);
QuickSort(rcd, pivotloc + 1, high);
}
}
void quick_sort(int rcd[], int low, int high){
if(low >= high){
return;
}
int pivot = rcd[low];
int left = low;
int right = high;
while (low < high){
while (low < high && rcd[high] >= pivot)
high--;
rcd[low] = rcd[high];
while (low < high && rcd[low] <= pivot)
low++;
rcd[high] = rcd[low];
}
rcd[low] = pivot;
quick_sort(rcd, left, low-1);
quick_sort(rcd, high+1, right);
}
归并排序
void Merge(int SR[], int TR[], int i, int m, int n){
int k, j;
for (k = i, j = m + 1; i <= m && j <= n; k++){
if (SR[i] <= SR[j]){
TR[k] = SR[i++];
}else{
TR[k] = SR[j++];
}
}
while (i <= m)
TR[k++] = SR[i++];
while(j <= n)
TR[k++] = SR[j++];
}
void MergeSort(int R1[], int R2[], int i, int low, int high){
int mid;
if(low == high){
if(i%2 == 1){
R2[low] = R1[low];
}
}else{
mid = (low + high) / 2;
MergeSort(R1, R2, i+1, low, mid);
MergeSort(R1, R2, i+1, mid+1, high);
if(i%2 == 1){
Merge(R1, R2, low, mid, high);
}else{
Merge(R2, R1, low, mid, high);
}
}
}
void merge_sort(int SR[], int TR[], int low, int high){
int i, j, k;
if(low >= high){
return;
}
int mid = (low + high) / 2;
merge_sort(SR, TR, low, mid);
merge_sort(SR, TR, mid+1, high);
for(k=low, i=low, j=mid+1; i<=mid && j<=high; k++){
if(SR[i] <= SR[j]){
TR[k] = SR[i++];
}else{
TR[k] = SR[j++];
}
}
while(i <= mid)
TR[k++] = SR[i++];
while(j <= high)
TR[k++] = SR[j++];
for(k=low; k<=high; k++){
SR[k] = TR[k];
}
}
查找算法
折半查找
int BinSearch(int rcd[], int key, int low, int high){
int mid = (low + high) / 2;
if(low > high)
return -1;
if(rcd[mid] == key)
return mid;
else if(rcd[mid] > key)
return BinSearch(rcd, key, low, mid-1);
else
return BinSearch(rcd, key, mid+1, high);
}
int bin_search(int rcd[], int key, int low, int high){
int mid;
while(low <= high){
mid = (low + high) / 2;
if(rcd[mid] == key)
return mid;
else if(rcd[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
|