//main.c
#include <iostream>
using namespace std;
extern void InsertSort(int* a, int n);
extern void SelectSort(int* a, int n);
extern void BubbleSort(int* a, int n);
extern void print(int* a,int n, int i);
extern void swap(int* a, int *b);
/*==================================================
//功能: 排序过程打印
//参数: int* a 数组起始地址;
// int n;数组总长度;
// int i 排序的趟数
//返回值: 最小数的下标
/*==================================================*/
void print(int* a,int n, int i)
{
cout<<"第"<<i+1<<"趟排序";
for(int i=0; i<n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void swap(int* a, int *b)
{
*a = *a+*b;
*b = *a-*b;
*a = *a-*b;
}
int main(void)
{
int a[] = {1,2,9,3,0,8,4,7,3,6,12,342,323,42,1,21,34,9};
print(a,sizeof(a)/sizeof(int),-1);
BubbleSort(a,sizeof(a)/sizeof(int));
print(a,sizeof(a)/sizeof(int),-1);
system("pause");
return 0;
}
//bubble.c
#include <iostream>
using namespace std;
extern void BubbleSort(int* a, int n);
extern void print(int* a,int n, int i);
extern void swap(int* a, int *b);
/*==================================================
//功能: 冒泡排序
// 依次选取最大数到结尾位置来排序
//参数: int* a 数组起始地址;
// int n;数组总长度;
//返回值:
/*==================================================*/
void BubbleSort1(int* a, int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1; j++)
{
if(a[j] > a[j+1])
{
swap(&a[j], &a[j+1]);
}
}
print(a,n,i);
}
}
/*==================================================
//功能: 冒泡排序
// 依次选取最大数到结尾位置来排序
//参数: int* a 数组起始地址;
// int n;数组总长度;
//返回值:
/*==================================================*/
void BubbleSort2(int* a, int n)
{
int k = 1;
for(int i=0; i<n-1 && k==1; i++)
{
k=0;
for(int j=0; j<n-i-1; j++)
{
if(a[j] > a[j+1])
{
k=1;
swap(&a[j], &a[j+1]);
}
}
print(a,n,i);
}
}
/*==================================================
//功能: 冒泡排序
// 依次选取最大数到结尾位置来排序
//参数: int* a 数组起始地址;
// int n;数组总长度;
//返回值:
/*==================================================*/
void BubbleSort3(int* a, int n)
{
int j = n-1;
while(j>0)
{
int pos = 0;
for(int i=0; i<j-1; i++)
{
if(a[i] > a[i+1])
{
pos = i;
swap(&a[i], &a[i+1]);
}
}
j = pos;
print(a,n,j);
}
}
/*==================================================
//功能: 冒泡排序
// 依次选取最大数到结尾位置来排序
//参数: int* a 数组起始地址;
// int n;数组总长度;
//返回值:
/*==================================================*/
void BubbleSort(int* a, int n)
{
int high = n-1;
int low = 0;
while(high>low)
{
int iPosHigh = 0;
int iPosLow = 0;
for(int i=low; i<high; i++)
{
if(a[i] > a[i+1])
{
iPosHigh = i;
swap(&a[i], &a[i+1]);
}
}
high = iPosHigh;
for(int i=high;i>low; i--)
{
if(a[i] < a[i-1])
{
iPosLow = i;
swap(&a[i], &a[i-1]);
}
}
low = iPosLow;
print(a,n,high);
}
}
//select.c
<pre name="code" class="cpp">#include <iostream>
using namespace std;
extern void print(int* a,int n, int i);
extern void swap(int* a, int *b);
extern void SelectSort(int*a, int n);
/*=========================================
//第0趟排序1 2 9 3 0 8 4 7 3 6
//第1趟排序0 2 9 3 1 8 4 7 3 6
//第2趟排序0 1 9 3 2 8 4 7 3 6
//第3趟排序0 1 2 3 9 8 4 7 3 6
//第4趟排序0 1 2 3 9 8 4 7 3 6
//第5趟排序0 1 2 3 3 8 4 7 9 6
//第6趟排序0 1 2 3 3 4 8 7 9 6
//第7趟排序0 1 2 3 3 4 6 7 9 8
//第8趟排序0 1 2 3 3 4 6 7 9 8
//第9趟排序0 1 2 3 3 4 6 7 8 9
/*==========================================*/
/*==================================================
//功能:
//描述: 查找到最小数的下标
//参数: int* a 数组起始地址;
// int n;数组总长度;
// int i 要开始查找的起始下标
//返回值: 最小数的下标
/*==================================================*/
int SelectMinKey(int* a,int n,int i)
{
int j = i;
for(int k=i+1; k<n; k++)
{
if(a[k] < a[j])
{
j = k;
}
}
return j;
}
/*==================================================
//功能: 选择排序
//描述: 依次选取最小/大数放到查找的起始位置
//参数: int* a 数组起始地址;
// int n;数组总长度;
//返回值:
/*==================================================*/
void SelectSort(int* a, int n)
{
int t;
for(int i=0; i<n-1; i++)
{
t = SelectMinKey(a,n,i);
if(t != i)
{
swap(&a[i], &a[t]);
}
print(a,n,i);
}
}
//insert.c
<pre name="code" class="cpp">#include <iostream>
using namespace std;
extern void InsertSort(int* a, int n);
extern void print(int* a,int n, int i);
/*=============================================
//第1趟排序1 2 9 3 0 8 4 7 3 6
//第2趟排序1 2 9 3 0 8 4 7 3 6
//第3趟排序1 2 3 9 0 8 4 7 3 6
//第4趟排序0 1 2 3 9 8 4 7 3 6
//第5趟排序0 1 2 3 8 9 4 7 3 6
//第6趟排序0 1 2 3 4 8 9 7 3 6
//第7趟排序0 1 2 3 4 7 8 9 3 6
//第8趟排序0 1 2 3 3 4 7 8 9 6
//第9趟排序0 1 2 3 3 4 6 7 8 9
/*============================================*/
/*==================================================
//功能: 插入排序
//描述: 将较大数依次后移,较小数插入到较大数前面
//参数: int* a 数组起始地址;
// int n;数组总长度;
//返回值:
/*==================================================*/
void InsertSort(int* a,int n)
{
for(int i=1; i<n; i++)
{
if(a[i-1] > a[i])
{
int filter = a[i];
int j = i-1;
while(filter < a[j] && j>=0)
{
a[j+1] = a[j];
j--;
}
a[j+1] = filter;
}
print(a,n,i-1);
}
}