Showing posts with label c code examples. Show all posts
Showing posts with label c code examples. Show all posts

Friday, December 2, 2011

C program for Bubble Sort

Today I am going to share how to perform bubble sort in C programming language. What is Bubble Sort? Bubble sort is a very simple sorting algorithm which compares consecutive numbers and swap them if both are not in right order. This gives the biggest element at the end in each inner loop cycle. Here is an animated example for bubble sort:
#include
#include

  void bubbleSort(int arr[],int length)
  {
        int i,j,temp;
         for(i=length;i>0;i--)
         {
            for(j=0;j<=i;j++)
   { 
                if(arr[j]>arr[j+1])
                { 
//swap if previous value is bigger then next value
                    temp=arr[j];
     arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
   }// End of Inner For loop
        }// End of Outer For loop.
  }// End of BubbleSort Function
  


 void main()
 {

  
  int data[] = {4,3,5,2,9,2,1,8,7,0}
  int i;

  clrscr();

  printf("\nData before sorting: ");
  
  for( i=0; i<10; i++)
  { 
   printf("%d , ",data[i]);
  }
       
  bubbleSort(data,10);

  printf("\nData after sorting: ");
  
  for( i=0; i<10; i++)
  {
   printf("%d , ",data[i]);
  }
  
  } //End of this example.

Sunday, September 4, 2011

C program for Binary Search

Hello, Today I am sharing how to implement Binary Search in C Programming 
language. Here is the basic algorithm for binary search:


C program for binary search :

#include<stdio.h>

int main(){
  int a[8];
  int i,x,first,last,mid;

printf("\nEnter 8 sorted elements like 
               [2 4 7 9 12 13 16 20]");
for(i=0;i<8;i++)
{
scanf("%d",&a[i]);
}
printf("\nYou entered following numbers:");
for(i=0;i<8;i++){
printf(" %d",a[i]);
}
printf("\nEnter the number you want to search: ");
scanf("%d",&x);
  
first=0,last=n-1;
while(first<=last){
      mid=(first+last)/2;
      
 if(x==a[mid]){
printf("\nThe number is found at %d", mid);
        return 0;
      }
      else if(x<a[mid]){
last=mid-1;
      }
      else
first=mid+1;
}

    printf("\nThe number is not in the list");

return 0;

}


I hope you this post is helpful for you. Thanks for reading.





Saturday, December 4, 2010

C program to find transpose of a matrix

Hello I want to discuss about the c program for transpose of the matrix.
Transpose matrix means interchange the rows with the columns and columns with the rows.

Program Code


#include < stdio.h >
#include < conio.h >
void main()
{
int arr[3][3],i,j;
clrscr();
printf("Enter elements for the array \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("Original array entered by the user is \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("\n Transpose of the array is \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
printf("%d ",arr[j][i]);
printf("\n");
}
getch();
}

Input:


Enter elements for the array
1
2
3
4
5
6
7
8
9

Output:


Original array entered by the user is
1 2 3
4 5 6
7 8 9

Transpose of the array is
1 4 7
2 5 8
3 6 9