1.冒泡
#include <iostream.h>
void BubbleSort(int* pData,int Count)
{
int iTemp;
for(int i=1;i<Count;i++)
{
for(int j=Count-1;j>=i;j--)
{
if(pData[j]<pData[j-1])
{
iTemp = pData[j-1];
pData[j-1] = pData[j];
pData[j] = iTemp;
}
}
}
}
2.交换
void ExchangeSort(int* pData,int Count)
{
int iTemp;
for(int i=0;i<Count-1;i++)
{
for(int j=i+1;j<Count;j++)
{
if(pData[j]<pData[i])
{
iTemp = pData[i];
pData[i] = pData[j];
pData[j] = iTemp;
}
}
}
}
3.选择
void SelectSort(int* pData,int Count)
{
int iTemp;
int iPos;
for(int i=0;i<Count-1;i++)
{
iTemp = pData[i];
iPos = i;
for(int j=i+1;j<Count;j++)
{
if(pData[j]<iTemp)
{
iTemp = pData[j];
iPos = j;
}
}
pData[iPos] = pData[i];
pData[i] = iTemp;
}
}
4.插入
void InsertSort(int* pData,int Count)
{
int iTemp;
int iPos;
for(int i=1;i<Count;i++)
{
iTemp = pData[i];
iPos = i-1;
while((iPos>=0) && (iTemp<pData[iPos]))
{
pData[iPos+1] = pData[iPos];
iPos--;
}
pData[iPos+1] = iTemp;
}
}
.快速排序:
#include <iostream.h>
void run(int* pData,int left,int right)
{
int i,j;
int middle,iTemp;
i = left;
j = right;
middle = pData[(left+right)/2]; //求中间值
do{
while((pData[i]<middle) && (i<right))//从左扫描大于中值的数
i++;
while((pData[j]>middle) && (j>left))//从右扫描大于中值的数
j--;
if(i<=j)//找到了一对值
{
//交换
iTemp = pData[i];
pData[i] = pData[j];
pData[j] = iTemp;
i++;
j--;
}
}while(i<=j);//如果两边扫描的下标交错,就停止(完成一次)
//当左边部分有值(left<j),递归左半边
if(left<j)
run(pData,left,j);
//当右边部分有值(right>i),递归右半边
if(right>i)
run(pData,i,right);
}
void QuickSort(int* pData,int Count)
{
run(pData,0,Count-1);
}
void main()
{
int data[] = {10,9,8,7,6,5,4};
QuickSort(data,7);
for (int i=0;i<7;i++)
cout<<data[i]<<" ";
cout<<"\n";
}
、其他排序
1.双向冒泡:
通常的冒泡是单向的,而这里是双向的,也就是说还要进行反向的工作。
代码看起来复杂,仔细理一下就明白了,是一个来回震荡的方式。
写这段代码的作者认为这样可以在冒泡的基础上减少一些交换(我不这么认为,也许我错了)。
反正我认为这是一段有趣的代码,值得一看。
#include <iostream.h>
void Bubble2Sort(int* pData,int Count)
{
int iTemp;
int left = 1;
int right =Count -1;
int t;
do
{
//正向的部分
for(int i=right;i>=left;i--)
{
if(pData[i]<pData[i-1])
{
iTemp = pData[i];
pData[i] = pData[i-1];
pData[i-1] = iTemp;
t = i;
}
}
left = t+1;
//反向的部分
for(i=left;i<right+1;i++)
{
if(pData[i]<pData[i-1])
{
iTemp = pData[i];
pData[i] = pData[i-1];
pData[i-1] = iTemp;
t = i;
}
}
right = t-1;
}while(left<=right);
}
class QuickSort
{
public:
void quick_sort(int* x,int low,int high)
{
int pivotkey;
if(low <high)
{
pivotkey=partion(x,low,high);
quick_sort(x,low,pivotkey-1);
quick_sort(x,pivotkey+1,high);
}
}
int partion(int* x,int low,int high)
{
int pivotkey;
pivotkey=x[low];
while(low <high)
{
while (low <high&&x[high]>=pivotkey)
--high; //还有while循环只执行这一句
x[low]=x[high];
while (low <high&&x[low] <=pivotkey)
++low; //还有while循环只执行这一句
x[high]=x[low];
}
x[low]=pivotkey;
return low;
}
};
SHELL排序
这个排序非常复杂,看了程序就知道了。
首先需要一个递减的步长,这里我们使用的是9、5、3、1(最后的步长必须是1)。
工作原理是首先对相隔9-1个元素的所有内容排序,然后再使用同样的方法对相隔5-1个元素的排序以次类推。
#include <iostream.h>
void ShellSort(int* pData,int Count)
{
int step[4];
step[0] = 9;
step[1] = 5;
step[2] = 3;
step[3] = 1;
int iTemp;
int k,s,w;
for(int i=0;i<4;i++)
{
k = step[i];
s = -k;
for(int j=k;j<Count;j++)
{
iTemp = pData[j];
w = j-k;//求上step个元素的下标
if(s ==0)
{
s = -k;
s++;
pData[s] = iTemp;
}
while((iTemp<pData[w]) && (w>=0) && (w<=Count))
{
pData[w+k] = pData[w];
w = w-k;
}
pData[w+k] = iTemp;
}
}
}
冒泡排序性能优化版
C/C++ code
#include <iostream>
using namespace std;
void maopao(int *list,int n)
{
int i=n,j,temp;
bool exchange;//当数据已经排好时,退出循环
for(i=0;i<n;i++)
{
exchange=false;
for (j=0;j<n-i-1;j++)
{
if (list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
exchange=true;
}
}
if (!exchange)
{
return;
}
}
}
//帕斯卡恒等式为C(n,k)=C(n-1,k-1)+C(n-1,k)
long fun(long n,long r)
{
if(r<0||n<0)
{
printf("\nError.Exit!");
exit(1);
}
if(r>n) return 0;
if(r==1||r==n-1)//根据组合定义,我们有C(n,1)=C(n,n-1)=n
return n;
if(r==0||r==n)//根据组合定义,我们有C(n,0)=C(n,n)=1
return 1;
return fun(n-1,r)+fun(n-1,r-1);//帕斯卡组合公式
}
//选择法对数组排序的函数模板
template <class T>
void selectsort(T arr[],int size)
{
T temp;
int i,j;
for (i=0;i<size-1;i++)
for (j=i+1;j<size;j++)
if (arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
//冒泡法对数组排序的函数模板
template<class T>
void bubblesort(T *d,int n)
{
int i,j;
T t;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(d[j]>d[j+1])
{
t=d[j];
d[j]=d[j+1];
d[j+1]=t;
}
}
//插入法对数组排序的函数模板
template <class T>
void InsertSort(T A[], int n)
{
int i, j;
T temp;
for (i = 1; i < n; i++)
{
temp = A[i];
for (j=i-1; j>=0&&temp<A[j];j--)
A[j+1]=A[j];
A[j+1] = temp;
}
}
//二分查找法的函数模板
template <class T>
int binary_search(T array[], T value, int size)
{
int high = size-1, low = 0, mid;
while (low<=high)
{
mid = (high + low) / 2;
if (value < array[mid])
high = mid - 1;
else if(value>array[mid])
low = mid + 1;
else return mid;
}
return -1;
}
将2~36进制数与10进制数相互转换
//将2~36进制数转换成10进制数
unsigned int OthToDec(char *oth, int base) //base为已知数的进制
{
unsigned int i=0, dec=0;
while (oth[i])
{
dec*=base;
if (oth[i]>='0' && oth[i]<='9')
dec+=oth[i]&15;//dec+=oth[i]-48;
else if (oth[i]>='A' && oth[i]<='Z')
dec+=oth[i]-55;
else if (oth[i]>='a' && oth[i]<='z')
dec+=oth[i]-87;
i++;
}
return dec;
}
//将10进制(无符号)转2~36进制数
char *DecToOth(char *oth, unsigned int dec, int base) //base为要转换的数的进制
{
char ch, *left=oth, *right=oth,num[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
do
{
*right=num[dec%base];
right++;
}while (dec/=base);
*right='\0';
right--;
while (left<right)
{
ch=*left;
*left=*right;
*right=ch;
left++;right--;
}
return oth;
}
//统计substr在str中的个数
int fun(char *str,char *substr)
{
int n=0;
char *q;
q=substr;
while(*str!='\0')
{
if(*str==*substr)
{
str++;
substr++;
if(*substr=='\0')
{
n++;
substr=q;
}
}
else
{
str++;substr=q;
}
}
return n;
}
使用数组实现约瑟夫环:
#include <stdio.h>
#include <stdlib.h>
main()
{
int m,n,i=1,j,k=1,per,o;
printf("请输入总共的人数,记为n \n");
scanf("%d",&n);
int array[n+1];
int order[n+1];
printf("请输入几号出圈,记为m \n");
scanf("%d",&m);
printf("\n**************************************\n");
for(;i <n;i++)//数组链表模拟
array[i]=i+1;
array[n]=1;
printf("%d ",array[n]);
i=1;j=1;per=n;
while(array[i]!=i){
if(k==m){
order[j]=i;
j++;
array[per]=array[i];
k=0;
for(o=1;o <=j;o++)
printf("%d* ",order[o]);
}
else{printf("%d ",array[i]);
per=i;
i=array[i];
k++;
}
}
order[n]=i;
printf("\n**************************************\n");
for(i=1;i <=n;i++)
printf("%d ",order[i]);
system("pause");
return 0;
}
#include <iostream.h>
void BubbleSort(int* pData,int Count)
{
int iTemp;
for(int i=1;i<Count;i++)
{
for(int j=Count-1;j>=i;j--)
{
if(pData[j]<pData[j-1])
{
iTemp = pData[j-1];
pData[j-1] = pData[j];
pData[j] = iTemp;
}
}
}
}
2.交换
void ExchangeSort(int* pData,int Count)
{
int iTemp;
for(int i=0;i<Count-1;i++)
{
for(int j=i+1;j<Count;j++)
{
if(pData[j]<pData[i])
{
iTemp = pData[i];
pData[i] = pData[j];
pData[j] = iTemp;
}
}
}
}
3.选择
void SelectSort(int* pData,int Count)
{
int iTemp;
int iPos;
for(int i=0;i<Count-1;i++)
{
iTemp = pData[i];
iPos = i;
for(int j=i+1;j<Count;j++)
{
if(pData[j]<iTemp)
{
iTemp = pData[j];
iPos = j;
}
}
pData[iPos] = pData[i];
pData[i] = iTemp;
}
}
4.插入
void InsertSort(int* pData,int Count)
{
int iTemp;
int iPos;
for(int i=1;i<Count;i++)
{
iTemp = pData[i];
iPos = i-1;
while((iPos>=0) && (iTemp<pData[iPos]))
{
pData[iPos+1] = pData[iPos];
iPos--;
}
pData[iPos+1] = iTemp;
}
}
.快速排序:
#include <iostream.h>
void run(int* pData,int left,int right)
{
int i,j;
int middle,iTemp;
i = left;
j = right;
middle = pData[(left+right)/2]; //求中间值
do{
while((pData[i]<middle) && (i<right))//从左扫描大于中值的数
i++;
while((pData[j]>middle) && (j>left))//从右扫描大于中值的数
j--;
if(i<=j)//找到了一对值
{
//交换
iTemp = pData[i];
pData[i] = pData[j];
pData[j] = iTemp;
i++;
j--;
}
}while(i<=j);//如果两边扫描的下标交错,就停止(完成一次)
//当左边部分有值(left<j),递归左半边
if(left<j)
run(pData,left,j);
//当右边部分有值(right>i),递归右半边
if(right>i)
run(pData,i,right);
}
void QuickSort(int* pData,int Count)
{
run(pData,0,Count-1);
}
void main()
{
int data[] = {10,9,8,7,6,5,4};
QuickSort(data,7);
for (int i=0;i<7;i++)
cout<<data[i]<<" ";
cout<<"\n";
}
、其他排序
1.双向冒泡:
通常的冒泡是单向的,而这里是双向的,也就是说还要进行反向的工作。
代码看起来复杂,仔细理一下就明白了,是一个来回震荡的方式。
写这段代码的作者认为这样可以在冒泡的基础上减少一些交换(我不这么认为,也许我错了)。
反正我认为这是一段有趣的代码,值得一看。
#include <iostream.h>
void Bubble2Sort(int* pData,int Count)
{
int iTemp;
int left = 1;
int right =Count -1;
int t;
do
{
//正向的部分
for(int i=right;i>=left;i--)
{
if(pData[i]<pData[i-1])
{
iTemp = pData[i];
pData[i] = pData[i-1];
pData[i-1] = iTemp;
t = i;
}
}
left = t+1;
//反向的部分
for(i=left;i<right+1;i++)
{
if(pData[i]<pData[i-1])
{
iTemp = pData[i];
pData[i] = pData[i-1];
pData[i-1] = iTemp;
t = i;
}
}
right = t-1;
}while(left<=right);
}
class QuickSort
{
public:
void quick_sort(int* x,int low,int high)
{
int pivotkey;
if(low <high)
{
pivotkey=partion(x,low,high);
quick_sort(x,low,pivotkey-1);
quick_sort(x,pivotkey+1,high);
}
}
int partion(int* x,int low,int high)
{
int pivotkey;
pivotkey=x[low];
while(low <high)
{
while (low <high&&x[high]>=pivotkey)
--high; //还有while循环只执行这一句
x[low]=x[high];
while (low <high&&x[low] <=pivotkey)
++low; //还有while循环只执行这一句
x[high]=x[low];
}
x[low]=pivotkey;
return low;
}
};
SHELL排序
这个排序非常复杂,看了程序就知道了。
首先需要一个递减的步长,这里我们使用的是9、5、3、1(最后的步长必须是1)。
工作原理是首先对相隔9-1个元素的所有内容排序,然后再使用同样的方法对相隔5-1个元素的排序以次类推。
#include <iostream.h>
void ShellSort(int* pData,int Count)
{
int step[4];
step[0] = 9;
step[1] = 5;
step[2] = 3;
step[3] = 1;
int iTemp;
int k,s,w;
for(int i=0;i<4;i++)
{
k = step[i];
s = -k;
for(int j=k;j<Count;j++)
{
iTemp = pData[j];
w = j-k;//求上step个元素的下标
if(s ==0)
{
s = -k;
s++;
pData[s] = iTemp;
}
while((iTemp<pData[w]) && (w>=0) && (w<=Count))
{
pData[w+k] = pData[w];
w = w-k;
}
pData[w+k] = iTemp;
}
}
}
冒泡排序性能优化版
C/C++ code
#include <iostream>
using namespace std;
void maopao(int *list,int n)
{
int i=n,j,temp;
bool exchange;//当数据已经排好时,退出循环
for(i=0;i<n;i++)
{
exchange=false;
for (j=0;j<n-i-1;j++)
{
if (list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
exchange=true;
}
}
if (!exchange)
{
return;
}
}
}
//帕斯卡恒等式为C(n,k)=C(n-1,k-1)+C(n-1,k)
long fun(long n,long r)
{
if(r<0||n<0)
{
printf("\nError.Exit!");
exit(1);
}
if(r>n) return 0;
if(r==1||r==n-1)//根据组合定义,我们有C(n,1)=C(n,n-1)=n
return n;
if(r==0||r==n)//根据组合定义,我们有C(n,0)=C(n,n)=1
return 1;
return fun(n-1,r)+fun(n-1,r-1);//帕斯卡组合公式
}
//选择法对数组排序的函数模板
template <class T>
void selectsort(T arr[],int size)
{
T temp;
int i,j;
for (i=0;i<size-1;i++)
for (j=i+1;j<size;j++)
if (arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
//冒泡法对数组排序的函数模板
template<class T>
void bubblesort(T *d,int n)
{
int i,j;
T t;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(d[j]>d[j+1])
{
t=d[j];
d[j]=d[j+1];
d[j+1]=t;
}
}
//插入法对数组排序的函数模板
template <class T>
void InsertSort(T A[], int n)
{
int i, j;
T temp;
for (i = 1; i < n; i++)
{
temp = A[i];
for (j=i-1; j>=0&&temp<A[j];j--)
A[j+1]=A[j];
A[j+1] = temp;
}
}
//二分查找法的函数模板
template <class T>
int binary_search(T array[], T value, int size)
{
int high = size-1, low = 0, mid;
while (low<=high)
{
mid = (high + low) / 2;
if (value < array[mid])
high = mid - 1;
else if(value>array[mid])
low = mid + 1;
else return mid;
}
return -1;
}
将2~36进制数与10进制数相互转换
//将2~36进制数转换成10进制数
unsigned int OthToDec(char *oth, int base) //base为已知数的进制
{
unsigned int i=0, dec=0;
while (oth[i])
{
dec*=base;
if (oth[i]>='0' && oth[i]<='9')
dec+=oth[i]&15;//dec+=oth[i]-48;
else if (oth[i]>='A' && oth[i]<='Z')
dec+=oth[i]-55;
else if (oth[i]>='a' && oth[i]<='z')
dec+=oth[i]-87;
i++;
}
return dec;
}
//将10进制(无符号)转2~36进制数
char *DecToOth(char *oth, unsigned int dec, int base) //base为要转换的数的进制
{
char ch, *left=oth, *right=oth,num[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
do
{
*right=num[dec%base];
right++;
}while (dec/=base);
*right='\0';
right--;
while (left<right)
{
ch=*left;
*left=*right;
*right=ch;
left++;right--;
}
return oth;
}
//统计substr在str中的个数
int fun(char *str,char *substr)
{
int n=0;
char *q;
q=substr;
while(*str!='\0')
{
if(*str==*substr)
{
str++;
substr++;
if(*substr=='\0')
{
n++;
substr=q;
}
}
else
{
str++;substr=q;
}
}
return n;
}
使用数组实现约瑟夫环:
#include <stdio.h>
#include <stdlib.h>
main()
{
int m,n,i=1,j,k=1,per,o;
printf("请输入总共的人数,记为n \n");
scanf("%d",&n);
int array[n+1];
int order[n+1];
printf("请输入几号出圈,记为m \n");
scanf("%d",&m);
printf("\n**************************************\n");
for(;i <n;i++)//数组链表模拟
array[i]=i+1;
array[n]=1;
printf("%d ",array[n]);
i=1;j=1;per=n;
while(array[i]!=i){
if(k==m){
order[j]=i;
j++;
array[per]=array[i];
k=0;
for(o=1;o <=j;o++)
printf("%d* ",order[o]);
}
else{printf("%d ",array[i]);
per=i;
i=array[i];
k++;
}
}
order[n]=i;
printf("\n**************************************\n");
for(i=1;i <=n;i++)
printf("%d ",order[i]);
system("pause");
return 0;
}