C語言之排序基礎
排序
1、排序
冒泡
選擇法 排序
代碼:
#include
#include
#include
void paixu_xuanze(int puke[],int count);
int main()
{
int puke[]={9,6,3,5,2,4,7}; //2 6 3 5 9
paixu_xuanze(puke,7);
for(int i=0;i<7;i++)
{
printf("%d\t",puke[i]);
}
return 0;
}
void paixu_xuanze(int puke[],int count)
{
int min_xuhao;
int temp;
int i;
for(int j=0;jpuke[i]) //1---4
{
min_xuhao=i;
}
else
{
}
}
if(min_xuhao!=j) //0--3
{
temp=puke[min_xuhao];
puke[min_xuhao]=puke[j];
puke[j]=temp;
}
}
return;
}
插入法排序
9,6,3,5,2,4,7
9,6,3,5,2,4,7
6 9 3 5 2 4 7
6 3 9 5 2 4 7
3 6 9 5 2 4 7
3 5 6 9 2 4 7
2 3 5 6 9 4 7
2 3 4 5 6 9 7
2 3 4 5 6 7 9
代碼:
#include
#include
#include
void paixu_charu(int puke[],int count)
{
int min_xuhao=0;
int temp;
if(puke[0]>puke[1])
{
temp=puke[1];
puke[1]=puke[0];
puke[0]=temp;
}
else
{
;
}
if(puke[1]>puke[2])
{
temp=puke[2];
puke[2]=puke[1];
puke[1]=temp;
}
else
{
;
}
if(puke[0]>puke[1])
{
temp=puke[1];
puke[1]=puke[0];
puke[0]=temp;
}
else
{
;
}
return;
}
int main()
{
int puke[]={9,6,3,5,2,4,7}; //2 6 3 5 9
paixu_charu(puke,7);
for(int i=0;i<7;i++)
{
printf("%d\t",puke[i]);
}
return 0;
}
//完善後的插入排序算法 代碼:
#include
#include
#include
void paixu_charu(int puke[],int count)
{
int temp;
int i;
int j;
for(j=0;j<4;j++)
{
for(i=j;i>=0;i--) //0-3
{
if(puke[i]>puke[i+1]) //0--1 //1--2 0--1
{
temp=puke[i+1];
puke[i+1]=puke[i];
puke[i]=temp;
}
else
{
break;
}
}
}
return;
}
int main()
{
int puke[]={9,6,3,5,2}; //3 6 9 5 2
paixu_charu(puke,5);
for(int i=0;i<5;i++)
{
printf("%d\t",puke[i]);
}
return 0;
}
查找:
順序查找
二分查找--------
3 6 9 23 56
找 23,如果找到,請返回下標;如果沒找到,返回-1
代碼:----還需要完善的代碼
#include
#include
#include
int find_binarySearch(int puke[],int count,int data)
{
int pos=-1;
int low=0;
int hign=count-1;
int mid=(low+hign)/2;
if(data==puke[mid])
{
pos=mid;
}
else if(data
#include
#include
int find_binarySearch(int puke[],int count,int data)
{
if(count<1)
{
return -1;
}
int pos=-1;
int low=0;
int hign=count-1;
int mid=(low+hign)/2;
if(data==puke[mid])
{
pos=mid;
}
else if(data