Sort, Pattern, Stack

#include<iostream> //Bubble
using namespace std;
int main(){
    int array[100],n,i,compare, step;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>array[i];
    for(step=0;step<n-1;step++)
    {
        for(compare=0;compare<n-1-step;compare++)
        {
            if(array[compare]>array[compare+1])
                swap(array[compare],array[compare+1]);
        }
    }
    for(i=0;i<n;i++)
        cout<<array[i]<<" ";
}




#include<iostream>  //Selection
using namespace std;
int main(){
    int n;
    int array[100];
    cin>>n;
    int step,compare,minimum, i;
    for(i=0;i<n;i++){
        cin>>array[i];
    }
    for(step=0;step<n;step++){
        minimum=step;
        for(compare=step+1; compare<n; compare++){
            if(array[compare]<array[minimum]){
                    minimum=compare;
            }
        }
        swap(array[step], array[minimum]);
    }
    for(i=0; i<n; i++)
        cout<<array[i]<<" ";
}

#include <iostream> //pattern

using namespace std;

int main()
{
    int i, j, count=0;
    string str, pattern;
    getline(cin,str);
    getline(cin,pattern);

    for ( i = 0; i<str.length(); i++)
    {
        for (  j = 0; j<pattern.length(); j++)
        {
            if (pattern[j]!=str[i+j])
                break;
        }
        if (pattern.length()==j)
        {
            cout<<"Pattern found in location:"<<i<<endl;
            count++;
        }
    }
        if (count ==0)
            cout<<"Pattern not found!!!!";
        else
            cout<<"No of pattern found: "<<count<<endl;
}

#include<iostream> //stack
#define MAX_SIZE 5
using namespace std;
int main() {
    int item, choice, i;
    int arr_stack[MAX_SIZE];
    int top = 0;
    int exit = 1;

    do {
        cout << "\n\n\nnStack Main Menu";

        cout << "\n1.Push \n2.Pop \n3.Display \nOthers to exit";
        cout << "\n\nEnter Your Choice : ";
        cin>>choice;
        switch (choice) {
            case 1:
                if (top == MAX_SIZE)
                    cout << "\n## Stack is Full! Overflow";
                else {
                    cout << "\nEnter The Value to be pushed : ";
                    cin>>item;
                    cout << "\n## Position : " << top << ", Pushed Value  :" << item;
                    arr_stack[top++] = item;
                }
                break;
            case 2:
                if (top == 0)
                    cout << "\n## Stack is Empty! Underflow";
                else {
                    --top;
                    cout << "\n## Position : " << top << ", Popped Value  :" << arr_stack[top];
                }
                break;
            case 3:
                cout << "\n## Stack Size : " << top;
                for (i = (top - 1); i >= 0; i--)
                    cout << "\n## Position : " << i << ", Value  :" << arr_stack[i];
                break;
            default:
                exit = 0;
                break;
        }
    } while (exit);
    return 0;

}

Comments