Hye there..
I just want some ideas to make my program better..
Below is the code. Any comment or recommendation are appreciated. :)
/*
* Name: Isihan dan Carian [Sorting and Searching]
* version: 4.0.1
* Copyright ©
* Support: http://tempek.net78.net
* Updates: August 27, 2008
* Compiler : Dev-C++ 4.9.9.2
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void random(int []);
void mergesorting(int [], int ,int );
void combine(int [], int , int ,int );
int const SIZE=100;
int search(int , int []);
int binary(int , int []);
int main()
{
int num[SIZE],number,NUMBER,ans;
char j;
random(num);
mergesorting(num,0,99);
system("color 3");
cout << "\n\n";
cout << "100 integers ( Descending order ) : " << endl << endl;
for(int index=0; index<100; index++)
{
cout << "Index " << index << " : " << num[index] << endl;
}
do
{
cout << "\nEnter a number which you want to search from sorted list : ";
cin >> NUMBER;
cout << "\nEnter 1 for SEQUENTIAL SEARCH or 2 for BINARY SEARCH : " ;
cin >> number;
switch(number)
{
case 1 : ans=search(NUMBER,num);
if(ans==-1)
cout << endl << endl << NUMBER << " is not in the sorted list." ;
else
cout<< endl << endl << NUMBER <<" is in the sorted list which located in index " << ans;
break;
case 2 : ans=binary(NUMBER,num);
if(ans==-1)
cout << endl << endl << NUMBER << " is not in the sorted list." ;
else
cout<< endl << endl <<NUMBER <<" is in the sorted list which located in index " << ans;
break;
}
cout <<"\n\n\n\nDo you want to continue (Y/N)? : ";
cin >> j;
if(j=='n' || j=='N')
return 0;
}while(j=='y' || j=='Y');
getchar();
return 0;
}
/* Randomize the number between 101-999 */
void random(int array[])
{
srand((unsigned)time(0));
int random_integer;
int lowest=101, highest=999;
int range=(highest-lowest)+1;
cout <<"Unsorted 100 random integers : " << endl << endl;
for(int x=0; x<100; x++)
{
random_integer = (range*rand()/(RAND_MAX + 1))+lowest;
array[x] = random_integer;
cout << "Index " << x << " : " << array[x] <<endl;
}
}
/* Merge sort funtion */
void mergesorting(int T[], int first,int last)
{
int mid;
if (first<last)
{ mid = (first+last)/2;
mergesorting(T, first, mid);
mergesorting(T, mid+1, last);
combine(T, first,mid, last );
}
}
void combine(int T[], int first, int mid,int last)
{
int ifirst, iTfana, imid, k;
int Tfana[SIZE];
ifirst=first;
iTfana=first;
imid=mid+1;
while((ifirst<=mid) &&(imid<=last))
{
if (T[ifirst]>=T[imid])
{
Tfana[iTfana]= T[ifirst];
++ifirst;
}
else
{
Tfana[iTfana]= T[imid];
++imid;
}
++iTfana;
}
if (ifirst>mid)
{
for (k=imid; k<= last; k++)
{
Tfana[iTfana] = T[k];
++iTfana;
}
}
else
{
for (k=ifirst; k<= mid; k++)
{
Tfana[iTfana] = T[k];
++iTfana;
}
}
for (k=first; k<= last; k++) {
T[k]=Tfana[k];
}
}
/*Sequantial search funtion*/
int search(int search_key , int NUM[])
{
int p;
int index =-1;
for ( p = 0; p < SIZE; p++ )
{
if (search_key > NUM[p] )
break;
else if (search_key == NUM[p])
{
index = p;
break;
}
}
system("CLS");
cout << endl << "Number of tests : " << p;
return index;
}
/*Binary search function*/
int binary( int search_key, int NUM[] )
{
bool found = false;
int index = -1,a=1;
int MIDDLE;
int LEFT = 0;
int RIGHT = SIZE-1;
while (( LEFT<= RIGHT ) && (!found))
{
a++;
MIDDLE = (LEFT + RIGHT ) / 2;
if ( NUM[MIDDLE] == search_key)
{
found = true;
index = MIDDLE;
}
else if (NUM[MIDDLE] > search_key)
LEFT= MIDDLE+ 1;
else
RIGHT= MIDDLE - 1;
}
system("CLS");
cout << endl << "Number of tests : " << a;
return index;
}