void bubble_(myType *a,int high)//冒泡排序
{
int t = 0;
for (int i = 1; i < high; i++)
{
for (int j = 0; j < i; j++)
{
if (a[i]<=a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
- 从未排好的序列中拿出首元素,并把它赋值给temp变量;
void insert(myType *a,int high)//插入排序
{
int i,j,t;
for (i = 1; i < high; ++i)
{
t = a[i];
for (j = i;j > 0 && a[j-1]>t; j--)
{
a[j] = a[j-1];
}
a[j] = t;
}
void select(myType *a,int high)//选择排序
{
int min,t;
for (int i = 0; i < high; i++)
{
min = i;
for (int j = 1; j < high; j++)
{
if (a[j]
基本思路
int binary(myType *a,int high)//二分法查找
{
int mid = 0,low = 0,value;
printf("Please input need looking for number:\n");
scanf("%d",&value);
while(low <= high)
{
mid = (low + high)/2;
if (value == a[mid])
{
return mid;
}
else if(value > a[mid])
{
low = mid + 1;
}
else
{
high = mid -1;
}
}
return -1;
}
void print_func(myType *a,int high)
{
for (int i = 0; i < high; ++i)
{
printf("%d\n",a[i] );
}
}
int main(int argc, char const *argv[])
{
int a[]={23,12,5,122,3,235,6,3456,34,1,34,23,4,35,34,534,8,346,234};
int high = sizeof(a)/4;
printf("Please select sort mode: 1.bubble\t2.insert\t3.select\n");
int x;
scanf("%d",&x);
switch(x)
{
case 1:bubble_(a,high);break;
case 2:insert(a,high);break;
case 3:select(a,high);break;
default :printf("error\n");break;
}
printf("The number coordinates are:%d\nif number is '-1' not find\n",binary(a,high) );
system("pause");
//print_func(a,high);
return 0;
}
#include
#define DEBUG0
typedef int myType;
void bubble_(myType *a,int high)//冒泡排序
{
int t = 0;
for (int i = 1; i < high; i++)
{
for (int j = 0; j < i; j++)
{
if (a[i]<=a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
#ifdef DEBUG1
for (int i = 0; i < high; ++i)
{
printf("%d\n",a[i] );
}
#endif
}
void insert(myType *a,int high)//插入排序
{
int i,j,t;
for (i = 1; i < high; ++i)
{
t = a[i];
for (j = i;j > 0 && a[j-1]>t; j--)
{
a[j] = a[j-1];
}
a[j] = t;
}
#ifdef DEBUG1
for (int i = 0; i < high; ++i)
{
printf("%d\n",a[i] );
}
#endif
}
void select(myType *a,int high)//选择排序
{
int min,t;
for (int i = 0; i < high; i++)
{
min = i;
for (int j = 1; j < high; j++)
{
if (a[j] a[mid])
{
low = mid + 1;
}
else
{
high = mid -1;
}
}
return -1;
}
void print_func(myType *a,int high)
{
for (int i = 0; i < high; ++i)
{
printf("%d\n",a[i] );
}
}
int main(int argc, char const *argv[])
{
int a[]={23,12,5,122,3,235,6,3456,34,1,34,23,4,35,34,534,8,346,234};
int high = sizeof(a)/4;
printf("Please select sort mode: 1.bubble\t2.insert\t3.select\n");
int x;
scanf("%d",&x);
switch(x)
{
case 1:bubble_(a,high);break;
case 2:insert(a,high);break;
case 3:select(a,high);break;
default :printf("error\n");break;
}
printf("The number coordinates are:%d\nif number is '-1' not find\n",binary(a,high) );
//print_func(a,high);
return 0;
}