#include<iostream>
using namespace std;
int buf[10] = { 1, 9, 7, 5, 6, 4, 2, 3, 8, 11};
void swap(int &a, int &b)//交换ab两数
{
a = a + b;
b = a - b;
a = a - b;
}
void print(int *p, int length)//输出函数
{
for (int i = 0; i < length; i++)
{
cout << p[i] << " ";
}
}
void BubbleSort(int *p, int length)//冒泡排序
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10 -1- i; j++)
{
if (p[j] > p[j + 1])
{
swap(p[j], p[j + 1]);
}
}
}
}
void quicksort(int left ,int right)//快速排序
{
if (left > right)
return;
int temp = buf[left];
int templeft = left;
int tempright = right;
while (templeft != tempright)
{
while (buf[tempright] >= temp && templeft < tempright)
tempright--;
while (buf[templeft] <= temp && templeft < tempright)
templeft++;
if (templeft < tempright)
{
swap(buf[templeft], buf[tempright]);
}
}
buf[left] = buf[templeft];
buf[templeft] = temp;
quicksort(left, templeft - 1);
quicksort(templeft + 1,right);
}
void insertsort(int[])//插入排序
{
int templength = sizeof(buf)/sizeof(int);
for (int i = 1; i < templength; i++)
{
if (buf[i - 1] > buf[i])
{
int j = i;
int waitinsert = buf[i];
while (j > 0 && waitinsert < buf[j - 1])
{
buf[j] = buf[j - 1];
j--;
}
buf[j] = waitinsert;
}
}
cout << "1" << endl;
}
int main(int argc, char *args[])
{
int m = sizeof(buf);
int n = sizeof(int);
print(buf, sizeof(buf) / sizeof(int));
cout << endl;
//BubbleSort(buf, sizeof(buf) / sizeof(int));
//quicksort(0, sizeof(buf) / sizeof(int)-1);
insertsort(buf);
print(buf, sizeof(buf) / sizeof(int));
cin.get();
}
冒泡排序,快速排序,插入排序的c++实现
最新推荐文章于 2024-06-06 09:34:24 发布