Bubble sort

Solution:

#include <iostream>
using namespace std;
int main()
{
int i,j;
int n,temp; // In case of dealing with 'char', instead of 'int', 'char' would have to be written
cin>>n;
int a[n];
for(i=0;i<n;i++) cin>>a[i];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++) cout<<a[i]<<endl;
return 0;
}
view raw Bubble sort.cpp hosted with ❤ by GitHub

Quicksort

Note: I have used pivot as the middle element each time.

#include <iostream>
using namespace std;
void quicksort(int a[],int left, int right)
{
int i,j,temp,pivot;
i=left;
j=right;
pivot=a[(left+right)/2];
while(i<=j)
{
//For left side
while(a[i]<pivot) i++;
//For right side
while(a[j]>pivot) j--;
// a[i], which is greater than the pivot and a[j], which is less than the pivot, those two are swapping,
// to make the all the elements left of the pivot, smaller than the pivot and right of the pivot, greater than the pivot
if(i<=j)
{
swap(a[i],a[j]);
/* Since after the swap, a[i] is smaller than the pivot & a[j] is greater than the pivot,
so there is no need to start checking from i or j, that's why i++ and j-- is done */
i++;
j--;
}
}
// After this loop, the left side of the present pivot is smaller and the right side is greater.
// Now it's turn to quick sort the left and the right part
if(left<j)
{
quicksort(a,left,j);
}
if(right>i)
{
quicksort(a,i,right);
}
}
int main()
{
int n,l;
cout<<"Enter the size of the array : "<<endl;
cin>>n;
int ar[n];
cout<<"Enter the unsorted array elements : "<<endl;
for(l=0;l<n;l++) cin>>ar[l];
quicksort(ar,0,n-1);
cout<<"The sorted array : "<<endl;
for(l=0;l<n;l++)
{
cout<<ar[l]<<endl;
}
return 0;
}
view raw Quick sort.cpp hosted with ❤ by GitHub

Is n a prime no. !

Normally with O(n) time:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b=0,i;
    printf("Input your desired number:\n");
    scanf("%d", &a);
    for(i=2; i<a; i++)
    {
        if(a%i==0)
        {
        b=1;
        break;
        }
    }
    if(b==1)
    {
        printf("The number is not prime!\n");
    }
    else
    {
        printf("The number is prime!!\n");
    }
    return 0;
}

With more efficient O(root n) time:

#include <iostream>

using namespace std;

int main()
{
    int a,b=0,i;
    cout<<"Input your desired number:"<<endl;
    cin>>a;
    for(i=2; i*i<=a; i++)
    {
        if(a%i==0)
        {
            b=1;
            break;
        }
    }
    if(b==1)
    {
        cout<<"The number is not prime!"<<endl;
    }
    else
    {
        cout<<"The number is prime!!"<<endl;
    }
    return 0;
}

The most efficient- Sieve:

#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define qu                  queue
#define pqu                 priority_queue
#define vc                  vector
#define vi                  vector<int>
#define vll                 vector<long long>
#define pii                 pair<int,int>
#define pis                 pair<int,string>
#define psi                 pair<string,int>
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define loop0(i,n)          for(int i=0;i<n;i++)
#define loop1(i,n)          for(int i=1;i<=n;i++)
#define loopab(i,a,b)       for(int i=a;i<=b;i++)
#define REV(i,n)            for(i=n; i>=0; i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d: ",z)
#define PI                  3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<r && ty>=0 && ty<c
#define intlim              2147483648
#define MAX                 10000007
#define inf                 100000008

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

bool p[MAX+2];
vector<int>prime;

void sieve()
{
    ms(p,0);
    p[0]=p[1]=1;
    int root=sqrt(MAX);
    for(int i=2; i<=root; i++)
    {
        if(p[i]==0)
        {
            prime.pb(i);
            for(int j=i*i; j<=MAX; j+=i)
            {
                p[j]=1;
                if(i%2==1) j+=i;
            }
        }
    }
}

int main()
{
    //freopen("out.txt","w",stdout);
    sieve();
    int n;
    cin>>n;
    ms(p,0);
    if(p[n]==0) pf("It is a prime number.\n");
    else pf("It is not a prime number.\n");

    return 0;
}


2. No. of maximum possible pairs

#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define qu                  queue
#define pqu                 priority_queue
#define vc                  vector
#define vi                  vector<int>
#define vll                 vector<long long>
#define pii                 pair<int,int>
#define pis                 pair<int,string>
#define psi                 pair<string,int>
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define loop0(i,n)          for(int i=0;i<n;i++)
#define loop1(i,n)          for(int i=1;i<=n;i++)
#define loopab(i,a,b)       for(int i=a;i<=b;i++)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d: ",z)
#define PI                  3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<r && ty>=0 && ty<c
#define intlim              2147483648
#define MAX                 1000000
#define inf                 10000000

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

bool p[MAX+2];
vector<int> prime;

void sieve()
{
    p[0]=p[1]=1;
    int root=sqrt(MAX);
    for(int i=2; i<=root; i++)
    {
        if(p[i]==0)
        {
            prime.pb(i);
            for(int j=i*i; j<=MAX; j+=i)
            {
                p[j]=1;
                if(i%2!=0) j+=i;
            }
        }
    }
}

int gold(int n)
{
    if(n==4) return 1;
    int pairs=0;
    for(int i=1; prime[i]<=n/2; i++)
    {
        int a=n-prime[i];
        if(p[a]==0)
        {
            pairs++;
        }
    }
    return pairs;
}

int main()
{
    //CIN;
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    sieve();
    int n;
    while(sf(n)==1 && n!=0)
    {
        if(n<4 || n%2==1)
        {
            pf("Invalid input\n");
            continue;
        }
        pf("No. of possible pairs is %d\n",gold(n));
    }

    return 0;
}