Server IP : 172.16.15.8 / Your IP : 3.137.169.56 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/sdjones/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// For Assignment 4 // Add the selection sort // p.137 // ADT - List: array of items with var. length // read strings from a file and put them in the list; then output the strings // #include <iostream> using namespace std; const int MAX = 50; class List { int data[MAX]; int length; public: List() { length = 0; } int Length() const { return length; } void Insert(int item) { data[length] = item; length ++; } void Print() const { for(int i=0; i<length; i++) cout << data[i] << " "; cout << "\n"; } // sequential search, p.139 bool IsPresent(int item) const { bool found = false; for(int i=0; i<length && !found; i++) if(data[i] == item) found = true; return found; } void SelectionSort() // p.140 - decending oder { int temp; int passCount; int maxIndex; for(passCount=0; passCount<length-1; passCount++) { maxIndex = passCount; for(int k=passCount+1; k<length; k++) { if( data[maxIndex] > data[k] ) maxIndex = k; } temp = data[maxIndex]; // swap data[maxIndex] = data[passCount]; data[passCount] = temp; } } void SelectionSort_R() // descending { int temp; int passCount; int minIndex; for(passCount=0; passCount<length-1; passCount++) { minIndex = passCount; for(int k=passCount+1; k<length; k++) { if( data[minIndex] < data[k] ) minIndex = k; } temp = data[minIndex]; // swap data[minIndex] = data[passCount]; data[passCount] = temp; } } }; int main() { List real; // read the data in, p.138 int x; // hold each item cin >> x; while(cin) { real.Insert(x); cin >> x; } // out cout << "Before sort: "; real.Print(); cout << "Ascending order - "; real.SelectionSort(); real.Print(); cout << "Decending order - "; real.SelectionSort_R(); real.Print(); return 0; }