Skip to main content

Posts

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() {    ...

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 a...

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 eleme...