• [算法] 6 种排序的重写,很熟练!


    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <vector>
    #include <stack>
    #include <deque>
    #include <queue>
    #include <bitset>
    #include <list>
    #include <map>
    #include <set>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    #include <utility>
    #include <sstream>
    #include <climits>
    #include <cassert>
    #define BUG puts("here!!!");
    
    using namespace std; // 8 5 4 9 2 3 6
    void heapify(int *a, int i, int size) {
    	int ls = 2*i, rs = 2*i + 1;
    	int large = i;
    	if(a[ls] > a[i]) large = ls;
    	if(a[rs] > a[large]) large = rs;
    	if(large != i) {
    		swap(a[large], a[i]);
    		heapify(a, large, size);
    	}
    }
    void buildHeap(int *a, int size) {
    	for(int i = size/2; i > 0; i++) {
    		heapify(a, i, size);
    	}
    }
    void heapSort(int *a, int size) {
    	buildHeap(a, size);
    	int len = size;
    	for(int i = len; i > 0; i--) {
    		swap(a[len], a[1]);
    		len--;
    		heapify(a, 1, len);
    	}
    }
    void quickSort(int *a, int left, int right) {
    	if(left < right) {
    		int l = left, r = right, x = a[l];
    		while(1) {
    			while(l < r && a[r] >= x) r--;
    			while(l < r && a[l] <= x) l++;
    			if(l >= r) break;
    			swap(a[l], a[r]);
    		}
    		swap(a[left], a[l]);
    		quickSort(a, left, l-1);
    		quickSort(a, l+1, right);
    	}
    }
    void mergeSort(int *a, int l, int r) {
    	if(l >= r) return;
    	int mid = (l+r) >> 1;
    	mergeSort(a, l, mid);
    	mergeSort(a, mid+1, r);
    	int *arr = new int[r-l+1];
    	int k = 0;
    	int i = l, j = mid+1;
    	while(i <= mid && j <= r) {
    		if(a[i] <= a[j]) arr[k++] = a[i];
    		else arr[k++] = a[j];
    	}
    	while(i <= mid) arr[k++] = a[i++];
    	while(j <= r) arr[k++] = a[j++];
    	for(int i = l; i <= r; i++) {
    		a[i] = arr[i-l];
    	}
    	delete []arr;
    }
    void insertSort(int *a, int len) {
    	int j;
    	for(int i = 1; i < len; i++) {
    		int temp = a[i];
    		for(j = i-1; j >= 0 && temp < a[j]; j--) {
    			a[j+1] = a[j];
    		}
    		a[j+1] = temp;
    	}
    }
    void bubbleSort(int *a, int len) {
    	for(int i = 1; i < len; i++) {
    		for(int j = 0; j < len-i; j++) {
    			if(a[j] > a[j+1]) swap(a[j], a[j+1]);
    		}
    	}
    }
    void selectSort(int *a, int len) {
    	for(int i = 0; i < len-1; i++) {
    		int k = i;
    		for(int j = i+1; j < len; i++) {
    			if(a[j] < a[k]) k = j;
    		}
    		swap(a[k], a[i]);
    	}
    }
    int main() {
    	return 0;
    }
    

  • 相关阅读:
    【转】Android之Inflate()方法用途+setContentView和inflate区别
    【转】Android 面试题笔记android五种布局及其作用
    温故知新系列(一)冒泡排序
    一道Java面试题目——随想+心得
    关于String 和 StringBuffer、StringBuilder
    Mysql变量
    Mysql函数
    Mysql流程控制
    Mysql事物
    Mysql视图
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787023.html
Copyright © 2020-2023  润新知