快速排序

#include <iostream>

using namespace std;

void Print(int arr[], int n) {
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << ' ';
    }
    cout << endl;
}

int Pivot(int arr[], int left, int right) {
    int key = arr[left];
    int l = left, r = right;

    while (l < r) {
        while (l < r && key <= arr[r]) {
            --r;
        }
        arr[l] = arr[r];

        while (l < r && key >= arr[l]) {
            ++l;
        }
        arr[r] = arr[l];
    }

    arr[l] = key;
    return l;
}

void QuickSort(int arr[], int left, int right) {
    if (left < right) {
        int p = Pivot(arr, left, right);
        QuickSort(arr, left, p - 1);
        QuickSort(arr, p + 1, right);
    }
}

int main() {
    int n = 10;
    int arr[] = {5,3,1,7,2,4,8,6,3,4};
    cout << "before sorted: ";
    Print(arr, n);
    cout << "after sorted: ";
    QuickSort(arr, 0, n - 1);
    Print(arr, n);

    system("pause");
    return 0;
}

堆排序

/*
堆排序
*/
#include <iostream>

using namespace std;

// 打印数组
void print(int* arr);
// 交换两个元素
void swap(int& a, int& b);
// 调整堆
void HeapAdjust(int* arr, int n, int i);
// 堆排序主程序
void HeapSort(int* arr, int n);

void print(int* arr, int n) {
	for (int i = 0; i < n; ++i) {
		cout << arr[i] << ' ';
	}
	cout << endl;
}

void swap(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}

void HeapAdjust(int* arr, int n, int i) {
	int largest = i;
	int left = i * 2 + 1;
	int right = i * 2 + 2;
	
	// 找到最大的元素的索引
	if (left < n && arr[largest] < arr[left]) {
		largest = left;
	}
	if (right < n && arr[largest] < arr[right]) {
		largest = right;
	}
	
	// 交换两个元素
	if (largest != i) {
		swap(arr[i], arr[largest]);
		HeapAdjust(arr, n, largest);
	}
}

void HeapSort(int* arr, int n) {
	// 建堆
	for (int i = (n - 1) / 2; i >= 0; --i) {
		HeapAdjust(arr, n, i);
	}
	
	// 将最大的元素交换到末尾
	for (int i = n - 1; i > 0; --i) {
		swap(arr[i], arr[0]);
		HeapAdjust(arr, i, 0);
	}
}

int main() {
	int arr[] = {30, 20, 40, 10, 0, 60, 80, 70};
	print(arr, 8);
	HeapSort(arr, 8);
	print(arr, 8);
}

冒泡排序

/*
冒泡排序
*/

#include <iostream>

using namespace std;

void Swap(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}

void Print(int* arr, int n) {
	for (int i = 0; i < n; ++i) {
		cout << arr[i] << ' ';
	}
	cout << endl;
}

// 朴素版冒泡排序
void NavieBubbleSort(int* arr, int n) {
	for (int i = 0; i < n - 1; ++i) {
		// 外层循环只需要 n - 1 次
		for (int j = 0; j < n - 1 - i; ++j) {
			// 第 i 次内层循环需要 n - i 次
			if (arr[j] > arr[j + 1]) {
				Swap(arr[j], arr[j + 1]);
			}
		}
	}
}

// 改进版冒泡排序
void AdvanceBubbleSort(int* arr, int n) {
	// 改进点一:内层循环没有交换操作发生,则排序完成
	// 改进点二:已经有序的后半部分无需进行内层循环

	bool flag = false;  // 是否发生交换标志位
	int max_dis = n - 1;  //
	int pos = 0;

	for (int i = 0; i < n - 1; ++i) {
		flag = true;
		for (int j = 0; j < max_dis; ++j) {
			if (arr[j] > arr[j + 1]) {
				Swap(arr[j], arr[j + 1]);
				flag = false;
				pos = j;
			}
		}
		max_dis = pos;

		if (flag) {
			break;
		}
	}
}

int main() {
	int arr[] = {30, 20, 40, 10, 0, 90, 80, 70};
	Print(arr, 8);
	NavieBubbleSort(arr, 8);
	Print(arr, 8);
}

归并排序

/*
归并排序
*/
#include <iostream>

using namespace std;

void Print(int* arr, int n) {
	for (int i = 0; i < n; ++i) {
		cout << arr[i] << ' ';
	}
	cout << endl;
}

void Merge(int* arr, int left, int mid, int right) {
	// 区间 [left, mid] 和区间 [mid + 1, right] 都是有序的
	int n1 = mid - left + 1;
	int n2 = right - mid;
	int L[n1 + 1];  // 为避免判断索引是否越界
	int R[n2 + 1];  // 最后一个元素设置为标志位

	for (int i = 0; i < n1; ++i) {
		L[i] = arr[left + i];
	}
	for (int i = 0; i < n2; ++i) {
		R[i] = arr[mid + 1 + i];
	}
	L[n1] = INT_MAX;
	R[n2] = INT_MAX;

	int i = 0, j = 0;
	for (int k = left; k <= right; ++k) {
		if (L[i] <= R[j]) {
			arr[k] = L[i];
			++i;
		}
		else {
			arr[k] = R[j];
			++j;
		}
	}
}

void MergeSort(int* arr, int left, int right) {
	if (left < right) {
		// 分
		int mid = left + (right - left) / 2;
		MergeSort(arr, left, mid);
		MergeSort(arr, mid + 1, right);
		// 合
		Merge(arr, left, mid, right);
	}
}

int main()
{
    int arr[] = {30, 20, 10, 0, 80, 70, 6, 90};
    int left = 0, right = 7;
    Print(arr, right + 1);
    MergeSort(arr, left, right);
    Print(arr, right + 1);
    return 0;
}

Copyright © 2024 foghorn
Powered by .NET 9.0 on Kubernetes