数据结构复习:交换排序原理及C++实现
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]进行完操作,每一次冒泡,使得值小的对象前移动,如此重复,直至有序,图解如下:

3.Shaker排序(双向冒泡排序)
此种排序是对冒泡排序的改进,冒泡排序每次只能从一个方向进行遍历,而shaker排序每次遍历包括两个方向,先从前向后,再从后往前双向交替,效率上较冒泡排序有所改进,图解如下:

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 = ; i < _len; ++i) list.push_back(_list[i]);
this->len = _len;
} /*
* bubbleSort functions
*/
void bubbleSort()
{
for (int i = ; i < len; ++i)
for (int j = i + ; j < len; ++j)
if (list[i] > list[j]) swap(i, j);
} /*
* shakerSort functions
*/
void shakerSort()
{
int i, left = ;
int shift = ;
int right = len - ;
while (left < right)
{
for (i = left; i < right; ++i) // From left to right
{
if (list[i] > list[i + ])
{
swap(i, i + );
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 - ])
{
swap(i, i - );
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 - );
if (j != right) quickSort(j + , 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 = ; i < len; ++i)
{
cout << list[i] << " ";
if ((i + ) % == ) cout << endl;
}
cout << endl;
}
}; #endif //exchangeSortTest
#include "ExchangeSort.h"
#include <vector>
using namespace std; const unsigned numEle = ;
int data[numEle] = {,,,,,,,}; int main()
{
vector<int> testData;
for (unsigned i = ; 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(, numEle-);
cout << "After sorting with testQuick: ";
testQuick.out(); return ;
}
6.参考文献
左飞:C++数据结构原理与经典问题求解
数据结构复习:交换排序原理及C++实现的更多相关文章
- MySQL索引背后的数据结构及算法原理【转】
本文来自:张洋的MySQL索引背后的数据结构及算法原理 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持 ...
- MySQL 索引背后的数据结构及算法原理
本文转载自http://blog.jobbole.com/24006/ 摘要本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引 ...
- MySQL索引背后的数据结构及算法原理 (转)
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- MySQL(二)索引背后的数据结构及算法原理
本文转载自CodingLabs,原文链接 MySQL索引背后的数据结构及算法原理 目录 摘要 一.数据结构及算法基础 1. 索引的本质 2. B-Tree和B+Tree 3. 为什么使用B-Tree( ...
- CodingLabs - MySQL索引背后的数据结构及算法原理
原文:CodingLabs - MySQL索引背后的数据结构及算法原理 首页 | 标签 | 关于我 | +订阅 | 微博 MySQL索引背后的数据结构及算法原理 作者 张洋 | 发布于 2011-10 ...
- MySQL索引之数据结构及算法原理
MySQL索引之数据结构及算法原理 MySQL支持多个存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BTree索引,哈希索引,全文索引等等.本文只关注BTre ...
- 【PHP数据结构】交换排序:冒泡、快排
上篇文章中我们好好地学习了一下插入类相关的两个排序,不过,和交换类的排序对比的话,它们真的只是弟弟.甚至可以说,在所有的排序算法中,最出名的两个排序都在今天要介绍的交换排序中了.不管是冒泡.还是快排, ...
- 【转】MySQL索引背后的数据结构及算法原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- [转]MySQL索引背后的数据结构及算法原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
随机推荐
- Cent OS5.2安装Hyper-V集成光盘
一.Hyper-V安装windows系统没有问题,windows2000以后系统都可以,一切顺利. 驱动程序:IDE.SCSI.网络.视频和鼠标 要想实现更强的功能,宿主机需要安装Hyper-V集成光 ...
- C#读写文件总结
1.使用FileStream读写文件 文件头: using System; using System.Collections.Generic; using System.Text; using ...
- enter mysql
1, mysql -u database username -p 2, database password 3, use (database name) -> change database 4 ...
- C# 关闭 Excel进程
namespace ExcelTest { class DataOutput { static void Main(string[] args) ...
- JS简单入门教程
JS简单教程 使用方法:放到任意html页面的head标签下 Test1方法弹出当前时间对话框 Test2方法for循环输出 Test3方法for(…in…)输出数组内容 <script typ ...
- Ubuntu上用快捷键关闭没有响应的程序
Linux 上有很多方法可以强制关闭无响应的程序,比如你可以通过按快捷键 Ctrl + Shift + T 来调出 Terminal 或者用 Ctrl + Shift + F1 进入 Console ...
- [转] Web前端优化之 Javascript篇
原文链接: http://lunax.info/archives/3099.html Web 前端优化最佳实践之 JavaScript 篇,这部分有 6 条规则,和 CSS 篇 重复的有几条.前端优化 ...
- InternetOpenA
[ilink32 Error] Error: Unresolved external 'InternetOpenA' referenced from ..\WIN32\DEBUG\NATIVEXML ...
- windows平台下安装python的setuptools工具
到下面的网址下载setuptools-0.6c11.win32-py2.7.exe http://pypi.python.org/pypi/setuptools#files 然后安装setuptool ...
- Type Encoding
[Type Encodings] The compiler encodes the return and argument types for each method in a character s ...