排序算法总结

#include 
#include 
#include 
#include 
using namespace std;

class MySort {
public:
	MySort(vector v):m_num(v) {}
	virtual ~MySort() = 0{}
	virtual void Sort() = 0;
	void display() {
		copy(m_num.begin(), m_num.end(), ostream_iterator(cout, " "));
		cout << endl;
	}
protected:
	vector m_num;
};

// 冒泡排序
class BubbleSort:public MySort {
public:
	BubbleSort(vector v):MySort(v) {};
	void Sort() {
		int len = m_num.size();
		if (len < 0)
			return ;
		for (int i = 0; i < len; ++ i) {
			for (int j = 1; j < len - i; ++ j) {
				if (m_num[j] < m_num[j-1])
					swap(m_num[j], m_num[j-1]);
			}
		}
	}
};

// 选择排序
class SelectSort: public MySort {
public:
	SelectSort(vector v):MySort(v) {}
	void Sort() {
		int len = m_num.size();
		if (len < 1) return;
		for (int i = 0; i < len; ++ i) {
			int iminPos = i;
			for (int j = i+1; j < len; ++ j) {
				if (m_num[j] < m_num[iminPos])
					iminPos = j;
			}
			swap(m_num[i], m_num[iminPos]);
		}
	}
};

// 插入排序
class InsertSort: public MySort {
public:
	InsertSort(vector v):MySort(v) {}
	void Sort() {
		int len = m_num.size();
		if (len < 1) return ;
		for (int i = 1; i < len; ++ i) {
			int tmp = m_num[i];
			for (int j = i-1; j >= 0; -- j) {
				if (m_num[j] > tmp)
					m_num[j+1] = m_num[j];
				else
					break;
			}
			m_num[j+1] = tmp;
		}
	}
};

// 归并排序
class MergeSort: public MySort {
public:
	MergeSort(vector v):MySort(v) {}

	void Merge(int llo, int mid, int rhi) {
		vector container(rhi-llo+1, 0);
		int lhi = mid, rlo = mid + 1, tmp = llo;
		int i = 0;
		while (llo <= lhi && rlo <= rhi) {
			if (m_num[llo] <= m_num[rlo])
				container[i ++] = m_num[llo ++];
			else
				container[i ++] = m_num[rlo ++];
		}
		if (llo <= lhi)
			copy(m_num.begin()+llo, m_num.begin()+lhi+1, container.begin()+i);
		else
			copy(m_num.begin()+rlo, m_num.begin()+rhi+1, container.begin()+i);
		copy(container.begin(), container.end(), m_num.begin()+tmp);
	}

	void helper(int lo, int hi) {
		if (lo >= hi)
			return ;
		int mid = lo + (hi - lo) / 2;
		helper(lo, mid);
		helper(mid+1, hi);
		Merge(lo, mid, hi);
	}

	void Sort() {
		int len = m_num.size();
		if (len < 1) return;
		helper(0, len-1);
	}
};

// 希尔排序
class ShellSort:public MySort {
public:
	ShellSort(vector v):MySort(v) {}
	void Sort() {
		int len = m_num.size();
		if (len < 1) return;
		int gap = len / 2;
		for(; gap > 0; gap /= 2) {
			for (int i = gap; i < len; ++ i) {
				int tmp = m_num[i];
				for (int j = i-gap; j >= 0; j -= gap) {
					if (m_num[j] > tmp)
						m_num[j+gap] = m_num[j];
					else
						break;
				}
				m_num[j+gap] = tmp;
			}
		}
	}
};

// 堆排序
class HeapSort:public MySort {
public:
	HeapSort(vector v):MySort(v) {}

	int getLeftChild(int i) {
		return 2 * i + 1;
	}

	void preDown(int pos, int len) {
		int left = getLeftChild(pos);
		int right = left + 1;
		int N = len / 2 - 1;
		while (pos <= N && m_num[pos] < max(m_num[left], (right < len ? m_num[right] : INT_MIN))) {
			if (m_num[left] > (right < len ? m_num[right] : INT_MIN)) {
				swap(m_num[left], m_num[pos]);
				pos = left;
			} else {
				swap(m_num[right], m_num[pos]);
				pos = right;
			}
			left = getLeftChild(pos);
			right = left + 1;
		}
	}

	void Sort() {
		int len = m_num.size();
		if (len < 1) return ;
		int N = len / 2 - 1, i;
		for (i = N; i >= 0; -- i) {
			preDown(i, len);
		}
		for (i = 0; i < len; ++ i) {
			swap(m_num[0], m_num[len - 1 - i]);
			preDown(0, len - 1 - i);
		}
	}
};

// 快速排序
class QuickSort:public MySort {
public:
	QuickSort(vector v):MySort(v) {}

	int partition(int lo, int hi) {
		int r = m_num[hi];
		int i = lo - 1, j = lo;
		while (j < hi) {
			if (m_num[j] < r) {
				swap(m_num[++i], m_num[j]);
			}
			j ++;
		}
		swap(m_num[++i], m_num[j]);
		return i;
	}

	void helper(int lo, int hi) {
		if (lo >= hi)
			return ;
		int q = partition(lo, hi);
		helper(lo, q - 1);
		helper(q + 1, hi);
	}

	void Sort() {
		int len = m_num.size();
		if (len < 1) return;
		helper(0, len - 1);
	}
};

int main() {
	int ia[] = {10, 9, 8, 7, -6, 5, 4, 3, -2, 1};
	vector v(ia, ia+10);
    // MySort *p = new BubbleSort(v);
    // MySort *p = new SelectSort(v);
    // MySort *p = new MergeSort(v);
    // MySort *p = new InsertSort(v);
    // MySort *p = new ShellSort(v);
    // MySort *p = new HeapSort(v);
	MySort *p = new QuickSort(v);
	p->Sort();
	p->display();
	return 0;
}

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