Posts

Showing posts from December 15, 2019

Heap Sort

Image
Heap Sort   A heap is a nearly complete binary tree with the following two properties: Structural property: all levels are full, except possibly the last one, which is filled from left to right Order (heap) property: for any node x, Parent(x) ≥ x Array Representation of Heaps A heap can be stored as an array A. Root of tree is A[1] Left child of A[i] = A[2i] Right child of A[i] = A[2i + 1] Parent of A[i] = A[ i/2 ] Heapsize[A] ≤ length[A] The elements in the subarray A[( ⌊ n/2 ⌋ +1) .. n] are leaves Max-heaps (largest element at root), have the max-heap property:   for all nodes i, excluding the root:     A[PARENT(i)] ≥ A[i] Min-heaps (smallest element at root), have the min-heap property: for all nodes i, excluding the root:   A[PARENT(i)] ≤ A[i] Adding/Deleting Nodes New nodes are always inserted at the bottom level (left to right) and nodes are removed from the bottom level (right to left). Operations on Heaps Maintai

C Program For Heap Sort | C Programming

Image
Heap A heap is a complete tree with an ordering-relation R holding between each node and its descendant. Note that the complete tree here means tree can miss only rightmost part of the bottom level. R can be smaller-than, bigger-than. E.g. Heap with degree 2 and R is “bigger than”. Heap Sort  Build a heap from the given set (O(n)) time, then repeatedly remove the elements from the heap (O(n log n)). Implementation Heaps are implemented by using arrays. Insertion and deletion of an element takes O(log n) time. More on this later Heap Sort Pseudo Code Heapsort ( A ) {     BuildHeap ( A )     for i <- length (A) downto 2 {       exchange A [ 1 ] <-> A [i]       heapsize <- heapsize - 1       Heapify (A, 1 ) } BuildHeap (A)    {    heapsize <- length (A)     for i <- floor ( length/ 2 ) downto 1       Heapify (A, i)    }     Heapify (A, i)    {        le < - left (i)                    ri < - right (i)                             if (le

C Program For Caesar Cipher [Encryption & Decryption] | C Programming

Image
Caesar Cipher The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is named after Julius Caesar, who apparently used it to communicate with his generals. More complex encryption schemes such as the Vigenère cipher employ the Caesar cipher as one element of the encryption process. The widely known ROT13 'encryption' is simply a Caesar cipher with an offset of 13. The Caesar cipher offers essentially no communication security, and it will be shown that it can be easily broken even by hand. C Program For Caesar Cipher [Encryption] #include <stdio.h> int main () {     char msg [ 100 ], ch;     int i,key;     printf ( "Enter a plaintext \n " );     gets (msg);     printf ( "Enter key" )

C Program For Chi-Square Test | C Programming

Image
Chi-Square Test •         Formalizes this notion of distribution fit –        O i represents the number of observed data values in the i -th interval. –        p i is the probability of a data value falling in the i -th interval under the hypothesized distribution. –        So we would expect to observe E i = np i , if we have n observations So the chi-squared statistic is     •         So the hypotheses are –        H 0 : the random variable, X , conforms to the distributional assumption with parameters given by the parameter estimates. –        H 1 : the random variable does not conform. C Program For Chi-Square Test #include<stdio.h> int main() {      int n,i,e,calc=0,z=16.9;      printf("Total number : ");      scanf("%d",&n);      int arr[n],o[10]={0,0,0,0,0,0,0,0,0,0},oo[10],ooo[10];      printf("Enter %d number : ",n);      for(i=0;i<n;i++)      {          scanf("%d",&a

C Program To Sort The Words In Alphabetical Order | C Programming

Image
C Program To Sort The Words In Alphabetical Order #include <stdio.h> #include <string.h> int main () {     int i , j , n ;     char str [ 25 ][ 25 ], temp [ 25 ];     puts ( "How many strings you are going to enter?: " );     scanf ( " %d " , & n );     puts ( "Enter Strings one by one: " );     for ( i = 0 ; i <= n ; i ++)     {         gets ( str [ i ]);     }     for ( i = 0 ; i <= n ; i ++)         for ( j = i + 1 ; j <= n ; j ++)         {             if ( strcmp ( str [ i ], str [ j ]) > 0 )             {                 strcpy ( temp , str [ i ]);                 strcpy ( str [ i ], str [ j ]);                 strcpy ( str [ j ], temp );             }         }     printf ( "Order of Sorted Strings:" );     for ( i = 0 ; i <= n ; i ++)         puts ( str [ i ]);     return 0 ; } OUTPUT