Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Sunday, 25 November 2018

Count All Increasing Sub Sequences GeeksForGeeks Python Program

for _ in range(int(input())):
    n=int(input());
    l=list(map(int,input().split()));
    lz=[0]*10;
    for i in range(len(l)):
        for j in range(l[i]-1,-1,-1):
            lz[l[i]]+=lz[j];
        lz[l[i]]+=1;
    print(sum(lz));
    
    

Friday, 10 November 2017

Program to Implement Insertion In Heap in C/C++

// Insertion In Heap Tree Data Structure

#include<bits/stdc++.h>
using namespace std;

int arr[10],n;

void insertHeap(int num,int loc)
{


int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}

arr[loc]=arr[par];
loc=par;
}

arr[0]=num;
}
void disp(int n)
{
for(int i=0;i<n;i++)
cout<<" "<<arr[i];
}


main()
{
n=0;

insertHeap(50,n);
n+=1;
insertHeap(30,n);
n+=1;
insertHeap(40,n);
n+=1;
insertHeap(29,n);
n+=1;
insertHeap(55,n);
n+=1;
cout<<"Insertion In HEAP: ";
disp(n);


}



Monday, 6 November 2017

C program to Implement Priority Queue Menu Driven Program

/*Program of priority queue using linked list*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
int priority;
int info;
struct node *link;
}*front=NULL;

void insert(int item, int item_priority);
int del();
void display();
int isEmpty();

main()
{
int choice,item,item_priority;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("03.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Input the item to be added in the queue : ");
scanf("%d",&item);
printf("Enter its priority : ");
scanf("%d",&item_priority);
insert(item, item_priority);
break;
case 2:
printf("Deleted item is %d\n",del());
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

void insert(int item,int item_priority)
{
struct node *tmp,*p;

tmp=(struct node *)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf("Memory not available\n");
return;
}
tmp->info=item;
tmp->priority=item_priority;
/*Queue is empty or item to be added has priority more than first element*/
if( isEmpty() || item_priority < front->priority )
{
tmp->link=front;
front=tmp;
}
else
{
p = front;
while( p->link!=NULL && p->link->priority<=item_priority )
p=p->link;
tmp->link=p->link;
p->link=tmp;
}
}/*End of insert()*/

int del()
{
struct node *tmp;
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
else
{
tmp=front;
item=tmp->info;
front=front->link;
free(tmp);
}
return item;
}/*End of del()*/

int isEmpty()
{
if( front == NULL )
return 1;
else
return 0;

}/*End of isEmpty()*/

void display()
{
struct node *ptr;
ptr=front;
if( isEmpty() )
printf("Queue is empty\n");
else
{    printf("Queue is :\n");
printf("Priority       Item\n");
while(ptr!=NULL)
{
printf("%5d        %5d\n",ptr->priority,ptr->info);
ptr=ptr->link;
}
}
}/*End of display() */

C program to Implement Queue using Stacks in C++

/*
 * C Program to Implement Queues using Stacks
 */
#include <stdio.h>
#include <stdlib.h>

void push1(int);
void push2(int);
int pop1();
int pop2();
void enqueue();
void dequeue();
void display();
void create();

int st1[100], st2[100];
int top1 = -1, top2 = -1;
int count = 0;

void main()
{
    int ch;

    printf("\n1 - Enqueue element into queue");
    printf("\n2 - Dequeu element from queue");
    printf("\n3 - Display from queue");
    printf("\n4 - Exit");
    create();
    while (1)
    {
        printf("\nEnter choice");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            enqueue();
            break;
        case 2:
            dequeue();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(0);
        default:
            printf("Wrong choice");
        }
    }
}

/*Function to create a queue*/
void create()
{
    top1 = top2 = -1;
}

/*Function to push the element on to the stack*/
void push1(int data)
{
    st1[++top1] = data;
}

/*Function to pop the element from the stack*/
int pop1()
{
    return(st1[top1--]);
}

/*Function to push an element on to stack*/
void push2(int data)
{
    st2[++top2] = data;
}

/*Function to pop an element from th stack*/

int pop2()
{
    return(st2[top2--]);
}

/*Function to add an element into the queue using stack*/
void enqueue()
{
    int data, i;

    printf("Enter data into queue");
    scanf("%d", &data);
    push1(data);
    count++;
}

/*Function to delete an element from the queue using stack*/

void dequeue()
{
    int i;

    for (i = 0;i <= count;i++)
    {
        push2(pop1());
    }
    pop2();
    count--;
    for (i = 0;i <= count;i++)
    {
        push1(pop2());
    }
}

/*Function to display the elements in the stack*/

void display()
{
    int i;

    for (i = 0;i <= top1;i++)
    {
        printf(" %d ", st1[i]);
    }
}

Friday, 1 September 2017

Insertion Sort Array C++ Program in Efficient Way

#include<bits/stdc++.h> //to include all header file
using namespace std;
main()
{
int n,i,j,k;
cout<<"Enter Size of Array:";
cin>>n;

int a[n];

cout<<"Enter Elements Into Array:";
for(i=0;i<n;i++)
cin>>a[i];

for(i=0;i<n-1;i++)
{
j=i;
while(j>=0&&a[j]<a[j+1])
{
swap(a[j],a[j+1]); //pre-defined function in STL
j--;
}
}
for(i=0;i<n;i++)
{
cout<<a[i];
}
}

Amazon1Ads