Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- system("color f0");
- int count=0;
- float numb;
- cout << "This will sort your numbers enter 0 to end";
- cout << endl << "Are you going to exceed 100 numbers? (y/n) ------> "; // Conserve memory space
- char ans,order;
- cin >> ans;
- cout << "Do you want the numbers sorted in ascending or desending order? (a/d) ----> ";
- cin >> order;
- char choice;
- if (ans=='y')
- {
- cout << endl << "How many numbers do you need to sort? ";
- int choice;
- cin >> choice;
- }
- else
- {
- choice=100;
- }
- float numbAr[choice];
- bool stop=false;
- while (stop==false)
- {
- cout << endl << "Enter a number ";
- cin >> numb;
- if (numb==0 || numb==0.0)
- {
- stop=true;
- }
- else
- {
- numbAr[count]=numb;
- count++;
- }
- }
- int outer=0,inner=0,swapct=0,sum=0;
- float temp;
- bool swapped=true;
- cout << endl << endl << "Sorted List: " << endl;
- while (outer<count && swapped!=false)
- {
- swapct=0;
- inner=0;
- while (inner<=(count-2))
- {
- if (numbAr[inner]<numbAr[inner+1])
- {
- temp=numbAr[inner+1];
- numbAr[inner+1]=numbAr[inner];
- numbAr[inner]=temp;
- swapct++;} inner++;
- }
- if (swapct==0)
- {
- swapped=false;
- }
- sum+=swapct;
- outer++;
- }
- switch (order)
- {
- case 'a':
- for (int i=(count-1);i>=0;i--)
- {
- cout << " " << numbAr[i];
- }
- cout << endl << endl;
- cout << "Amount of swaps = " << sum << endl << endl;
- break;
- case 'd':
- for (int i=0;i<count;i++)
- {
- cout << " " << numbAr[i];
- }
- cout << endl << endl;
- cout << "Amount of swaps = " << sum << endl << endl;
- break;
- case 'A':
- for (int i=(count-1);i>=0;i--)
- {
- cout << " " << numbAr[i];
- }
- cout << endl << endl;
- cout << "Amount of swaps = " << sum << endl << endl;
- break;
- case 'D':
- for (int i=0;i<count;i++)
- {
- cout << " " << numbAr[i];
- }
- cout << endl << endl;
- cout << "Amount of swaps = " << sum << endl << endl;
- break;
- default:
- cout << endl << "Invalid output parameter" << endl << endl;
- };
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement