-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch
106 lines (100 loc) · 1.64 KB
/
search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<stdio.h>
void Bubble_Sort(int *num, int len)
{
for(int i=0;i<len;i++)
for(int j=0;j<len-i-1;j++)
if(num[j]>num[j+1]){
int temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
int binarySearch(int a[],int n,int key){
int low=0,high=n-1;
int mid;
while(low<=high){
mid=low+(high-low)/2;
if(key<a[mid])
high=mid-1;
else if(key>a[mid])
low=mid+1;
else
return mid;
}
return 0;
}
void insert_sort(int a[],int n)
{
int i,j,t;
for(i=1;i<n;i++)
{
t=a[i];
for(j=i-1;j>=0&&t>a[j];j--)
a[j+1]=a[j];
a[j+1]=t;
}
}
void quicksort(int left,int right)
{
int i,j,temp,t;
if(left>right)
return;
temp=a[left];
i=left;
j=right;
while(i!=j)
{
while(a[j]>=temp&&i<j)
j--;
while(a[i]<=temp&&i<j)
i++;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
a[left]=a[i];
a[i]=temp;
quicksort(left,i-1);
quicksort(i+1,right);
}
void shell_sort(int a[], int len)
{
int gap, i, j, temp;
for(gap = len/2; gap > 0; gap /= 2)
{
for(i = gap; i < len; i++)
{
for(j = i-gap; j>=0 && a[j]>a[j+gap]; j -= gap)
{
temp = a[j];
a[j] = a[j+gap];
a[j+gap] = temp;
}
}
}
}
int main()
{
int num[9];
int i,x,t;
for(i=0;i<9;i++)
{
scanf("%d",&num[i]);
}
Bubble_Sort(num, 9);
for(i=0;i<9;i++)
printf("%d ",num[i]);
while(1)
{
printf("请输入要查找的数字:");
scanf("%d",&x);
if(t=binarySearch(num,9,x))
printf("第%d个\n",t+1);
else
printf("没找到!\n");
}
return 0;
}