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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
# include <iostream>
using namespace std;
const int MLS = 50;
const int SENTINEL = -1;
typedef int element;
int read_int();
class AList {
private:
element items[MLS];
int size;
int Read_int();
element Read_element();
public:
void Read();
void BubbleSort();
void Swap(int pos1, int pos2);
void Print();
void TernarySearch(element target, bool & found,
int & position);
};
int main(){
class AList A;
element target;
bool found;
int position;
A.Read();
A.Print();
A.BubbleSort();
A.Print();
A.TernarySearch( target,found, position);
}
void AList::Read(){
//PRE: NONE
//POST: The N.O AList is valid date passed by the user
element userval;
size=0;
cout<<"Enter Numbers from list, type "<< SENTINEL;
cout <<" to stop: ";
userval= read_int();
while((userval != SENTINEL) && (size < MLS)){
items[size]=userval;
size++;
if (size < MLS)
userval = read_int();
else
cout << "Array based list is full, moving on" <<endl;
}
}
void AList::Print(){
//PRE: the N.O is Valid
//POST: The N.O is unchanged and the elemetns have been displayed
for(int i=0; i<size; i++)
cout<<items[i]<<" ";
cout << endl;
}
void AList::BubbleSort(){
//PRE: The N.O AList is valid
//POST: The N.O Alist is unchanged except its elements have been
// reorder to be in asscending order
for(int i=0; i<size -1; i++)
for(int j=0; j <size -1 -i; j++)
if(items[j] > items[j+1])
Swap(j, j+1);
else
;
}
int read_int(){
//This Function checks for data type vadaliation
int val;
cin>>val;
while(!cin.good()){
cout<<"Invalid type, should be an intger, try again: ";
cin.clear();
cin.ignore(80,'\n');
cin>>val;
}
return val;
}
void AList::Swap(int pos1, int pos2){
//Pre: The N.O AList must be valid
//0 <= pos 1 and 0 <=pos2 < size
//Post: The N.O is unchanged execpt the elements in pos1 and pos2
//have exchanged
element temp;
temp = items[pos1];
items[pos1]= items[pos2];
items[pos2] = temp;
}
void AList::TernarySearch(element target, bool &found, int &position){
//PRE: The N.O. is valid and target is a valid target. The N.O. AList
// must be in asscending order
//POST: The N.O. AList is unchanged, and if the target exist on the
// target N.O. AList, then found will be true and position will
// be the subscript of an instance of target on the N.O. AList
// otherwise (target does not exist on the N.O. AList) then found
// will be false and position will be UNDEFINED
int low;
int high;
int mid;
int midlow;
int midhigh;
cout<<"Eneter a target element to search: ";
cin>> target;
low=0;
high=size -1;
found=false;
while((!found) && (low<=high) ){
midlow=(low+high)/3;
midhigh=((low+high)/3 )*(2);
if (target= items[midlow]){
found=true;
position=midlow;
}
else if (target= items[midhigh]){
found=true;
position=midhigh;
}
else if (target > items[midhigh])
low= midhigh +1;
else if(target > items[midlow])
low= midlow +1;
else if(target < items[midlow])
high= midlow+1;
else if(target < items[midhigh])
high= midhigh+1;
else //((target < items[midhigh]) && (target > items[midlow])
low= midlow + 1;
}
cout<<position;
cout<< endl;
}
}
|