#include
#include
#include
#define RANDOM(i) (rand()%i) //随机的常量
#define N 10 //设置数组长度
//分区操作
int Partition(int array[], int left, int right)
{
int i,j;
int temp;
j = left-1;
for (i=left; i<=right; i++)
{
if (array[i] <= array[right]) //以最后一个数组的值为基准
{
j++;
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
return j;
}
//迭代运算
void QuikSort(int array[], int left, int right)
{
int pivot;
if (left < right)
{
pivot = Partition(array, left, right);
QuikSort(array, left, pivot-1);
QuikSort(array, pivot+1, right);
}
}
/*主函数*/
int main()
{
int i = 0;
int a[N];
srand((int)time(0)); //设置随机数种子
for (i=0; i{
a[i] = RANDOM(100); //100以内的随机数
printf("%d\t", a[i]); //输出随机得到的数组
}
printf("\n\n");
QuikSort(a, 0, N-1);
for (i=0; i{
printf("%d\t", a[i]);
}
printf("\n");
}
#include
#define SIZE 8
void bubble_sort(int a[], int n);
void bubble_sort(int a[], int n)
{
int i, j, temp;
for (j = 0; j < n - 1; j++)
for (i = 0; i < n - 1 - j; i++)
{
if(a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
int main()
{
int number[SIZE] = {95, 45, 15, 78, 84, 51, 24, 12};
int i;
bubble_sort(number, SIZE);
for (i = 0; i < SIZE; i++)
{
printf("%d", number[i]);
}
printf("\n");
}
T629800处好友
T668100加我V吧