Skip to main content

Posts

Showing posts from February, 2022

Binary Search

Binary Search The method is based on the Divide-Conquer algorithmic strategy. Precondition For Binary Search Data must be organized in a linear way. Data has to be in the sorted oder in either ascending or decending order. Advantages of Binary Serach Worst case time complexity is O(log2n), which is very efficient compared to linear search O(n). Disadvantages of Binary  Search Data has to be in sorted order. Complexity of Binary Search Best Case :-The key is found at the middle position after 1 comparision.                       -The best case time complexity is  Ω(1). Worst Case :-The Worst case time complexity is O(log2n). CODE 👇 #include <stdio.h> int main() {        int first, middle,last, n, i, search, a[100];        setbuf(stdout, NULL);        printf("ENTER THE SIZE OF ARRAY:");        scanf("%d", &n);        printf("ENTER %d ELEMENT IN ASCENDING ORDER:\n",n);        for(i=0; i<n; i++)             scanf("%d", &a[i]);      

Linear Search(Sequential Search)

  Linear Search It is also Called as "Sequential Search". This is simplest method. It can be applied to sequential storage structure like files, array, or linked list. Advantages of Linear Search It is very simple method. It does not require the data to be sorted. It does not require any additional data structure. Disadvanatges of Linear Search If  'n' is very large, this method is very in efficient and slow. It's worst case complexity O(n). Analysis of sequential search Best Case : -The best case  occurs when the key is found at the first position i.e at index 0.                       -The best case time complesxity  Ω (1).   Worst Case :-The case occurs when the key is not found in array.                         -The worst case time complexity O(n). Average Case :-Average comparisons = n(n+1)/2n = (n+1)/2.                              -The average case time complexity of this method is O(n). CODE 👇 #include <stdio.h> int linear_search(int a[10], int n, in

Radix Sort

Radix Sort: Radix sort algorithm requires the number of passes which  are equal to the number of digits present in the largest number among the list of numbers . For example, if the largest number is a 3 digit number then that list is sorted with 3 passes. Example: Original, unsorted list: 170, 45, 75, 90, 802, 24, 2, 66 Sorting by least significant digit (1s place) gives:  [*Notice that we keep 802 before 2, because 802 occurred  before 2 in the original list, and similarly for pairs  170 & 90 and 45 & 75.] 170, 90, 802, 2, 24, 45, 75, 66 Sorting by next digit (10s place) gives:   [*Notice that 802 again comes before 2 as 802 comes before  2 in the previous list.] 802, 2, 24, 45, 66, 170, 75, 90 Sorting by the most significant digit (100s place) gives: 2, 24, 45, 66, 75, 90, 170, 802 Advantages 1. Fast when the keys are short i.e. when the range of the array elements is less. 2. Used in suffix array constuction algorithms like Manber's algorithm and DC3 algorithm. Disadvan

Counting Sort

  Counting Sort Counting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array. Advantages of Counting Sort: Counting sort generally performs faster than all comparison-based sorting algorithms, such as merge sort and quicksort, if the range of input is of the order of the number of input. Counting sort is easy to code. Disadvantages of Counting Sort: Counting sort doesn’t work on decimal values. Counting sort is inefficient if the range of values to be sorted is very large. Complexity: Best :       O(n+k) Worst: O(n+k) Average: O(n+k) Space : O(max) Stability Yes CODE: #include <stdio.h> void  countingsort( int  a[],  int  k,  int  n); void  main() { int  n, i, k=0, a[15]; printf( "How many element in array:" ); scanf( "%d" , &n); printf( "\n Enter the element for array:" ); for( i=1; i<=n; i++) { scanf( "%d" ,  &a[i]);

Quick Sort

Quick Sort Quicksort is an in-place sorting algorithm which means it doesn't take an additional array to sort the data. It uses the same array to sort the elements. Advantages; The quick sort is regarded as the best sorting algorithm. It is able to deal well with a huge list of items. Because it sorts in place, no additional storage is required as well Disadvantages: The slight disadvantage of quick sort is that its worst-case performance is similar to average performances of the bubble, insertion or selections sorts. If the list is already sorted than bubble sort is much more efficient than quick sort If the sorting element is integers than radix sort is more efficient than quick sort. Complexity of the Quick Sort Algorithm; Data structure: Array Worst-case time complexity: O(n2) Average-case time complexity Θ(nlogn) Best-case time complexity:            Ω(nlogn) Worst-case space complexity: O(logn) CODE: #include <stdio.h> void  quicksort( int  a[20],  int  low,  in

Mergesort

 Mergesort Merge sort is a recursive algorithm based on Divide-and-Conquer. The algorithm divides the unsorted list into n sublists, recursively, until all sublists are of length one, which is considered already sorted. The algorithm compares and repeatedly merge sublists to produce sorted sublists by calling the merge function, this is done until the sorted sublists merge into one final sorted list. Merge sort is preferred over quick sort when there is a large dataset, and/or stored in external storage. Example:   The merge sort procedure recursively divides the unsorted list into sublists of equal halves in a top-down manner, until sublists of length one. As the list recursively divides, the original order of the list is preserved. In the case of odd length, the list is divided into sublists of near-equal size, using round functions.                Input List:     [7, 6, 5, 4, 3, 2, 1]                                            /               \                                [7, 6

Insertion Sort

Insertion Sort The basic idea of this method is to insert an unsorted element in it's correct position in a sorted set of element. Insertion sort is simple sorting algorithm that works similar to the way you sort playing cards in your hand. Advantages of Insertion Sort It is  a simple sorting method. No additional data structure is required. It is stable sorting method. Best case time complexity is  Ω  (n). It also exhibits good performance when deling with a small list. Disadvantages of Insertion Sort It does not perform as well as other, better sorting algorithm. The insertion sort does not deal well with a huge list. The insertion sort is particularly useful only when sorting a list of few items. worst case time complexity is O(n 2 ).      ⊚ Complexity of Insertion Sort Time Complexity Best Case:  Ω  (n) Woest Case: O(n 2 ) Space Comlplexity Worst Case: O(1) Stable: YES CODE 👇 #include<stdio.h> int comp_cnt; void main() {        int a[20], i, n;      void insertion(int a[

Bubble Sort

Bubble Sort Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.  Bubble sort with n element required n – 1 passes. Example: First Pass:  ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.  ( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap since 5 > 4  ( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap since 5 > 2  ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass:  ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )  ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2  ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )  ( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 )  Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. Third Pass:   ( 1 2 4 5 8 ) –>

ARRAY

 Array in C/ C++:- Array declaration by  initializing elements:-     // Array declaration by initializing elements     int arr[] = { 10, 20, 30, 40 }     // Compiler creates an array of size 4.     // above is same as "int arr[4] = {10, 20, 30, 40}" Advantages of an Array in C/C++:  Random access of elements using array index. Use of less line of code as it creates a single array of multiple elements. Easy access to all the elements. Traversal through the array becomes easy using a single loop. Sorting becomes easy as it can be accomplished by writing less line of code. Disadvantages of an Array in C/C++:  Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation. Accessing Array Elements:  Array elements are accessed by using an integer index. Array inde