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
|
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MLS = 50;
const int SENTINEL = -1;
typedef int element;
class AList {
private:
element items[MLS];
int size;
int Read_int();
void Swap(int pos1, int pos2);
element Read_element();
void exit_method();
public:
void Menu();
void Read();
void Ran_Gen();
void BubbleSort();
void SelectionSort();
void InsertionSort();
void Print();
void LinearSearch(element target,bool&found,int&position);
void BinarySearch(element target,bool&found,int&position);
};
int main() {
srand(int(time(0)));
cout<< "\n\nSort and Search Demo Program, version 1.0\n"
<< "(c) 2011,\n\n";
AList B;
B.Menu();
}
void AList::Menu() {
element menuchoice;
element target_linear;
bool found_linear;
int position_linear;
element target_binary;
bool found_binary;
int position_binary;
const int keyboard=1,
random=2,
bubble=3,
insertion=4,
selection=5,
linear=6,
binary=7,
exit=8;
do {
cout<< "Current List: ";
if (menuchoice>0)
Print();
else
cout<< "(empty)\t";
if ((menuchoice>2)&&(menuchoice<6))
cout<<"(list is known to be ordered)\n\n";
else
cout<<"(list is not know to be ordered)\n\n";
cout<< "Actions:\n";
cout<< " 1. Reset the current list from the keyboard\n";
cout<< " 2. Reset the current list using randomly generated "
<< "elements\n";
cout<< " 3. Perform Bubble Sort on the current list\n";
cout<< " 4. Perform Insertion Sort on the current list\n";
cout<< " 5. Perform Selection Sort on the current list\n";
cout<< " 6. Perform Linear Search on the current list\n";
cout<< " 7. Perform Binary Search on the current list\n";
cout<< " 8. Quit the program\n\n";
cout<< "Choose an action: ";
menuchoice=Read_int();
cout<< endl;
switch (menuchoice) {
case 1:
Read();
break;
case 2:
Ran_Gen();
break;
case 3:
BubbleSort();
break;
case 4:
InsertionSort();
break;
case 5:
SelectionSort();
break;
case 6:{
LinearSearch(target_linear,found_linear,position_linear);
}
break;
case 7: {
BinarySearch(target_binary,found_binary,position_binary);
cout<< "The target was "<<found_binary<< " on the"
<< " current list in position "<<position_binary;
cout<< endl<<endl;
}
break;
case 8:
exit_method();
break;
default: cout<< "Please enter a valid menu choice: ";
menuchoice=Read_int();
;
}
}
while (menuchoice !=8);
}
void AList::Print() {
for (int i=0; i<size; i++)
cout<< items[i]<<" ";
}
void AList::Ran_Gen() {
int usersize;
int max;
int min;
int x;
cout<< "Enter low: ";
cin>> min;
cout<< "Enter high: ";
cin>> max;
cout<< "Enter size: ";
cin>> usersize;
for (int i = 0; i <usersize; i++)
x = rand()+(max-min+1)+min;
size = x;
|