C语言实现的超详细的冒泡排序(附有详细代码)


1.冒泡排序的基本思想 冒泡排序是交换排序中一种简单的排序方法。
 
它的基本思想是对所有相邻记录的关键字值进行比效,如果是逆顺(a[j]>a[j+1]),则将其交换,最终达到有
序化;
 
其处理过程为: 
 
(1)将整个待排序的记录序列划分成有序区和无序区,初始状态有序区为空,无序区包括所有待排序的记
录。 
 
(2)对无序区从前向后依次将相邻记录的关键字进行比较,若逆序将其交换,从而使得关键字值小的记录向
上”飘浮”(左移),关键字值大的记录好像石块,向下“堕落”(右移)。 每经过一趟冒泡排序,都使无序区中
关键字值最大的记录进入有序区,对于由n个记录组成的记录序列,最多经过n-1趟冒泡排序,就可以将这n个记
录重新按关键字顺序排列。
 
 
 2.原始的冒泡排序算法 对由n个记录组成的记录序列,最多经过(n-1)趟冒泡排序,就可以使记录序列成为 
有序序列,第一趟定位第n个记录,此时有序区只有一个记录;第二趟定位第n-1个记录,此时有序区有两个记
录;以此类推,算法框架为: for(i=n;i>1;i—) { 定位第i个记录; }

代码如下:

#include<stdio.h>

int main()
{
   int data[5];
   printf("请输入数据:\n");
   int i,j;
   for(i = 0;i < 5;i ++)
   {
   	scanf("%d",&data[i]);	//输入数据 
   }
   
   for(i = 0;i < 5;i ++)  //采用双重循环 
   {
   	for(j = 0;j < 5 - 1 - i;j++)
   	{
   	  if(data[j] > data[j+1])     //这里默认采用升序来排列 
		 {
		 	int temp;        //定义一个临时值来存储数据,以此来达到交换数值的目的
			temp = data[j];
			data[j] = data[j+1];
			data[j+1] = temp;
		 }	
	}
   }
   
   printf("排序好的数组为:\n");
   for(i = 0;i < 5;i ++)    //用for循环来遍历并输出排序完成的数组 
   {
   	 printf("%d ",data[i]);
   }
   return 0;
} 

  • 35
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
### 回答1: C语言使用链表实现冒泡排序代码实现如下: struct listNode { int data; struct listNode *next; }; void BubbleSort(struct listNode* head) { struct listNode *p, *q; int temp; if (head == NULL || head->next == NULL) return; for (p = head; p->next != NULL; p = p->next) { for (q = head; q->next != NULL; q = q->next) { if (q->data > q->next->data) { temp = q->data; q->data = q->next->data; q->next->data = temp; } } } } ### 回答2: 下面是用C语言实现冒泡排序算法的链表形式的代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* createNode(int data) { struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); if (newNode == NULL) { printf("内存分配失败\n"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } struct Node* insertNode(struct Node* head, int data) { if (head == NULL) { return createNode(data); } else { struct Node* newNode = createNode(data); newNode->next = head; return newNode; } } struct Node* bubbleSort(struct Node* head) { if (head == NULL || head->next == NULL) { return head; } struct Node* current; struct Node* next; int swapped; do { swapped = 0; current = head; while (current->next != NULL) { next = current->next; if (current->data > next->data) { int temp = current->data; current->data = next->data; next->data = temp; swapped = 1; } current = next; } } while (swapped); return head; } void printList(struct Node* head) { struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { struct Node* head = NULL; int n, data; printf("请输入要排序的数字个数:"); scanf("%d", &n); for (int i = 0; i < n; i++) { printf("请输入第%d个数字:", i+1); scanf("%d", &data); head = insertNode(head, data); } printf("排序前的链表:"); printList(head); head = bubbleSort(head); printf("排序后的链表:"); printList(head); return 0; } ``` 以上代码创建了一个称为 Node 的结构体,表示链表中的节点。`createNode` 函数用于创建新的节点,`insertNode` 函数用于向链表插入节点。`bubbleSort` 函数用于实现冒泡排序算法并返回排好序的链表头节点。 在主函数中,首先获取要排序的数字个数,并逐个插入到链表中。然后调用 `bubbleSort` 对链表进行排序,最后打印排序前和排序后的链表。 请注意,这里的冒泡排序算法是从头节点开始两两比较相邻节点的值,如果顺序不对则交换。算法会一直遍历链表直到没有发生交换操作,即达到最佳排序顺序为止。 ### 回答3: 冒泡排序是一种简单的排序算法,通过反复交换相邻元素的位置来实现排序。使用链表实现冒泡排序需要以下步骤: 1. 定义链表节点结构体,包含一个整型数据域和一个指向下一个节点的指针域。 2. 创建链表,并向其中插入待排序的元素。 3. 定义一个排序函数,用于对链表进行冒泡排序。函数内部使用两层循环,外层循环遍历链表的节点个数次,内层循环进行相邻节点的比较和交换操作。 在内层循环中,如果当前节点的数据大于下一个节点的数据,则交换它们的位置;否则,继续比较下一对节点。 通过这样的循环操作,每次内层循环结束后,链表最后的节点就是当前未排序部分的最大值。 外层循环重复执行该过程,每次只需要遍历到上一轮未排序部分的前一个节点。 4. 在排序函数中添加适当的边界判断和指针操作,确保链表正确排序。 5. 输出排序后的链表。 以下是一个使用链表实现冒泡排序代码示例: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 typedef struct Node { int data; // 数据域 struct Node* next; // 指针域 } Node; // 创建链表 Node* createList(int arr[], int size) { Node* head = NULL; Node* tail = NULL; for (int i = 0; i < size; i++) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = arr[i]; newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } return head; } // 冒泡排序 void bubbleSort(Node* head) { if (head == NULL || head->next == NULL) { return; } Node* p; Node* q = NULL; int swapped; do { swapped = 0; p = head; while (p->next != q) { if (p->data > p->next->data) { int temp = p->data; p->data = p->next->data; p->next->data = temp; swapped = 1; } p = p->next; } q = p; } while (swapped); } // 打印链表 void printList(Node* head) { Node* p = head; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { int arr[] = {4, 2, 6, 3, 1, 5}; int size = sizeof(arr) / sizeof(arr[0]); Node* head = createList(arr, size); bubbleSort(head); printList(head); return 0; } ``` 运行结果为:1 2 3 4 5 6,表示链表已经成功按照升序进行冒泡排序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ken'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值