C++完整代码
#include<iostream>
#pragma comment(lib,"opencv_ml2413d.lib")
using namespace std;
int arr[] = { 4, 2, 5, 7, 3, 0, 9, 6, 2, 7, 4, 1 };
void BubbleSort(int a[], int n);
void InsertSort(int a[], int n);
void ChooseSort(int a[], int n);
void swap(int &a, int& b);
int main()
{
int num = (sizeof(arr)) / (sizeof(arr[0]));
BubbleSort(arr, num);
InsertSort(arr,num);
ChooseSort(arr,num);
return 0;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void BubbleSort(int a[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (a[j]>a[j + 1])
swap(a[j], a[j + 1]);
}
}
cout << "冒泡排序结果" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i];
}
cout << endl;
}
void InsertSort(int a[], int n)
{
int i, j, temp;
for (i = 1; i <= n; i++)
{
if (a[i] < a[i - 1])
{
temp = a[i];
for (j = i; temp < a[j - 1]; --j)
{
a[j] = a[j - 1];
}
a[j] = temp;
}
}
cout << "插入排序结果" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i];
}
cout << endl;
}
void ChooseSort(int a[], int n)
{
int i, j, index;
for (i = 0; i < n; i++)
{
index = i;
for (j = i+1; j <n; j++)
{
if (a[j] < a[j-1])
index = j;
}
swap(a[index], a[i]);
}
cout << "选着排序结果" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i];
}
cout << endl;
}