常用排序算法

源码如下:

class Solution {
private:
	/*void max_heapify(vector &a, int start, int end) {
		//https://baike.baidu.com/item/%E5%A0%86%E6%8E%92%E5%BA%8F/2840151?fr=aladdin
		//建立父节点指标和子节点指标
		int dad = start;
		int son = 2 * dad + 1;
		while(son <= end) { //若子节点指标在范围内才做比较
			if(son + 1 <= end && a[son] < a[son + 1]) { //先比较两个子节点大小,选择最大的
				son ++;
			}
			if(a[dad] > a[son]) { //如果父节点大於子节点代表调整完毕,直接跳出函数
				return;
			} else { //否则交换父子内容再继续子节点和孙节点比较
				swap(a[dad], a[son]);
				dad = son;
				son = 2 * dad + 1;
			}
		}
	} 
	void heap_sort(vector &a) {
		int len = a.size();
		//初始化,i从最後一个父节点开始调整
		for(int i = len / 2 - 1; i >= 0; -- i) {
			max_heapify(a, i, len - 1);
		}
		//先将第一个元素和已经排好的元素前一位做交换,再从新调整(刚调整的元素之前的元素),直到排序完毕
		for(int i = len - 1; i > 0; -- i) {
			swap(a[0], a[i]);
			max_heapify(a, 0, i - 1);
		}
	}*/
	/*void Quick_sort(vector &a, int low, int high) {
		if(low >= high) {
			return;
		}
		int temp = a[low];
		int i = low;
		int j = high;
		while(i < j) {
			while(i < j && a[j] >= temp) {
				j --;
			}
			if(i < j) {
				a[i] = a[j];
				i ++;
			}
			//a[i] = a[j];
			while(i < j && a[i] <= temp) {
				i ++;
			}
			if(i < j) {
				a[j] = a[i];
				j --;
			}
			//a[j] = a[i];
		}
		a[i] = temp;
		Quick_sort(a, low, i - 1);
		Quick_sort(a, i + 1, high);
	}*/
	void mergeSort(vector<int> &a, int left, int right) {
		int mid;
		if(left < right) {
			mid = left + (right - left) / 2;
			mergeSort(a, left, mid);
			mergeSort(a, mid + 1, right);
			merge(a, left, mid, right);
		}
	}
	
	void merge(vector<int> &a, int left, int mid, int right) {
		vector<int> L, R;
		int n1 = mid - left + 1;
		int n2 = right - mid;
		L.resize(n1 + 1); //申请空间
		R.resize(n2 + 1);
		
		for(int i = 0; i < n1; i ++) {
			L[i] = a[left + i];
		}
		for(int i = 0; i < n2; i ++) {
			R[i] = a[mid + 1 + i];
		}
		L[n1] = INT_MAX;
		R[n2] = INT_MAX;
		int i = 0;
		int j = 0;
		for(int k = left; k <= right; k ++) {
			if(L[i] <= R[j]) {
				a[k] = L[i];
				i ++;
			} else {
				a[k] = R[j];
				j ++;
			}
		}
	}
	 
public:
    vector<int> sort(vector<int> input, int k) {
        int n = input.size();
        if(k > n || n == 0) {
        	return res;
		}
        /*//冒泡排序 
        for(int i = 0; i < n - 1; i ++) {
            for(int j = 0; j < n - i - 1; j ++) {
                if(input[j] > input[j + 1]) {
                    int tmp = input[j];
                    input[j] = input[j + 1];
                    input[j + 1] = tmp;
                }
            }
        }*/ 
		
		/*// 直接插入排序
		for(int i = 1; i < n; i ++) {
			int temp = input[i];
			int j = i - 1;
			while(j > -1 && temp < input[j]) {
				input[j + 1] = input[j];
				-- j;
			}
			input[j + 1] = temp;
		}*/
		/*// 直接选择排序 
		for(int i = 0; i < n; i ++) {
			//int j = i;
			int key = i;
			for(int j = i; j < n; j ++) {
				if(input[j] < input[key]) {
					key = j;
				}
			}
			int temp = input[i];
			input[i] = input[key];
			input[key] = temp;
		} */
		//堆排序
		//heap_sort(input); 
		//快速排序
		//Quick_sort(input, 0, n - 1); 
		//归并排序 
		mergeSort(input, 0, n - 1);
		return input;
    }
};
int main() {
	Solution *p; //= new Solution;
	vector<int> n = {2,4,1,6,3,5};//{6,-3,-2,7,-15,1,2,2};
	auto r = p->sort(n, 3);
	for(auto t : r) {
		cout << t << endl;
	}
	return 0;
}

输出结果:1 2 3 4 5 6

你可能感兴趣的:(算法,排序算法,C++)