Posts

Showing posts from 2018

Insertion Sort

Image
Sorting : - Input: A Sequence of n numbers (a 1 ,a 2 …….a n ). Output : A permutation (recording) <a’ 1 ,a’ 2 ,….a’ n > of the input sequence such that a’ 1 <=a’ 2 <=,….<=a’ n . Insertion Sort Insertion sort is an efficient sorting algorithm for sorting a small number of elements. Insertion sort works the way people sort a hand of playing card. We start with empty hand and card face down on the table. We remove one card at a time from the table and insert it into the correct position in the left hand. For correct position of card we compare it with each of the card already in the hand in left. algorthms-cormen Algorithm  INSERT-SORT(A) for j <-- 2 to length[A]   do key <-- A[j]      Insert A[j] into the sorted sequence A[1.... j-1]      i <-- 1      while i > 0 and A[j] > key          do A[i+1] <-- A[i]              i <-- i-1      A[i+1] <-- key Ex: A = (5,2,4,6,1,3)  the index j indicate the

Dynamic Memory Allocation in C

Image
Dynamic Memory Allocation in C First, we should discuss the little bit about the static memory allocation. Static memory allocation means fixed size.   Ex:  int x; All primitive data types take static memory allocation. Array and structure also are static variable   int arr[5]; structure are user define data types.  Ex: struct student  s[5];  fixed number of record can be stored in the above structure. Memory size is fixed. So we call it static memory allocation. DYNAMIC MEMORY ALLOCATION Dynamic memory allocation means  storage/memory can be allocated to variables during the runtime is called Dynamic Memory Allocation. The number of elements in array increase and decrease in Dynamic memory allocation. For  dynamic memory allocation we have 4 library fuction define under <stdlib.h> as follows -- malloc() -- used to allocate memory to structure. calloc()  -- used to allocate memory to array. realloc() -- used to increase or decrease  the size of array. free()  

Implementation of stack Using Array

Implementation  of stack Using Array #include<stdio.h> void pop(); void push(); void display(); int i,n,ch=0,top,item,stack[50]; int main() {     printf("Enter the number of element in Stack:\t");     scanf("%d",&n);     top=-1;     printf("Stack Operations");     printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");    do     {         printf("Enter the choice: ");         scanf("%d",&ch);         switch(ch)         {             case 1:push();                    break;             case 2:pop();                    break;             case 3:display();                    break;             case 4:exit(0);                    break;             default:printf("!!Wrong choice!!\n");         }     }     while(ch!=4);     return 0; } void push(int item) {     if(top==n-1)         printf("Overflow\n");     else     {        

Stack Data Structure with PUSH,POP and STACK-EMPTY Algorithm and Code in C

Image
Stack Data Structure Stacks are data structures that follow the Last In First Out (LIFO) principle or First in last out(FILO) .  The last item to be inserted into a stack is the first one to be deleted from it. For example, you have a stack of trays on a table. The tray at the top of the stack is the first item to be moved if you require a tray from that stack. Inserting and deleting elements Stacks have restrictions on the insertion and deletion of elements. Elements can be inserted or deleted only from one end of the stack from the  top .  The element at the  top   is called the top element.  The operations of inserting element is called push() and deleting elements is called pop(). When the top element of a stack is deleted, if the stack remains non-empty, then the element just below the previous top element becomes the new top element of the stack. For example, in the stack of trays, if you take the tray on the top and do not replace it, then the second tray aut

Input Output function in C

Input/Output Function Standard o/p function          Console o/p function     (stdio.h)                               (conio.h)             printf()                                 cprintf() putc() putchar() printf v/s cprintf printf is a predefine o/p function of stdio.h header file. It is used to print the content on the standard o/p device(Monitor). If we apply some properties like text colour then it will not be reflected in the text printed by printf function. cprintf is a predefine o/p function of conio.h header file it is used to print the content on the console screen. If we apply some property like text colour then it gets reflected in the text printed by cprintf function. Example:- #include <stdio.h> #include<conio.h> void main() {    clrscr();     textcolor(Red);    cprintf("Hello\n");    printf("Hi"):    getch(): } Both printf and cprintf function is used to display formatted output.

Queue in Data Structure(DS)

Image
Queue Queues are data structures that follow the First In First Out (FIFO) i.e. the first element that is added to the queue is the first one to be removed. Elements are always added to the back(rear) and removed from the front. Think of it as  a line of people waiting for a bus. The person who is at the beginning of the line is the first one to enter the bus.   Basic features of Queue Like a stack, the queue is also an ordered list of elements of similar data types. A queue is a FIFO( First in First Out ) structure. Once a new element is inserted into the Queue, all the elements inserted before the new element in the queue  must be removed, to remove the new element. In a simple queue, there is a disadvantage after deleting element we cannot insert the element in the queue because of insertion  is possible from the rear but deletion is performed from the front.                                           insertion                                     ------------------

virtual function and pure virtual function

Virtual function A virtual function a member function which is declared within a base class and is re-defined (Override) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for the function call. They are mainly used to achieve Runtime polymorphism Functions are declared with a virtual keyword in a base class. The resolving of a function call is done at Run-time. #include<iostream> using namespace std; class Base { public:     virtual void print()     {         cout<<"Print the Base class"<<endl;     }     void show()     {         cout<<"Show base class"<<endl;     } }; class Derived:public Base { public:     void print()

friend function in cpp

Friend Function In general only member function of a class have access to the private member of the class.However, it is possible to allow a non-member function access to the private member of a class by declaring it as a friend of a class. To make a function friend of a class we include its prototype in the class declaration and preceded it with the 'friend' keyword. characteristic of friend function-- 1. It is not in the scope of the class through which it has been declared as a friend. 2. A friend function cannot be called using the object of that class. It can be invoked like a normal function without   the help of an object. 3. It cannot access member character directly. 4. It can use object name to access member variable. 5. It can be declared either can be public or protected part of a class without affecting its meaning. 6. Usually, it has the object as an argument. #include<iostream> using namespace std; class FrdTest {     int x,y; public

Function overloading in cpp

Function overloading C++ program for function overloading.Function overloading means two or more functions can have the same name but either the number of arguments or the data type of arguments has to be different. Return type has no role because the function will return a value when it is called and at compile time compiler will not be able to determine which function to call. Function overloading is also known as compile time polymorphism. #include <iostream> using namespace std; void display(int); void display(float); void display(int, float); int main() {     int a = 5;     float b = 5.5;     display(a);     display(b);     display(a, b);     return 0; } void display(int x) {     cout << "Integer number: " << x << endl; } void display(float x) {     cout << "Float number: " << x << endl; } void display(int x1, float x2) {     cout << "Integer number: " << x1;  

Concepts of OOP

OOP CONCEPTS Object-Oriented Programming is a paradigm that provides many concepts such as inheritance, Abstraction, Encapsulation, polymorphism, etc. What is Abstraction? Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. What is Encapsulation?  Encapsulation simply means binding object state(fields) and behavior (methods) together. If you are creating a class, you are doing encapsulation. What is Polymorphism? Polymorphism is an object-oriented programming feature that allows us to perform a single action in a different way. What is Inheritance? When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism. What is Class? Collection of objects is called class. It is a logical entity. A class can also be defined as a blueprint from which you can create an individual object. Class does