int a[10] = {1,9,7,4,8,5,3,6,0,2};
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10-i-1; j++)
{
if (a[j+1]<a[j])
{
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < 10; i++)
cout << a[i] << endl;
void sort(LinkList *L)
{
int i, j, t;
LinkList *p = L;
int count = 0;
while (p->link != NULL)
{
count++;
p = p->link;
}
for (i = 0; i<count - 1; i++) /* 冒泡法排序 */
{
p = L;
for (j = 0; j<count - i - 1; j++)
{
if (p->data > p->link->data)
{
t = p->data;
p->data = p->link->data;
p->link->data = t;
}
p = p->link;
}
}
}
本文介绍了使用冒泡排序算法对整数数组及链表进行排序的两种实现方式。首先通过数组实现冒泡排序,展示了如何比较相邻元素并交换位置以完成排序过程;其次,文章还探讨了针对链表数据结构的冒泡排序方法,并详细解释了计数节点数量及遍历链表进行元素比较的具体步骤。
4万+

被折叠的 条评论
为什么被折叠?



