C Program to sort using insertion sort

This program demonstrates insertion sort in C. Insertion sort  proceeds by considering each item in the list and placing it in the position it belongs to in the sorted list. Other elements will be shifted either to the left or to the right in order to make space for the current element. Insertion sort is only efficient for smaller lists and cannot be used in larger lists since the complexity will be high. In the best case insertion sort has a time complexity of O(n) and in the worst case, the time complexity is O(n2).

Program


#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],small,n,i,j;

printf("Enter the size of the array: ");

scanf("%d",&n);

printf("Enter the elements: \n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=1;i<n;i++)

{

small=a[i];

j=i-1;

while(j>=0&a[j]>small)

{

a[j+1]=a[j];

j--;

}

a[j+1]=small;

}

printf("\nArray after sorting\n");

for(i=0;i<n;i++)

printf(" %d",a[i]);

getch();

}


Output


Insertion sort program in c
Insertion Sort











Related post:-  C++ program to implement insertion sort

C program to sort using selection sort

This program demonstrates selection sort in C. The time complexity of selection sort is O(n2). The advantage of selection sort is its simplicity and the disadvantage is that it is more time consuming when sorting large arrays. Selection sort finds smallest element in a list and replaces it with the left most element. This procedure will be repeated till the whole list is sorted.

Program


//Implementation of selection sort program in c

#include<stdio.h>

#include<conio.h>

void main()

{

int a[25],i,j,n,small,t;

printf("Enter the size of the array\n");

scanf("%d",&n);

printf("Enter the elements\n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=0;i<n;i++)                   //Selection sort starts here.

{

    small=a[i];

    for(j=i+1;j<n;j++)

    {

        if(a[j]<small)

        {

            small=a[j];

            t=a[i];

            a[i]=small;

            a[j]=t;

        }

    }

}

printf("Array after selection sort\n");

for(i=0;i<n;i++)

{

printf("%d ",a[i]);

}

getch();

}


Output

C program for selection sort
Selection sort

c program to find the length of a string without using strlen

This program finds the length of a string without using strlen() function. strlen() is a built in function available in c, which returns the length of a string. The program reads a string and stores it in the character array a[]. By default a character array stores a '/0' at the end, which marks the end of string. The while loop uses loop variable i, which is initially set to 0. At each iteration in the while loop the value of count is incremented. The loop continues until, the value of a[i] equals '/0'.

Program



#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int count=0,i=0;

char a[10];

printf("Enter the string");

scanf("%s",&a);

while(a[i]!='\0')

{

count++;i++;

}

printf("%d",i);

getch();

}


Output

Length of a string without using strlen
c program to find the length of a string without using strlen


C++ program to swap 2d character array

This program swaps two 2d character arrays.  The limit of the two arrays are read to the variables n1 and n2. Then the program reads the strings one by one to both arrays. After reading the strings the program swaps the two arrays using a for loop and a while loop. The program uses strcpy() function to copy the string from one location to other.

Program



// Program to swap two dimensional character arrays
#include<iostream.h> #include<conio.h> #include<string.h> void main() { char a[5][10],b[5][10],t[10]; int i,j,n1,n2; cout<<"Enter the value for n1: "; cin>>n1; cout<<"Enter the value for n2: " ; cin>>n2; cout<<"Enter the strings to first array"; for(i=0;i<n1;i++) cin>>a[i]; cout<<"Enter the strings to second array"; for(i=0;i<n2;i++) cin>>b[i]; for(i=0;i<n1&&n2;i++) { strcpy(t,a[i]); strcpy(a[i],b[i]); strcpy(b[i],t); } if(i==n1) { while(i<n2) { strcpy(a[i],b[i]); i++; } } else { while(i<n1) { strcpy(b[i],a[i]); i++; } } cout<<"\nArrays after swapping\n"; cout<<"\nArray 1\n"; for(i=0;i<n2;i++) cout<<"\n"<<a[i]; cout<<"\nArray 2\n"; for(i=0;i<n1;i++) cout<<"\n"<<b[i]; getch(); }

C++ Program to demonstrate virtual function working

This program demonstrates virtual function in c++. When there exist two functions with same function name in both base and derived class the compiler identifies which function to use at run time based on the type of object pointed to by base pointer. The run time polymorphism in c++ is achieved through virtual functions.

Program


 //Run time polymorphism

#include<iostream.h>

#include<conio.h>

class base

{

public:

void display()

{

cout<<"\nDisplay Base";

}

virtual void show()

{

cout<<"\nShow Base";

}

};

