1、冒泡
void bubbleSort(int a[], int n){for(int i =0 ; i< n-1; ++i) {for(int j = 0; j < n-i-1; ++j) {if(a[j] > a[j+1]){int tmp = a[j] ; a[j] = a[j+1] ; a[j+1] = tmp;}}}
}/*1.设置一标志性变量pos,用于记录每趟排序中最后一次进行交换的位置。由于pos位置之后的记录均已交换到位,故在进行下一趟排序时只要扫描到pos位置即可。
改进后算法如下:
*/
void Bubble_1 ( int r[], int n) {int i= n -1; //初始时,最后位置保持不变while ( i> 0) { int pos= 0; //每趟开始时,无记录交换for (int j= 0; j< i; j++)if (r[j]> r[j+1]) {pos= j; //记录交换的位置 int tmp = r[j]; r[j]=r[j+1];r[j+1]=tmp;} i= pos; //为下一趟排序作准备}
}/*2.传统冒泡排序中每一趟排序操作只能找到一个最大值或最小值,我们考虑利用在每趟排序中进行正向和反向两遍冒泡的方法一次可以得到两个最终值(最大者和最小者) , 从而使排序趟数几乎减少了一半。
改进后的算法实现为:
*/
void Bubble_2 ( int r[], int n){int low = 0; int high= n -1; //设置变量的初始值int tmp,j;while (low < high) {for (j= low; j< high; ++j) //正向冒泡,找到最大者if (r[j]> r[j+1]) {tmp = r[j]; r[j]=r[j+1];r[j+1]=tmp;} --high; //修改high值, 前移一位for ( j=high; j>low; --j) //反向冒泡,找到最小者if (r[j]<r[j-1]) {tmp = r[j]; r[j]=r[j-1];r[j-1]=tmp;}++low; //修改low值,后移一位}
}
2、快速排序
#include <iostream>
#include <stack>
using namespace std;int GetKeyIndex(int array[], int left, int right)
{int mid = left + ((right - left)>>1);if(array[left] < array[right]){if(array[mid] < array[left])return left;if(array[mid] > array[right])return right;elsereturn mid;}else{if(array[mid] < array[right])return right;else if(array[mid] > array[left])return left;elsereturn mid;}
}int partion(int array[], int left, int right)
{int begin = left;int end = right-1;//三个数取中间int keyIndex = GetKeyIndex(array, left, right);if(keyIndex != right){std::swap(array[keyIndex], array[right]);}int key = array[right];while( begin < end ){while( begin < end && array[begin] <= key)begin++;while( begin < end && array[end] > key)end--;if(begin < end){std::swap(array[begin], array[end]);begin++;}}if(begin != right && array[begin] > array [right]){swap(array[begin], array [right]);return begin;}return right;
}void QuickSort(int array[], int left, int right)
{if(left < right){int div = partion(array, left, right);QuickSort(array, left, div - 1);QuickSort(array, div + 1, right);}
}/*int partion2(int array[], int left, int right)
{int begin = left;int end = right;三个数取中间//int keyIndex = GetKeyIndex(array, left, right);//if(keyIndex != right)//{// std::swap(array[keyIndex], right);//}int key = array[right];while( begin < end ){while( begin < end && array[begin] <= key)begin++;std::swap(array[begin], array[end]);while( begin < end && array[end] > key)end--;std::swap(array[begin], array[end]);}return begin;
}void QuickSort(int array[], int left, int right)
{if(left < right){int div = partion2(array, left, right);QuickSort(array, left, div - 1);QuickSort(array, div + 1, right);}
}int partion3(int array[], int left, int right)
{int begin = left;int end = right;int key = array[left];while( begin < end ){while( begin < end && array[end] >= key)end--;if(begin < end){array[begin] = array[end];begin++;}while( begin < end && array[begin] <= key)begin++;if(begin < end){array[end] = array[begin];end--;}}array[begin] = key;return begin;
}void QuickSort(int array[], int left, int right)
{if(left < right){int div = partion3(array, left, right);QuickSort(array, left, div - 1);QuickSort(array, div + 1, right);}
}int partion4(int array[], int left, int right)
{int Cur = left;int Prev = Cur - 1;int key = array[right];while(Cur <= right){if(array[Cur] < key && ++Prev != Cur)std::swap(array[Prev], array[Cur]);++Cur;}std::swap(array[++Prev], array[right]);return Prev;
}void QuickSort(int array[], int left, int right)
{if(left < right){int div = partion4(array, left, right);QuickSort(array, left, div - 1);QuickSort(array, div + 1, right);}
}void QuickSort_nor(int *array, int size)
{stack<int> s;s.push(size-1);s.push(0);while(!s.empty()){int left = s.top();s.pop();int right = s.top();s.pop();if(left < right){int div = partion3(array, left, right);s.push(right);s.push(div +1);s.push(div -1);s.push(left);}}
}
*/