伪冒泡排序------数组/链表实现
来源:互联网 发布:影响原油数据 编辑:程序博客网 时间:2024/06/05 03:58
/*Bubblesort_1.cpp-------伪冒泡排序*/
/*
分别用数组和链表实现伪装冒泡排序。
第i个元素,依次跟第i+1,i+2个元进行比较,如果前者大,这交换元素。
*/
/*----------------数组实现------------*/
/*
#include <stdio.h>
#define LEN 5
void init_arr(int *pArr,int len);
void swap(int &temp1,int &temp2);
void show_arr(int *pArr,int len);//数组的首地址和数组的长度
void bubble_sort(int *pArr,int len);//伪冒泡排序
int main(void)
{
int arr[LEN];
init_arr(arr,LEN);
show_arr(arr,LEN);
printf("排序\n");
bubble_sort(arr,LEN);
show_arr(arr,LEN);
return 0;
}
void init_arr(int * pArr,int len)
{
int i;
int value;
for(i=0;i<len;i++)
{
printf("请输入第%d个元素的值:",i+1);
scanf("%d",&value);
pArr[i]=value;
}
return;
}
void swap(int * temp1,int * temp2)
{
int temp;//临时变量
temp=*temp1;
*temp1=*temp2;
*temp2=temp;
return;
}
void show_arr(int *pArr,int len)
{
int i;
for(i=0;i<len;i++)
{
printf("%d ",pArr[i]);
}
printf("\n");
return;
}
void bubble_sort(int * pArr,int len)
{
int i,j;
for(i=0;i<len-1;i++)
{
for(j=i+1;j<len;j++)
{
if(pArr[i]>pArr[j])
{
swap(&pArr[i],&pArr[j]);
}
}
}
return;
}
*/
/*------------------------------------*/
/*----------用链表实现----------------*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *pNext;
}Node,*pNode;
void swap(int * temp1,int * temp2);
pNode init_list();
void traverse_list(pNode pHead);
void bubble_sort(pNode pHead);
bool is_empty(pNode pHead);
int len_list(pNode pHead);
int main(void)
{
pNode pHead=NULL;
pHead=init_list();
traverse_list(pHead);
printf("排序\n");
bubble_sort(pHead);
traverse_list(pHead);
return 0;
}
pNode init_list()
{
int len;//结点个数
int value;//节点的值
int i;
pNode pHead=(pNode)malloc(sizeof(Node));//申请头节点
pHead->pNext=NULL;//表示空表
if(NULL==pHead)
{
printf("动态内存分配失败!\n");
exit(-1);
}
pNode pTail=pHead;
pTail->pNext=NULL;
printf("请输入初始化的结点个数:");
scanf("%d",&len);
for(i=0;i<len;i++)
{
printf("请输入第%d个数的值:",i+1);
scanf("%d",&value);
pNode pNew=(pNode)malloc(sizeof(Node));
if(NULL==pNew)
{
printf("内存分配失败\n");
exit(-1);
}
pNew->data=value;
pNew->pNext=NULL;
pTail->pNext=pNew;
pTail=pNew;
}
return pHead;
}
void traverse_list(pNode pHead)
{
if( is_empty(pHead) )
{
printf("链表为空\n");
return ;
}
pNode pOut=pHead->pNext;
while(NULL!=pOut)
{
printf("%d ",pOut->data);
pOut=pOut->pNext;
}
printf("\n");
return ;
}
bool is_empty(pNode pHead)
{
if(NULL==pHead->pNext)
{
return true;
}
else
{
return false;
}
}
void bubble_sort(pNode pHead)
{
int len=len_list(pHead);
int i,j;
pNode pNow=NULL,pNow_next=NULL;
if(0==len)
{
printf("空表不排序\n");
return ;
}
for(i=0,pNow=pHead->pNext;i<len-1;i++,pNow=pNow->pNext)
{
for(j=i+1,pNow_next=pNow->pNext;j<len;j++,pNow_next=pNow_next->pNext)
{
if(pNow->data>pNow_next->data)
{
swap(&(pNow->data),&(pNow_next->data));
}
}
}
return ;
}
int len_list(pNode pHead)
{
int len=0;
pNode pOut=pHead->pNext;
while(NULL!=pOut)
{
len++;
pOut=pOut->pNext;
}
return len;
}
void swap(int * temp1,int * temp2)
{
int temp;
temp= *temp1;
*temp1= *temp2;
*temp2=temp;
return ;
}
- 伪冒泡排序------数组/链表实现
- 冒泡排序-----数组/链表实现
- 冒泡升序排序的数组实现和链表实现
- Java数组实现冒泡排序
- Java 数组实现冒泡排序
- 冒泡排序(数组实现)
- 链表实现冒泡排序
- 链表实现冒泡排序
- 链表实现冒泡排序
- 冒泡排序实现整数数组的排序
- 排序之冒泡排序(数组实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)
- 判断二叉树是否对称
- 递归复制文件夹
- 并查集
- linq 动态排序 order by
- 伪冒泡排序------数组/链表实现
- 索引扫描
- HDU 1796 How many integers can you find
- 【SpringMVC整合MyBatis】提供学习参考的项目源码
- 欢迎使用CSDN-markdown编辑器
- 常用容器
- ArrayList类的toArray方法
- 几何画板怎样画对数函数图像
- SATA硬盘和IDE硬盘的区别