数据结构复习:交换排序原理及C++实现
2015-01-13 17:22
393 查看
1. 交换排序的基本思想
两两比较key值,如果发生逆序(排列的顺序与期望的顺序相反)就交换,知道所有对象都排序完毕!常见的3种交换排序算法:冒泡排序,shaker排序和快速排序。2. 冒泡排序
设待排序列中有 n 个对象, 首先比较对象v[n-1]和v[n-2], 如果v[n-1] < v[n-2],则交换v[n-1]和v[n-2],然后对v[n-i]和v[n-i-1]进行同样操作,知道对v[1]和v[0]进行完操作,每一次冒泡,使得值小的对象前移动,如此重复,直至有序,图解如下:![](http://images.cnitblog.com/blog/659319/201501/131700459177633.jpg)
3.Shaker排序(双向冒泡排序)
此种排序是对冒泡排序的改进,冒泡排序每次只能从一个方向进行遍历,而shaker排序每次遍历包括两个方向,先从前向后,再从后往前双向交替,效率上较冒泡排序有所改进,图解如下:![](http://images.cnitblog.com/blog/659319/201501/131706070262865.jpg)
4.快速排序
快速排序采用的是一种分治的思想:取待排序对象序列中的某个对象为基准(任意取一个),按照其他对象与该基准对象的大小关系,将整个序列划分为左右两个子序列,其中左侧子序列中所有对象值都小于或者等于基准值,右侧对象序列中的所有对象值都大于基准值,然后对左右这两个子序列重复上述操作(递归),直至子序列为空为止!还是比较容易理解,这里就不画图了。5.C++实现
#ifndef EXCHANGESORT_H #define EXCHANGESORT_H #include <vector> #include <iostream> using std::vector; using std::cout; using std::endl; template <typename T> class ExchangeSort { private: int len; vector<T> list; public: /* * Construction function */ ExchangeSort(vector<T> _list, int _len) { for (int i = 0; i < _len; ++i) list.push_back(_list[i]); this->len = _len; } /* * bubbleSort functions */ void bubbleSort() { for (int i = 0; i < len; ++i) for (int j = i + 1; j < len; ++j) if (list[i] > list[j]) swap(i, j); } /* * shakerSort functions */ void shakerSort() { int i, left = 0; int shift = 0; int right = len - 1; while (left < right) { for (i = left; i < right; ++i) // From left to right { if (list[i] > list[i + 1]) { swap(i, i + 1); shift = i;// Record the last index } }// end for right = shift; for (i = right; i > left; --i)//From right to left { if (list[i] < list[i - 1]) { swap(i, i - 1); shift = i; } }// end for left = shift; }//end while } /* * quick sort */ void quickSort(int left, int right) { int i = left; int j = right; int pivot = list[left]; while (i < j) { while (i < j && list[j] >= pivot) --j; // Search the number less than pivot if (i < j) swap(i, j); while (i < j && list[i] <= pivot) ++i; // Search the number larger than pivot if (i < j) swap(i, j); } if (i != left) quickSort(left, i - 1); if (j != right) quickSort(j + 1, right); } /* * Exchange two elements of list */ void swap(int i, int j) { T temp = list[i]; list[i] = list[j]; list[j] = temp; } /* * Display the sorted result */ void out() { for (int i = 0; i < len; ++i) { cout << list[i] << " "; if ((i + 1) % 18 == 0) cout << endl; } cout << endl; } }; #endif //exchangeSortTest #include "ExchangeSort.h" #include <vector> using namespace std; const unsigned numEle = 8; int data[numEle] = {1,5,7,3,8,2,6,4}; int main() { vector<int> testData; for (unsigned i = 0; i < numEle; ++i) testData.push_back(data[i]); ExchangeSort<int> testBubble(testData, numEle); cout << "Before sorting: "; testBubble.out(); testBubble.bubbleSort(); cout << "After sorting with bubbleSort: "; testBubble.out(); ExchangeSort<int> testShaker(testData, numEle); cout << "Before sorting: "; testShaker.out(); testShaker.shakerSort(); cout << "After sorting with shakerSort: "; testShaker.out(); ExchangeSort<int> testQuick(testData, numEle); cout << "Before sorting: "; testQuick.out(); testQuick.quickSort(0, numEle-1); cout << "After sorting with testQuick: "; testQuick.out(); return 0; }
6.参考文献
左飞:C++数据结构原理与经典问题求解相关文章推荐
- 数据结构图文解析之:直接插入排序及其优化(二分插入排序)解析及C++实现
- 数据结构复习:希尔排序的C++实现
- [150422][C++]数据结构复习——队列实现源码
- 数据结构,图的邻接矩阵创建,邻接矩阵与邻接表的交换,两种表的输出,过程用C++实现
- [150421][C++]数据结构复习——栈实现源码
- [150420][C++]数据结构复习——链表实现源码
- 数据结构之C/C++实现2个交换类排序
- 数据结构与算法:C++实现选择排序
- 数据结构:二项队列原理及其C++实现
- 直接选择排序及交换二个数据的实现
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- 数据结构单项链表C++实现改变C版本
- 交换两个数据的值而不采用新的存储单位(C++实现)
- c++ 中sort的用法 实例说明 sort是用来排序 实现不同类型的数据排序
- 数据结构之双向循环链表(C++实现)
- 【转载】白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- [C++]数据结构:排序算法Part1----冒泡排序、选择排序、插入排序、堆排序
- 程序员面试宝典之数据结构基础----C++两个栈实现一个队列功能
- C++实现数据结构作业——表达式求值
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现