1、氣泡排序、雞尾酒排序、選擇排序、插入排序、快速排序/********
#include
//氣泡排序
voidmaopao(int a,int n)
int i, j;
for(i = 0; i < n - 1; i++)
for(j = 0; j < n - 1 -i; j++)
if(a[j] > a[j + 1])
swap(a,j,j+1);
//交換兩個數
void swap(inta,int i,int j)
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
//雞尾酒排序
voidcocktailsort(int a,int n)
int left = 0,right = n - 1;
int i;
while(left < right)
for(i = left; i < right; i++)
if(a[i] > a[i + 1])
swap(a,i,i+1);
right--;
for(i = right; i > left; i--)
if(a[i - 1] > a[i])
swap(a,i-1,i);
left++;
//列印函式
void print(inta,int n)
int i;
for(i = 0; i < n; i++)
printf("%d",a[i]);
printf("\n");
//選擇排序
void choosesort(inta,int n)
int i, j, min;
for(i = 0; i < n - 1; i++)
min= i;
for(j = i + 1; j < n; j++)
if(a[min] > a[j])
min= j;
if(min != i)
swap(a,min,i);
//插入排序
void insertsort(inta,int n)
int i,j;
int get;
for(i = 1; i < n; i++)
get= a[i];
j= i - 1;
while((j >= 0) && (get a[j + 1] = a[j];
j--;
a[j + 1] = get;
int main()
int a = ;
int n;
n = sizeof(a) / sizeof(int);
print(a,n);
//cocktailsort(a,n);
//choosesort(a,n);
insertsort(a,n);
print(a,n);
return 0;
2、順序查詢、折半查詢(二分查詢):
#include
int search(int *str,int len, int key);
int main()
int a[5] = ;
int pos;
int find;
int i;
find = 99;
for(i = 0; i < 5; i++)
printf("%d ",a[i]);
printf("\n");
pos = search(a,5,find);
printf("the position of %d isno.%d\n",find,pos);
return 0;
int search(int *str,int len ,int key)
int i;
for(i = 0; i < len; i++)
if(str[i] == key)
return i+1;
折半查詢(二分查詢):
#include
int halfsearch(int*str, int len, int find);
int main()
int num[10] = ;
int find;
int pos;
int i;
for(i = 0; i < 10; i++)
printf("%d",num[i]);
printf("\ninput the num you want tofind:\n");
scanf("%d",&find);
pos = halfsearch(num,10,find);
printf("the position of %d isno.%d\n",find,pos);
return 0;
int halfsearch(int*str, int len, int find)
int low = 0,high = (len - 1),mid;
int i;
while(low <= high)
mid = (low + high) / 2;
if(find > str[mid])
low = mid + 1;
else if(find < str[mid])
high = mid - 1;
else
return (mid + 1);
return -1;
3、二叉排序樹的插入、查詢、刪除:
#include
#include
#define ok 1
#define error 0
typedef struct nodetree,*treeptr;
//前序遍歷建立二叉樹
voidcreatetree(treeptr *t)
char a;
scanf("%c",&a);
if('#' == a)
(*t) = null;
return ;
(*t) = (treeptr)malloc(sizeof(tree));
(*t)->data = a;
createtree(&(*t)->lchild);
createtree(&(*t)->rchild);
//中序遍歷
voidmidorderprint(treeptr t)
if(t == null)
return;
midorderprint(t->lchild);
printf("%c",t->data);
midorderprint(t->rchild);
return ;
//後序遍歷
intafterorderprint(treeptr t)
if(null == t)
returnerror;
afterorderprint(t);
printf("%c",t->data);
afterorderprint(t);
return ok;
int main()
treeptr t;
printf("please input preorderdata:\n");
createtree(&t);
printf("midorderprint:");
midorderprint(t);
printf("\n");
printf("afterorderprint:\n");
afterorderprint(t);
printf("\n");
return 0;
快速排序 :
查詢最小的 k 個元素
題目:輸入 n 個整數,輸出其中最小的 k 個。
例如輸入1,2,3,4,5,6,7和8這8個數字,
則最小的4個數字為1,2,3和4。
#include
#define n 10
void quick_sort(ints, int l, int r)
int i, j, x;
if(l < r)
i = l;
j = r;
x = s[l];
while(i < j)
while((i < j) && (s[j]>= x))
j--;
if(i < j)
s[i++] = s[j];
while((i < j) && (s[i]< x))
i++;
if(i < j)
s[j--] = s[i];
s[i] = x;
quick_sort(s, l, i - 1);
quick_sort(s, i + 1, r);
int main()
int i;
int a[n];
int k;
printf("please input 10numbers!\n");
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
printf("please input the numbers ofmin!\n");
scanf("%d",&k);
quick_sort(a, 0, 9);
for(i = 0; i < 10; i++)
printf("%d ",a[i]);
printf("\nthe least numbers is:\n");
for(i = 0; i < k; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
查詢和排序
二分查詢演算法 def search list,m low 0 high len list 1 while low high mid low high 2 if list mid m high mid 1 elif list mid m low mid 1 else return mid retur...
查詢和排序
題目描述 把乙個陣列最開始的若干個元素搬到陣列的末尾,我們稱之為陣列的旋轉。輸入乙個非遞減排序的陣列的乙個旋轉,輸出旋轉陣列的最小元素。例如陣列為的乙個旋轉,該陣列的最小值為1。note 給出的所有元素都大於0,若陣列大小為0,請返回0。思路 折半查詢。如果a mid 大於a right left ...
查詢和排序
includeint main for i 1 ia j for i 1 i k i return 0 time limit 1 sec memory limit 128 mb hardlevel 0 submit 386 solved 198 submit status web board 輸入n...