class derived:public base

{

public:

void display()

{

cout<<"\nDisplay Derived";

}

void show()

{

cout<<"\nShow Derived";

}

};

void main()

{

base *bptr;

base b;

bptr=&b;

bptr->display();

bptr->show();

derived d;

bptr=&d;

bptr->display();

bptr->show();

getch();

}


Output



Display Base
Show Base
Display Base
Show Derived

C program to find the transpose of a sparse matrix

This program finds the transpose of a sparse matrix in c. A sparse matrix is a matrix where majority of its elements are 0. A sparse matrix can be represented by using 3 tuple method to avoid memory wastage. This program reads a matrix as input and finds its 3  tuple representation. Then the program finds the transpose of this sparse matrix.

Program


//program to find the transpose of a sparse matrix

#include<stdio.h>

#include<conio.h>

void main()

{

int m,p,n,i,j,k,t=0,l,a[10][10],s[10][10],st[10][10];

printf("Enter the size of the orginal array");

scanf("%d%d",&m,&n);

printf("\nEnter the array elements\n");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

scanf("%d",&a[i][j]);

}

}

k=1;

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

if(a[i][j]!=0)

{

s[k][0]=i;

s[k][1]=j;

s[k][2]=a[i][j];

k++;

if(j>t)

t=j;

}

}

}

s[0][0]=m;

s[0][1]=n;

s[0][2]=k-1;

printf("3 tuple representation of the given matrix is\n");

for(i=0;i<k;i++)

{

printf("\n");

for(j=0;j<3;j++)

{

printf(" %d",s[i][j]);

}

}

p=1;

l=k-1;

printf("\nt= %d, l= %d",t,l);

for(i=0;i<=t;i++)

{

k=1;

for(j=0;j<l;j++)

{

if(s[k][1]==i)

{

st[p][1]=s[k][0];

st[p][0]=s[k][1];

st[p][2]=s[k][2];

p++;

k++;

}

else

k++;

}

}

st[0][0]=s[0][1];

st[0][1]=s[0][0];

st[0][2]=s[0][2];

printf("\nTranspose of the sparse matrix is\n");

for(i=0;i<p;i++)

{

printf("\n");

for(j=0;j<3;j++)

{

printf(" %d",st[i][j]);

}

}

getch();

}



C++ program to demonstrate unary operator overloading

This program demonstrate unary minus overloading in c. The value of x is read as input in the function getdata() and the putdata() function is used to display the value of x. The function void operator -() performs the overloading, which changes the sign of x to negative. In the main function, the statement a.getdata(); calls the function getdata() and the statement a.putdata(); calls the function putdata(). The statement -a; calls the function void operator -(), which overloads the unary minus operator. The statement a.putdata(); after the statement cout<<"\nAfter Overloading value of x="; prints the negative value of x.

Program

//program to overload unary operator -

#include<iostream.h>

#include<conio.h>

class overld

{

int x;

public:

void getdata()

{

cout<<"Enter the value for x";

cin>>x;

}

void putdata()

{

cout<<x;

}

void operator -()

{

x=-x;

}

};

void main()

{

overld a;

a.getdata();

cout<<"\nBefore Overloading value of x=";

a.putdata();

-a;

cout<<"\nAfter Overloading value of x=";

a.putdata();

getch();

}

C++ program to demonstrate file operations

This program demonstrates file operations in c++. The program reads as input five names and stores it in a file named sample.txt using the out statement, then the file is closed. Next the program reads a new name to be searched in the file and stores it in the variable name. Then the program program compares each name in the file with the value stored in the variable name, if there is a match the string is found in the file. Otherwise the string is not present in the file.


Program


//c++ program to check whether a particular word is present in the file

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<string.h>

#include<process.h>

void main()

{

int i;

char name[20],t[20];

ofstream out;

out.open("sample1.txt");

cout<<"Enter five names\n\n";

for(i=0;i<5;i++)

{

cin>>name;

out<<name<<"\n";

}

out.close();

cout<<"Enter the name to be searched for";

cin>>name;

ifstream in;

in.open("sample1.txt");

while(in)

{

in.seekg(2,ios::beg);

in>>t;

cout<<"\n"<<t;

}

in.close();

in.open("sample1.txt");

while(in)

{

in>>t;

if(strcmp(name,t)==0)

{

cout<<"\nString found!!!";

getch();

in.close();

exit(0);

}

}

cout<<"\nString not found!!!";

in.close();

getch();

}