Posts

Showing posts from September, 2015

TRANSPOSE OF A MATRIX

TRANSPOSE OF A MATRIX: A=[2 3 4] A'=[2 1] [1 2 3] [3 2] [4 3] C SOURCE CODE : #include<stdio.h> #include<conio.h> void main() { int m,n,i,j,a[20][20],b[20][20]; printf("\n Enter the no. of rows in matrix:"); scanf("%d",&m); printf("\n Enter the no. of columns in matrix:"); scanf("%d",&n); printf("\n Enter the matrix:"); for(i=0;i<m;i++) {    for(j=0;j<n;j++)       scanf("%d",&a[i][j]); } printf("\n MATRIX IS:\n"); for(i=0;i<m;++i) {    for(j=0;j<n;j++)       printf("%5d",a[i][j]);    printf("\n"); } } for(i=0;i<m;i++) {    for(j=0;j<n;j++)       a[i][j]=b[j][i]; } printf("\n TRANSPOSE OF MATRIX :\n"); for(i=0;i<n;++i) {    for(j=0;j<m;j++)       printf("%5d",a[i][j]);    printf("\n"); } getch(); }

BUBBLE SORT

BUBBLE SORT: It is the simpler and well known sorting technique. It is less efficient for large amount of data as its average and worst case complexity is high. Bubble sort is  stable  and  adaptive .In this sorting technique, we need to compare and swap the adjacent elements of the array till we get whole array sorted.i Worst case:O(n 2 ) Average case :O(n 2 ) Best case:O(n) e.g. To sort  8,5,2,1 8 , 5 , 2 , 1               8>5, swap(8,5) 5 , 8 , 2 , 1               8>2, swap(8,2) 5 , 2 , 8 , 1               8>1, swap(8,1) 5 , 2 , 1 , 8 5>2, swap(5,2) 2 , 5 , 1 , 8 5>1, swap(5,1) 2 , 1 , 5 , 8 2>1, swap(2,1) 1 , 2 , 5 , 8 SORTED ARRAY ALGORITHM: Input the array, a[] while (i<=n-1) while(j<(n-j-1) Compare a[j] with a[j+1] and swap as per requirement Increment j [END OF INNER LOOP] Increment i [END OF OUTER LOOP] C SOURCE CODE : #include<stdio.h> #include<conio.

SELECTION SORT

SELECTION SORT The idea of selection sort is rather simple. In this we repeatedly find the largest and smallest value in  array and place it at its final position as required for ascending or descending order.It is less efficient than insertion sort and has n 2  complexity for all three cases.It is also not  much efficient for larger amount of data. Worst Case:O(n 2 ) Average Case:O(n 2 ) Best Case:O(n 2) ALGORITHM: Input array as a[] for i=0 to i<n-1 for j=i+1 to n find the smallest in between j to (n-1)th index and compare and swap it with i th index. Print the sorted array e.g , To sort  5,3,2,0  5 , 3 , 2 , 0 i=0, min=0, swap(5,0) 0 , 3 , 2 , 5 i=1, min=2, swap(3,2) 0 , 2 , 3 , 5 i=2, do nothing 0 , 2 , 3 , 5 SORTED ARRAY C SOURCE CODE : #include<stdio.h> #include<conio.h>   void main() { int a[100], n, i, j, temp;   printf("Enter number of elements\n"); scanf("%d", &

INSERTION SORT

INSERTION SORT: In this sorting technique, as the name suggests, insert each element to its proper location. It is simple to implement and efficent on small data set. It is stable, in-place(only requires const amount of extra memory space O(1)) and an online algorithm(i.e. it can sort a list as it recieves). Worst case:О( n 2 ) Average Case:О( n 2 ) Best case:Ω( n ) ALGORITHM: Input Array as a[] for i = 1 to n – 1 j = i while j > 0 and a[j-1] > a[j] swap a[j] and a[j-1] j = j – 1 end while end for loop PRINT sorted array as a[] e.g. sort 6,4 2,5 6 , 4 , 2 , 5 4 to be inserted ? , 6 , 2 , 5 j=0, reached boundary 4 , 6 , 2 , 5 4 inserted 4 , 6 , 2 , 5 2 to be inserted 4 , ? , 6 , 5 4>2 shift ? , 4 , 6 , 5 j=0, reached boundary 2 , 4 , 6 , 5 2 inserted 2 , 4 , 6 , 5 5 to be inserted 2 , 4 , ? , 6 6>5 shift 2 , 4 , 5 , 6 4<5, 5 inserted C SOURCE CODE : #inc

PATTERN:

A B  C D  E  F G  H  I  J C SOURCE CODE: #include<stdio.h> #include<conio.h> void main() { int n, i, j,c=65;   printf("  Enter the number of rows\n"); scanf("%d", &n);   for (i = 1; i<= n; i++) { for (j= 1; j<= i; j++) { printf("%3c",c); c++; } printf("\n"); } getch(); }

Basic C++ Programs

Image

How to use comparator in java?

import java . util . * ; class ComparatorTest { public static void main ( String [ ] args ) { List list = new ArrayList < > ( ) ; list . add ( new Employee ( "B" , 3 ) ) ; list . add ( new Employee ( "A" , 1 ) ) ; list . add ( new Employee ( "C" , 2 ) ) ; Collections . sort ( list , new EmployeeComparator ( ) ) ; for ( Employee emp : list ) { System . out . println ( emp . name + "\t" + emp . age ) ; } } } class EmployeeComparator implements Comparator < Employee > { @Override public int compare ( Employee o1 , Employee o2 ) { if ( o1 . age > o2 . age ) { return - 1 ; } else { return 1 ; } } } class Employee { String name ; int age ; public Employee ( String name , int age ) { this . name = name ;