数据结构复习:交换排序原理及C++实现

时间:2024-04-18 23:06:31

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]进行完操作,每一次冒泡,使得值小的对象前移动,如此重复,直至有序,图解如下:

数据结构复习:交换排序原理及C++实现

3.Shaker排序(双向冒泡排序)

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

数据结构复习:交换排序原理及C++实现

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++数据结构原理与经典问题求解