折半查找:
该算法只适用于关键字有序(增序/降序)的查找,效率比顺序查找效率高很多。
下边是对于增序序列的 查找代码:(降序序列只需将low和high对换即可)
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 20
int bin_search(int a[], int len,int key )
{
int low,mid,high;
low = 0;
high = len-1;
while( low <= high)
{
mid = (low + high)/2;
if( a[mid] == key ) return mid;
else if( a[mid] < key )
low = mid + 1;
else
high = mid -1;
}
return -1;
}
int main()
{
int i,len,number,ans,a[MAX_LENGTH];
printf("please input the length of your number\n");
scanf("%d",&len);
printf("please input the numbers\n");
for( i = 0; i < len; i++ )
{
scanf("%d",&a[i]);
}
printf("please input the number you want to find\n");
scanf("%d",&number);
ans = bin_search( a, len, number );
if( ans != -1 )
printf("the number %d is located at %dth in array\n",number,ans);
else
printf("can't find the number %d in array\n",number);
return 0;
}
直接插入排序:
基本思想是:从第一个依次往下,找到该值在它前边所有有序的序列的位置,并放置其中,依次下去直到最后一个。
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 20
void insert_sort(int a[], int len)
{
int i,j,temp;
for( i = 1; i < len; i++ )
{
j = i - 1;
while( (a[i] < a[j]) && (i >= 1) )
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
j = --i - 1;
}
}
}
int main()
{
int i,len,a[MAX_LENGTH];
printf("please input the length of your array needed to be sorted\n");
scanf("%d",&len);
printf("please input the array needed to be sorted\n");
for( i = 0; i < len; i++ )
{
scanf("%d",&a[i]);
}
insert_sort( a, len );
printf("the sorted array is:\n");
for( i = 0; i < len; i++ )
{
printf("%d ",a[i]);
}
return 0;
}
选择排序
在所有元素中找到最小的元素,放置第一个位置,之后在剩下的元素中找到最小的元素,放在第二个位置,依次下去。
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 20
void select_sort( int a[], int len )
{
int i,j,min,temp;
for( i = 0; i < len; i++ )
{
min = i;
for( j = i+1; j < len; j ++)
{
if( a[min] > a[j] )
min = j;
}
if( min != i )
{
temp = a [i];
a[i] = a[min];
a[min] = temp;
}
}
}
int main()
{
int i,len,a[MAX_LENGTH];
printf("please input the length of your array needed to be sorted\n");
scanf("%d",&len);
printf("please input the array needed to be sorted\n");
for( i = 0; i < len; i++ )
{
scanf("%d",&a[i]);
}
select_sort( a, len );
printf("the sorted array is:\n");
for( i = 0; i < len; i++ )
{
printf("%d ",a[i]);
}
return 0;
}
相邻元素比较,小的放大的前边,依次比较下去,最大的放最后边,循环以上操作,加上flag可以提高效率
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 20
void bubble_sort( int a[], int len)
{
int i,j,temp,flag;
flag = 1;
for( i = 0; (i < len) && (flag == 1); i++ )
{
flag = 0;
for( j = 0; j < (len-i-1); j++ )
{
if( a[j] > a [j+1] )
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
flag = 1;
}
}
}
}
int main()
{
int i,len,a[MAX_LENGTH];
printf("please input the length of your array needed to be sorted\n");
scanf("%d",&len);
printf("please input the array needed to be sorted\n");
for( i = 0; i < len; i++ )
{
scanf("%d",&a[i]);
}
bubble_sort( a, len );
printf("the sorted array is:\n");
for( i = 0; i < len; i++ )
{
printf("%d ",a[i]);
}
return 0;
}
快速排序:
采用了递归方法。选一个基准元素,将小于该元素的放置该元素左边,大于的放置右边,之后对左边的和右边的子序列再进行以上处理,依次下去直至最终的子序列个数为1。
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 20
void quick_sort( int a[], int bot, int end )
{
int i,j,temp;
if(bot < end)
{
i = bot + 1;
j = end;
while(1)
{
while( !( a[i] >= a[bot] || i == end ) )
i++;
while( !( a[j] <= a[bot] || j == bot ) )
j--;
if(i < j)
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
else
break;
}
temp = a[bot];
a[bot] = a[j];
a[j] = temp;
quick_sort(a,bot,j-1);
quick_sort(a,j+1,end);
}
}
int main()
{
int i,len,a[MAX_LENGTH];
printf("please input the length of your array needed to be sorted\n");
scanf("%d",&len);
printf("please input the array needed to be sorted\n");
for( i = 0; i < len; i++ )
{
scanf("%d",&a[i]);
}
quick_sort( a, 0, len - 1 );
printf("the sorted array is:\n");
for( i = 0; i < len; i++ )
{
printf("%d ",a[i]);
}
return 0;
}