标签:
简单顺序查找,有序表的二分查找,索引顺序表的查找
1 #include "iostream" 2 #include "iomanip" 3 #include "time.h" 4 #include "stdlib.h" 5 using namespace std; 6 7 #define Max 20 8 9 /* 10 *简单顺序查找 11 */ 12 int Seq_Search(int A[],int n,int x) 13 { 14 int i=n; 15 time_t start,end; 16 start = clock(); 17 A[0] = x; 18 while(A[i]!=A[0])i--; 19 end = clock(); 20 cout<<"num:"<<i<<endl; 21 cout<<"Seq_Search time:"<<(double)(end - start )/1000<<"S"<<endl; 22 return 0; 23 } 24 25 /* 26 *二分查找 27 */ 28 int bin_search(int A[],int n,int x) 29 { 30 time_t start,end; 31 int mid,low=0,high=n-1;//初始化查找区域 32 start = clock(); 33 bool flags = false; 34 while(!flags) 35 { 36 mid = (low + high) /2; 37 if(A[mid]==x)//查找成功 38 { 39 cout<<"num:"<<mid<<endl; 40 flags = true; 41 } 42 else 43 { 44 if(A[mid]<x){ 45 low = mid +1; 46 }else high = mid -1; 47 } 48 } 49 end = clock(); 50 cout<<"bin_Search time:"<<(double)(end - start)/1000<<"S"<<endl; 51 return 0; 52 } 53 54 /* 55 *索引顺序表的查找 56 */ 57 int index_serach(int A[],int n,int x,int flags) 58 { 59 int index[Max][2]; 60 int i,j,count = 0,indexdata; 61 indexdata = A[0]; 62 index[count][0] = 0; 63 index[count][1] = A[0]; 64 i=1; 65 time_t start,end; 66 start = clock(); 67 if(flags==1)//块之间降序 68 { 69 while(i<n)//构建索引表 70 { 71 if(A[i]>index[count][1]) 72 { 73 count++; 74 index[count][0] = i; 75 index[count][1] = A[i]; 76 } 77 i++; 78 } 79 i=0; 80 while(index[i][1]<x)i++; 81 if(i+1<=count) 82 { 83 j = index[i+1][0]; 84 }else j = n; 85 i = index[i][0]; 86 for(;i<j;i++) 87 { 88 if(A[i]==x) 89 { 90 cout<<"num:"<<i<<endl; 91 break; 92 } 93 } 94 }else if(flags==0){//块之间升序 95 96 while(i<n)//构建索引表 97 { 98 if(A[i]<index[count][1]) 99 { 100 count++; 101 index[count][0] = i; 102 index[count][1] = A[i]; 103 } 104 i++; 105 } 106 i=0; 107 while(index[i][1]>x)i++; 108 if(i+1<=count) 109 { 110 j = index[i+1][0]; 111 }else j = n; 112 i = index[i][0]; 113 for(;i<j;i++) 114 { 115 if(A[i]==x) 116 { 117 cout<<"num:"<<i<<endl; 118 break; 119 } 120 } 121 } 122 end = clock(); 123 cout<<"index list:"<<endl; 124 for(i=0;i<=count;i++) 125 { 126 cout<<"index:"<<index[i][0]<<",data:"<<index[i][1]<<endl; 127 } 128 cout<<"index_Search time:"<<(double)(end - start)/1000<<"S"<<endl; 129 130 return 0; 131 } 132 int main() 133 { 134 135 int i; 136 int A[Max]={10,10,6,9,6,5,20,18,17,13,30,25,28,27,40,39,35,32,50,48}; 137 int B[Max]={50,52,40,43,44,45,30,38,37,36,20,29,28,27,10,15,17,19,16,14}; 138 cout<<"initialize Array:"<<endl; 139 for(i=0;i<Max;i++) 140 { 141 cout<<setw(5)<<A[i]<<" "; 142 if((i+1)%10==0)cout<<endl; 143 } 144 cout<<endl; 145 cout<<"Seq_Search:"<<A[2]<<endl; 146 Seq_Search(A,Max-1,A[2]); 147 cout<<"bin_search:"<<A[2]<<endl; 148 bin_search(A,Max-1,A[2]); 149 150 int flags; 151 cout<<"please input flags:[1,索引表块内降序降序;0,索引表快内升序;其他,退出]"<<endl; 152 cout<<"flags:"; 153 while(cin>>flags) 154 { 155 switch(flags) 156 { 157 case 1: 158 for(i=0;i<Max;i++) 159 { 160 cout<<setw(5)<<A[i]<<" "; 161 if((i+1)%10==0)cout<<endl; 162 } 163 cout<<endl; 164 cout<<"index_serach:"<<A[3]<<endl; 165 index_serach(A,20,A[3],flags); 166 cout<<"please input flags:[1,索引表块内降序降序;0,索引表快内升序;其他,退出]"<<endl; 167 cout<<"flags:"; 168 break; 169 case 0: 170 for(i=0;i<Max;i++) 171 { 172 cout<<setw(5)<<B[i]<<" "; 173 if((i+1)%10==0)cout<<endl; 174 } 175 cout<<endl; 176 cout<<"index_serach:"<<B[4]<<endl; 177 index_serach(B,20,B[4],flags); 178 cout<<"please input flags:[1,索引表块内降序;0,索引表块内升序;其他,退出]"<<endl; 179 cout<<"flags:"; 180 break; 181 default: 182 exit(0); 183 184 } 185 } 186 return 0; 187 }
标签:
原文地址:http://www.cnblogs.com/minmsy/p/5020091.html