Download Presentation

Loading in 2 Seconds...

1 / 19

# 排序和查找 - PowerPoint PPT Presentation

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

## PowerPoint Slideshow about '排序和查找' - stan

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 关于排序

1.选择排序

#define N 10

void SelSort(int a[N]) /*选择排序函数*/

{ int i, j, minj, t;

for(i=0; i<N-1; i++)

{ minj=i;

for(j=i+1; j<N; j++) if(a[j]<a[minj]) minj=j;

if(minj!=i)

{ t=a[i]; a[i]=a[minj]; a[minj]=t; }

}

}

void main()

{ int a[N],i;

for(i=0; i<N; i++) scanf("%d", a+i);

SelSort(a);

for(i=0; i<N; i++) printf("%6d", a[i]);

}

2.冒泡排序 例2：

#define N 10

void main()

{ int a[N], i, j, t;

printf("Input %d numbers:\n", N);

for(i=0; i<N; i++) scanf("%d", a+i);

for(i=1; i<N; i++)

for(j=0; j<N-i; j++)

if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; }

printf("Reslut:");

for(i=0; i<N; i++) printf("%6d", a[I]);

printf("\n");

}

*冒泡排序的改进：

void BubSort(int a[N])

{ int i, j, t, sorted;

for(i=1; i<N; i++)

{ sorted=1;

for(j=0; j<N-i; j++)

if(a[j]>a[j+1])

{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; sorted=0;}

if(sorted) break;

}

}

*3.插入排序

N元数组a[0]~a[N-1]由小到大排序：

a[0]~a[N-1]升序；算法停止。

#define N 10

void InsSort(int a[N]) /*N元数组插入排序*/

{ int i, j, x;for(i=1; i<N; i++){ x=a[i]; j=i-1; while(j>=0&&a[j]>x) { a[j+1]=a[j]; j--; }

a[j+1]=x;}

}

void main()

{ int a[N], i;for(i=0; i<N; i++) scanf("%d", &a[i]);InsSort(a);for(i=0; i<N; i++) printf("%6d", a[i]);

}

1.顺序查找

#include <stdio.h>

#include <string.h>

char *name[]={"zhao","qian","sun","li","wang",

"chen","liu","zhang"};

main()

{ char **first, **last, **mid;

char search[10];

int i, j, k;

printf("Please input the name:"); gets(search);

first=name;

last=name+sizeof(name)/sizeof(char *);

while(first<=last)

if(strcmp(*first, search)!=0) first++;

else { printf("Have found it!,name:%s\n",*first);

break;

}

if(first>last) printf("The name isn't exist!");

}

2.二分法查找

@ 当h<=r时执行以下步骤：

x<a[m]，显然x在a[h]~a[m-1]之间，则h不变;

r=m-1; m=(h+r)/2; 转@ x>a[m]，显然x在a[m+1]~a[r]之间，则r不变;

h=m+1; m=(h+r)/2; 转@ 若h>r，则表明查找失败.

main()

{ int a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int b, s;

scanf("%d",&s);

b=locate(a, 10, s);

if(b==-1)

printf("The number isn't in the array!\n");

else

printf("The number is in the array!\n");

}

int locate(int a[], int n, int x)

{ int h, r, m;

h=0;

r=n-1;

m=(h+r)/2;

while(h<=r&&x!=a[m])

if(x<a[m])

{ r=m-1; m=(h+r)/2; }

else

{ h=m+1; m=(h+r)/2; }

if(h>r) return -1;

return m;

}

1、编函数int fun(int *a, int *n, int pos[]), 其功能是：

2、编一个程序，其功能是：读入十个整数存入一

3、编一个函数void sort(char **q)，它的功能是：

main通过键盘输入。

4、编一个程序，读入十个整数存入一维数组n中，