版权声明:访问者可将本博客提供的内容或服务用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯本网站及相关权利人的合法权利。除此以外,将本网站任何内容或服务用于其他用途时,须征得本博客及相关权利人的书面许可,并支付报酬。 https://blog.csdn.net/qq894040717/article/details/81909384
在链表中可以让对应的i,j成为链表指针,其中arr【j+1】可以替换成arr->next->val【数据】,则可以变换成链表的算法;
#include <iostream>
#include <time.h>
//2018年8月21日 15:17:53
/*冒泡排序-1*/
void MaopaoSort(int *arr, int n)
{
int i, j, temp; //定义两个指针,用于标记,再设置一个中间变量,用于交换;
for (i =n-1; i>-1; i--)//将i指向尾部,逐个向前移动指针i;
{
for ( j = 0; j <i; j++)//快速移动指针j,两两交换将最大值移动到最后一位,下一次i指针向<<右移动时候,将最大值排除在外。
{
if (arr[j + 1]<arr[j])//交换算法
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
/*冒泡排序-2*/
void MaopaoSort(int *arr, int n)
{
int i, j, temp; //定义i计数器,定义j指针,用于标记,再设置一个中间变量,用于交换;
for (i =0; i<n; i++)//计数器i用于遍历数组;
{
for (j = 0; j <n-1-i; j++)//快速移动指针j,两两交换,将最大值移动到最后一位,通过计数器i的移动将最后一位排除在外,再次遍历。
{
if (arr[j + 1]<arr[j])//交换算法
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
/*测试主函数*/
int main()
{
int a[] = { 4, 1, -2, 56, 22, 4, 7, 52, 4, 8, 6, 2, -5 };
time_t t1, t2;
t1 = clock();
int num = sizeof(a) / sizeof(int);
/*InsertSort(a, num);*/
MaopaoSort(a, num);
for (int i = 0; i < num; i++)
{
printf("%d ", a[i]);
}
t2 = clock();
printf("\n程序总运行时间:%f", (t2 - t1) / CLOCKS_PER_SEC);
getchar();
return 0;
}