Sayan MohntA 's blog...welcome to my world

Me,Myself,My life and My View on Philosophy,IT,Mathematics, War,Google,Microsoft,Java, Society and Business and many more. A complete world of my own...

Friday, September 04, 2009

hav a luk plz

#include #include
using namespace std;
typedef enum { ASCENDING, DESCENDING } SortOrder;void arraySort(unsigned *, size_t, SortOrder);int binarySearch(unsigned *a, int len, unsigned val, SortOrder);void arrayPrint(unsigned *, size_t);void arrayRandomize(unsigned *, size_t, int);int random(int range);const size_t N = 7;const unsigned MAX_VAL = 101;
int main(int argc, char *argv[]) { unsigned array[N],n; time_t t; int x;
srand((unsigned)time(&t)); arrayRandomize(array,N,MAX_VAL);
cout << "Random: " <<> "; cin >> n; cout << n; if ((x = binarySearch(array,N,n,ASCENDING)) != -1) { cout << " found at index " << x << endl; } else { cout << " not found." << endl; } } return 0;}
//// Selection sort to arrange the elements of// the given array in the specified order.//void arraySort(unsigned *a, size_t len, SortOrder order) { size_t i = 0,j,min; unsigned t;
while (i < min =" i;" j =" i+1;" order ="="" min =" j;"> a[min]) min = j; } ++j; } t = a[min]; a[min] = a[i]; a[i++] = t; }}
//// Binary search for a specified value in the// given array. The array is sorted in the// specified order. Returns index of value if// found; -1 if not found.//int binarySearch(unsigned *a, int len, unsigned val, SortOrder order) { int left = 0, right = len-1, x;
do { x = (left + right) / 2; if (order == ASCENDING) { if (val < right =" x-1;" left =" x+1;"> a[x]) right = x-1; else left = x+1; } } while ((val != a[x]) && (left <= right)); if (val != a[x]) x = -1;
return x;}
//// Initialize array with random values in the// range [0 .. max)//void arrayRandomize(unsigned *a, size_t len, int max) { for (size_t i = 0; i < len; i++) { a[i] = random(max); }}
//// Print the given array//void arrayPrint(unsigned *a, size_t len) { for (size_t i = 0; i < len; i++) { cout << setw(2) << setfill('0') << a[i] << " "; }}
//// random: return a random in [0 .. range)//int random(int range) { float x = ((float)rand() / (float)RAND_MAX) * range; return (int)x;}