Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (2024)

Ali ELJALAOUI

Embedded Software Engineer

  • Report this post

๐Ÿ”— **Tutorial: Bit Manipulation in C**Are you intrigued by the art of bit manipulation in C? Let's embark on a journey to understand the fundamentals of tinkering with individual bits within variables. This knowledge can be invaluable for low-level operations, data compression, and creating masks for various tasks.**1. Bitwise Logical Operations:**Bitwise operators like AND (`&`), OR (`|`), XOR (`^`), and NOT (`~`) allow us to perform operations on specific bits within operands.Example:```cunsigned int a = 5;// 0101 in binaryunsigned int b = 3;// 0011 in binaryunsigned int result_and = a & b;// 0001 (bitwise AND)unsigned int result_or = a | b;// 0111 (bitwise OR)unsigned int result_xor = a ^ b;// 0110 (bitwise XOR)```**2. Bit Shifting:**Shifting operations (`<<` and `>>`) enable us to move bits left or right within a variable.Example:```cunsigned int value = 10;// 1010 in binaryunsigned int shifted_left = value << 2;// 101000 in binary (shifted left by 2)unsigned int shifted_right = value >> 1; // 0101 in binary (shifted right by 1)```**3. Manipulating Specific Bits:**Crafting masks and applying them to specific bits can be a game-changer. For instance, consider `(1 << 2)` as a mask to manipulate the 3rd bit.Example (Clearing the 3rd bit):```cunsigned int value = 12;// 1100 in binaryunsigned int mask = ~(1 << 2);// Creates a mask for the 3rd bit (0010)unsigned int result = value & mask;// Clears the 3rd bit (result: 1000, decimal 8)```Remember these core principles when delving into bit manipulation:- Understand sign behavior during shifts.- Master proper masking and unmasking techniques.- Consider endianness and its implications.- Utilize standardized types (`<stdint.h>`) for consistency.- Strike a balance between optimization and readability.- Ensure code portability and safety across platforms.Whether you're working in C or C++, these principles remain steadfast. Cultivate your curiosity and explore the universe of bit manipulationโ€”it's a fundamental tool for efficient programming!#CProgramming #BitManipulation #CodingTips #LinkedInLearning---Feel free to tailor the post according to your preferences and audience. Happy learning and coding!

  • Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (2)

1

Like Comment

To view or add a comment, sign in

More Relevant Posts

  • Aman Pandey

    Coder | Programmer | Student (2nd yr)C++, JAVA, PHP| DSA, OPPS | Learning Web Development

    • Report this post

    Hello ๐Ÿ‘‹ connections, it's day no. 26 โœ… of 100daysDsaChallenge#100daysofleetcode and here is the problem that I solved today๐Ÿ˜ŠProblem:Valid ParenthesesProblem link: https://bit.ly/34kxPaqIntution:Here we used a stack data structure to check if a given string of parentheses is valid or not. The basic idea is to iterate through the string, and every time an opening parenthesis is encountered, it is pushed onto the stack. When a closing parenthesis is encountered, the code checks if there is a corresponding opening parenthesis at the top of the stack. If there is, it means that the parentheses are properly nested, and the opening parenthesis is popped from the stack. If not, or if there is no opening parenthesis in the stack when a closing parenthesis is found, the string is considered invalid.Here's a step-by-step breakdown:Initialize an empty stack to keep track of opening parentheses.Iterate through each character in the input string.If the character is an opening parenthesis ('(', '{', '['), push it onto the stack.If the character is a closing parenthesis (')', '}', ']'), check if the stack is not empty. If it's not empty, pop the top element from the stack and check if it matches the corresponding opening parenthesis for the current closing parenthesis. If it matches, continue; otherwise, return false.If the stack is empty after processing all characters, return true; otherwise, return false.Algorithmic Approach:The algorithm uses a stack to keep track of the opening parentheses, ensuring that each closing parenthesis has a corresponding opening parenthesis. It efficiently handles different types of parentheses and returns true if the parentheses are valid; otherwise, it returns false.Time Complexity:The time complexity of this code is O(n), where n is the length of the input string. This is because it iterates through each character in the string exactly once, and each operation inside the loop takes constant time.Space Complexity:The space complexity is O(n), where n is the length of the input string. In the worst case, the stack can have all the opening parentheses from the string.

    4

    Like Comment

    To view or add a comment, sign in

  • Mohamed Tarek

    Embedded Software Engineer @ SEITech Solutions

    • Report this post

    Different Types of pointers===============wild pointerNull pointervoid pointerdangling pointer1.wild pointer:pointer that is unintialized is known as a wild pointer . performing any deferencing operation orpointer arithmetic will result in undefined behaviour.#include<stdio.h>int main(){int a=10;int *ptr;//wild pointerprintf("%d\n",*ptr);//undefined behaviour or value}2.Null pointer==========pointer that points to NULL is known as a NULL pointer ..performing any dereferncing or pointer arithmetic results in segmentation fault.basically NULL is a macro whose value is 0 defined already ..where ever NULL is written at the preprocessing stage it will be replaced with 0.#include<stdio.h>int main(){int a=10;int *ptr=NULL;//int *ptr=0;//NULL pointerprintf("%d\n",*ptr);//segmentation fault}Dangling pointer=============pointer pointing to a freed memory location or an unused memory location is termed as a dangling pointer.performing any pointer dereferncing or pointer arithmetic results in the existing value or undefined behaviour#include<stdio.h>int* fun();intmain(){int *ptr=fun();//dangling pointerprintf("%d\n",*ptr);//undefined behaviour}int* fun(){int a=10;int *p=&a;return p;}void pointer=========void pointer is also known as a generic pointer that can point to any type of datatype.but whenever we are performing any operations on void pointer we need to do the type convertion or type casting.type casting is nothing but changing the data from one type to another type.syntax===void *pointer_name;#include<stdio.h>int main(){int a=10;void *vptr=&a;printf("%d\n",*(int *)vptr);char ch='m';vptr=&ch;printf("%c\n",*(char *)vptr);float f=9.8;vptr=&f;printf("%f\n",*(float *)vptr);double d=789.09;vptr=&d;printf("%lf\n",*(double *)vptr);}

    Like Comment

    To view or add a comment, sign in

  • purnendu kumar

    Senior Technical leader

    • Report this post

    Different Types of pointers===============wild pointerNull pointervoid pointerdangling pointer1.wild pointer:pointer that is unintialized is known as a wild pointer . performing any deferencing operation orpointer arithmetic will result in undefined behaviour.#include<stdio.h>int main(){int a=10;int *ptr;//wild pointerprintf("%d\n",*ptr);//undefined behaviour or value}2.Null pointer==========pointer that points to NULL is known as a NULL pointer ..performing any dereferncing or pointer arithmetic results in segmentation fault.basically NULL is a macro whose value is 0 defined already ..where ever NULL is written at the preprocessing stage it will be replaced with 0.#include<stdio.h>int main(){int a=10;int *ptr=NULL;//int *ptr=0;//NULL pointerprintf("%d\n",*ptr);//segmentation fault}Dangling pointer=============pointer pointing to a freed memory location or an unused memory location is termed as a dangling pointer.performing any pointer dereferncing or pointer arithmetic results in the existing value or undefined behaviour#include<stdio.h>int* fun();intmain(){int *ptr=fun();//dangling pointerprintf("%d\n",*ptr);//undefined behaviour}int* fun(){int a=10;int *p=&a;return p;}void pointer=========void pointer is also known as a generic pointer that can point to any type of datatype.but whenever we are performing any operations on void pointer we need to do the type convertion or type casting.type casting is nothing but changing the data from one type to another type.syntax===void *pointer_name;#include<stdio.h>int main(){int a=10;void *vptr=&a;printf("%d\n",*(int *)vptr);char ch='m';vptr=&ch;printf("%c\n",*(char *)vptr);float f=9.8;vptr=&f;printf("%f\n",*(float *)vptr);double d=789.09;vptr=&d;printf("%lf\n",*(double *)vptr);}

    266

    15 Comments

    Like Comment

    To view or add a comment, sign in

  • Chandan Agrawal

    SDE @Microsoft | Ex-Infosys | 130K+ on LinkedIn | 2000+ rated on LeetCode | Code, Content and Memes

    • Report this post

    ๐๐ข๐ญ ๐Œ๐š๐ง๐ข๐ฉ๐ฎ๐ฅ๐š๐ญ๐ข๐จ๐ง ๐ข๐ง ๐ƒ๐’๐€, is very important and sometimes people fears as well ( including me ) so here are essential tips and tricks to unravel the magic of bit manipulation, which will definately gonna help you in Data Structures and Algorithms. ๐Ÿ’ป๐Ÿ“Œ ๐‚๐ก๐ž๐œ๐ค๐ข๐ง๐  ๐ข๐Ÿ ๐๐ฎ๐ฆ๐›๐ž๐ซ ๐€ ๐ข๐ฌ ๐„๐ฏ๐ž๐ง/๐Ž๐๐- If `A & 1` equals 0, then A is even.- If `A & 1` equals 1, then A is odd.๐Ÿ“Œ ๐Œ๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ฒ (๐‹๐ž๐Ÿ๐ญ ๐’๐ก๐ข๐Ÿ๐ญ) ๐€ ๐›๐ฒ ๐ฉ๐จ๐ฐ๐ž๐ซ๐ฌ ๐จ๐Ÿ ๐Ÿ- A = `A << k`.๐Ÿ“Œ๐ƒ๐ข๐ฏ๐ข๐๐ž (๐‘๐ข๐ ๐ก๐ญ ๐’๐ก๐ข๐Ÿ๐ญ) ๐€ ๐›๐ฒ ๐ฉ๐จ๐ฐ๐ž๐ซ๐ฌ ๐จ๐Ÿ ๐Ÿ- A = `A >> k`.๐Ÿ“Œ ๐’๐ž๐ญ๐ญ๐ข๐ง๐  ๐š ๐ฃ'๐ญ๐ก ๐๐ข๐ญ- A = A | (1 << j).๐Ÿ“Œ ๐”๐ง๐ฌ๐ž๐ญ๐ญ๐ข๐ง๐  ๐š ๐ฃ'๐ญ๐ก ๐๐ข๐ญ- A = A & (~(1 << j)).๐Ÿ“Œ ๐“๐จ๐ ๐ ๐ฅ๐ข๐ง๐ (๐…๐ฅ๐ข๐ฉ) ๐ฃ'๐ญ๐ก ๐๐ข๐ญ- A = A ^ (1 << j).๐Ÿ“Œ ๐‚๐ก๐ž๐œ๐ค๐ข๐ง๐  ๐ข๐Ÿ ๐ฃ-๐ญ๐ก ๐๐ข๐ญ ๐ข๐ฌ ๐’๐ž๐ญ ๐ข๐ง ๐€- If `(A & (1 << j)) > 0`, then the j-th bit in A is set.๐Ÿ“Œ ๐‚๐ก๐ž๐œ๐ค๐ข๐ง๐  ๐ข๐Ÿ ๐ฃ-๐ญ๐ก ๐๐ข๐ญ ๐ข๐ฌ ๐”๐ง๐ฌ๐ž๐ญ ๐ข๐ง ๐€- If `(A & (1 << j)) == 0`, then the j-th bit in A is unset.๐Ÿ“Œ ๐ˆ๐ง๐ฏ๐ž๐ซ๐ญ๐ข๐ง๐  ๐š๐ฅ๐ฅ ๐๐ข๐ญ๐ฌ ๐ข๐ง ๐€- (A = ~A).๐Ÿ“Œ ๐•๐š๐ฅ๐ฎ๐ž ๐จ๐Ÿ ๐‹๐ž๐š๐ฌ๐ญ ๐’๐ข๐ ๐ง๐ข๐Ÿ๐ข๐œ๐š๐ง๐ญ ๐๐ข๐ญ ๐ญ๐ก๐š๐ญ ๐ข๐ฌ ๐Ž๐ง ( ๐Ÿ๐ข๐ซ๐ฌ๐ญ ๐Ÿ๐ซ๐จ๐ฆ ๐ซ๐ข๐ ๐ก๐ญ)- T = (A & (-A))๐Ÿ“Œ ๐•๐š๐ฅ๐ฎ๐ž ๐จ๐Ÿ ๐‹๐ž๐š๐ฌ๐ญ ๐’๐ข๐ ๐ง๐ข๐Ÿ๐ข๐œ๐š๐ง๐ญ ๐๐ข๐ญ ๐ญ๐ก๐š๐ญ ๐ข๐ฌ ๐Ž๐Ÿ๐Ÿ ( ๐Ÿ๐ข๐ซ๐ฌ๐ญ ๐Ÿ๐ซ๐จ๐ฆ ๐ซ๐ข๐ ๐ก๐ญ)- T = (~A & (A + 1)).๐Ÿ“Œ๐“๐ฎ๐ซ๐ง ๐Ž๐ง ๐€๐ฅ๐ฅ ๐๐ข๐ญ๐ฌ ๐ข๐ง ๐š ๐’๐ž๐ญ ๐จ๐Ÿ ๐’๐ข๐ณ๐ž ๐ง- A = (1 << n) - 1๐Ÿ“Œ ๐ˆ๐ญ๐ž๐ซ๐š๐ญ๐ž ๐“๐ก๐ซ๐จ๐ฎ๐ ๐ก ๐€๐ฅ๐ฅ ๐’๐ฎ๐›๐ฌ๐ž๐ญ๐ฌ ๐จ๐Ÿ ๐š ๐’๐ž๐ญ ๐จ๐Ÿ ๐’๐ข๐ณ๐ž ๐ง- for ( x = 0; x < (1 << n); ++x )Share your interesting tricks for bit manipulation; I'd love to learn, and others will benefit too. Let's uncover the magic together! ๐Ÿ’ปโœจFollow Chandan Agrawal for more ๐Ÿ˜Š๐Ÿ˜Š#meme #dsa #interview #coding #fun #weekend #leetcode #bit #lc #gfg #cp #maang #google #amazon

    • Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (15)

    603

    7 Comments

    Like Comment

    To view or add a comment, sign in

  • Arijit Ghosh

    LinkedIn Content Creator || Discord moderator @scaler || DSA @python || Full stack Web Developer(LAMP)

    • Report this post

    ๐Ÿฎ๐Ÿฑ ๐—บ๐—ผ๐˜€๐˜ ๐—ถ๐—บ๐—ฝ๐—ผ๐—ฟ๐˜๐—ฎ๐—ป๐˜ ๐—ฎ๐—น๐—ด๐—ผ๐—ฟ๐—ถ๐˜๐—ต๐—บ๐˜€ ๐˜†๐—ผ๐˜‚ ๐—บ๐˜‚๐˜€๐˜ ๐—ป๐—ผ๐˜ ๐—ฎ๐˜ƒ๐—ผ๐—ถ๐—ฑ1. ๐™Ž๐™ค๐™ง๐™ฉ๐™ž๐™ฃ๐™œ ๐˜ผ๐™ก๐™œ๐™ค๐™ง๐™ž๐™ฉ๐™๐™ข๐™จ ๐Ÿช„:๐Ÿ”น Explore various techniques to arrange data in specific orders, including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and QuickSort2. ๐™Ž๐™š๐™–๐™ง๐™˜๐™๐™ž๐™ฃ๐™œ ๐˜ผ๐™ก๐™œ๐™ค๐™ง๐™ž๐™ฉ๐™๐™ข๐™จ ๐Ÿ”: ๐Ÿ”น Master methods to efficiently locate specific elements within data collections, such as Linear Search and Binary Search.3. ๐™‡๐™ž๐™ฃ๐™ ๐™š๐™™ ๐™‡๐™ž๐™จ๐™ฉ๐™จโ›“๏ธ:๐Ÿ”น Build a foundation in non-contiguous memory allocation by understanding linked list structures with operations like insertion, deletion, and reversal within linked lists.4. ๐™Ž๐™ฉ๐™–๐™˜๐™ ๐™จ ๐™–๐™ฃ๐™™ ๐™Œ๐™ช๐™š๐™ช๐™š๐™จ๐Ÿ›ข๏ธ:๐Ÿ”น Master stacks (pancake towers) and queues (orderly lines) built with arrays or linked lists โœจ5. ๐™๐™ง๐™š๐™š๐™จ ๐ŸŒด:๐Ÿ”น Explore hierarchical data structures with a focus on binary trees.Know about traversal techniques (inorder, preorder, postorder) to navigate and process tree data.6. ๐™‚๐™ง๐™–๐™ฅ๐™๐™จ ๐Ÿ“Š:๐Ÿ”น Represent connections and relationships between entities using graphs. Implement graph traversal algorithms like Depth-First Search (DFS) and Breadth-First Search (BFS) for various applications.7. ๐™๐™š๐™˜๐™ช๐™ง๐™จ๐™ž๐™ค๐™ฃ ๐Ÿ”„:๐Ÿ”น Understand the concept of self-referential functions and apply it to problems like factorial calculation and Fibonacci series generation.8. ๐˜ฟ๐™ฎ๐™ฃ๐™–๐™ข๐™ž๐™˜ ๐™‹๐™ง๐™ค๐™œ๐™ง๐™–๐™ข๐™ข๐™ž๐™ฃ๐™œ ๐Ÿ”ข:๐Ÿ”น Break down complex problems into overlapping subproblems for efficient solutions. Explore examples like Fibonacci calculation using dynamic programming and the 0/1 Knapsack problem.9. ๐™ƒ๐™–๐™จ๐™๐™ž๐™ฃ๐™œ ๐Ÿ”’:๐Ÿ”น Implement hash tables for efficient storage and retrieval of data using key-value pairs. Practice hash table operations, including insertion, deletion, and search.10. ๐™ƒ๐™š๐™–๐™ฅ๐™จ โ›ฐ๏ธ:๐Ÿ”น Build a specialized tree-based data structure that maintains the heap property (parent nodes always greater/smaller than children). Learn Heapify and Heap Sort algorithms for efficient sorting.11. ๐˜ฝ๐™ž๐™ฉ ๐™ˆ๐™–๐™ฃ๐™ž๐™ฅ๐™ช๐™ก๐™–๐™ฉ๐™ž๐™ค๐™ฃ โš™๏ธ:๐Ÿ”น Manipulate individual bits within data using bitwise operators (AND, OR, XOR, Shifts). Apply bit manipulation techniques for tasks like data compression and encryption.----------------------------------------------------------------FollowArijit Ghoshfor more insightful shares ๐Ÿ“š1๏ธโƒฃTelegram link to join ongoing activity and access many other coding resourceshttps://lnkd.in/dy2Hkh2m2๏ธโƒฃ Like and share with your friends ๐Ÿ”„_______________________________________#dsa #algorithms #datastructuresandalgorithms #internship #hiring #graphs #sorting #stacks

    • Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (20)

    68

    11 Comments

    Like Comment

    To view or add a comment, sign in

  • Jeffrey Dean Kelly

    President

    • Report this post

    Here's an interesting, useful and novel update to our brute-force variable subset selection (BFVSS) technique!Recently, we showed how a simple brute-force approach to perform parallelized and sparse data regression (PSDREG) via IMPL-DATA(c)'s variable subset selection was effective at finding multiple linear regression (MLR) models to the Dow Data Challenge Problem (DDCP) better than previously reported in the following papers:1. Braun et. al., IFAC, (2020),2. Qin et. al., CACE, (2021) and3. Barton & Lennox, Dig. Chem. Eng., (2022).As previously mentioned, the DDCP is a 44 x 1 MISO problem and supplies both the raw training and testing data.The past studies above employed several different and well-known machine learning methods where the best Q2 found was 0.698 (i.e., R2 for the testing data). In our previous two posts, we found a Q2 = 0.716 with eight (8) regressors using a mathematical programming VSS approach and Q2 = 0.718 with five (5) predictors using our BFVSS with N = 1000 number of shuffles, selections, samples, scenarios or situations varying the number of explanatory variables from 1 to 12 in parallel with the same random seed.However, here we apply something different using IMPL-DATA's semantic of "coefficient-setups" which includes or excludes an independent variable's regression coefficient i.e., zero (0) to exclude and one (1) to include. In our last post, we highlighted the Green (1977) algorithm to perform a "k" out of "m" shuffle or select which exogenously determines the coefficient-setups.Each coefficient-setup sample (n = 1..N) corresponds to a single MLR run. Below we detail two sets of Q2 results when N = 1000 and k = 2..13 (M = 44) where all runs use the same random seed.k= 2: Q2=0.625 -> Q2=0.641k= 3: Q2=0.669 -> Q2=0.693k= 4: Q2=0.687 -> Q2=0.711k= 5: Q2=0.682 -> Q2=0.723*k= 6: Q2=0.691 -> Q2=0.709k= 7: Q2=0.692 -> Q2=0.703k= 8: Q2=0.723 -> Q2=0.719*k= 9: Q2=0.701 -> Q2=0.723k=10: Q2=0.681 -> Q2=0.720k=11: Q2=0.699 -> Q2=0.705k=12: Q2=0.683 -> Q2=0.693k=13: Q2=0.689 -> Q2=0.695Now here's the difference, when we run the first set of Q2's, we fit or estimate a MLR by regressing the zero-one coefficient-setups w.r.t. to the mean squared error (MSE) of the testing data.By sorting their respective coefficients from negative to positive or smallest to largest, we arbitrarily record the top 22 (say 44 / 2 or any other subset) and only include the top subset when generating the coefficient-setups for the next set of samples for a particular k.As the results show for the second Q2's after "->" above, these Q2's are improved except for k = 8 and as the number of k's increase the Q2 values rise then fall where for k = 4..11, they are all better than the best Q2 = 0.698 found by others ;-)#industrial #algorithms #nonlinear #dynamic #optimization #control #industrialai #machinelearning

    7

    2 Comments

    Like Comment

    To view or add a comment, sign in

  • Sanyam Goyal

    SDE 2 @ Ecom Express | Logistics | Fintech, Ex: Stashfin | IIIT Delhi

    • Report this post

    ๐Ÿ” Harnessing Hashing: Unlocking Key-Value Pairs' Secrets๐Ÿ—๏ธHash Tables: A Powerful Data StructureHash tables are a fundamental data structure that is used in a wide variety of applications. They are a type of associative array that maps keys to values. Hash tables are efficient for searching, inserting, and deleting elements.๐ŸŽฏWhat are Hash Tables?A hash table is a data structure that uses a hash function to map keys to values. The hash function takes a key and produces a hash value, which is an integer. The hash value is used to determine the location of the key in the hash table.โ™ป How are Hash Tables Used?Hash tables are used in a wide variety of applications, including:Symbol tables:Hash tables can be used to implement symbol tables,which are data structures that map names to values.๐Ÿ—ณ Caches:Hash tables can be used to implement caches,which are data structures that store frequently accessed data.Databases:Hash tables can be used to implement databases,which are data structures that store data in a structured format.๐Ÿ”€Collision Resolution: Collisions occur when two different keys have the same hash value. There are a number of different collision resolution techniques that can be used to handle collisions. Some of the most common techniques include:โ–ถ Separate chaining:Separate chaining uses a linked list to store the values that have the same hash value.โ–ถ Linear probing:Linear probing probes the next available slot in the hash table until an empty slot is found.โ–ถ Quadratic probing:Quadratic probing probes the hash table using a quadratic formula.๐Ÿ“ˆAverage Time Complexity:The average time complexity for searching, inserting, and deleting elements in a hash table is O(1). This means that the time it takes to perform these operations is independent of the size of the hash table.ConclusionHash tables are a powerful and versatile data structure that is used in a wide variety of applications. They are efficient for searching, inserting, and deleting elements. If you are not already familiar with hash tables, I encourage you to learn more about them.for more details please check out the: link: https://lnkd.in/dCZut4vY#hashtables #algorithms

    2

    Like Comment

    To view or add a comment, sign in

  • Anshul Dohare

    Software Developer | Java | C++ | Android Application Development

    • Report this post

    ๐Ÿ” Exploring the Basics: ๐Œ๐ž๐ซ๐ ๐ž ๐’๐จ๐ซ๐ญ ๐Ÿ”Hello, LinkedIn friends!๐Ÿ‘‹ Let's have a look at a reliable and efficient sorting algorithm : Selection Sort!๐Ÿ—ฃ๏ธ๐Œ๐ž๐ซ๐ ๐ž ๐’๐จ๐ซ๐ญ is a popular and efficient sorting algorithm that follows the divide and conquer strategy. It divides the unsorted list into n sub-lists, each containing one element, and then repeatedly merges these sub-lists to produce new sorted sub-lists until there is only one sub-list remaining, which is the sorted list.๐ŸŒ๐Ÿ’ปWithout going into coding, here's a quick look:๐Ÿ› ๏ธ ๐‡๐จ๐ฐ ๐ข๐ญ ๐–๐จ๐ซ๐ค๐ฌ:-> Divide : Divide the unsorted list into sub-lists, each containing one element.-> Conquer : Repeatedly merge sub-lists to produce new sorted sub-lists.-> Combine : Continue merging sub-lists until there is only one sub-list remaining, which is the sorted list.๐ŸŽฏ ๐–๐ก๐ฒ ๐Œ๐ž๐ซ๐ ๐ž ๐’๐จ๐ซ๐ญ?-> Merge Sort is a reliable choice for general purpose sorting and a strong algorithm with a variety of uses. Merge Sort can help you with sorting large datasets, external sorting problems, and consistent performance all around. ๐Ÿš€โฐ ๐“๐ข๐ฆ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ:-> Best case : O(nlog n)-> Average case : O(nlog n)-> Worst case : O(nlog n)๐Ÿ† ๐๐ซ๐จ๐ฌ:-> ๐ŸŒ๐˜š๐˜ต๐˜ข๐˜ฃ๐˜ญ๐˜ฆ ๐˜š๐˜ฐ๐˜ณ๐˜ต๐˜ช๐˜ฏ๐˜จ : Maintains the relative order of equal elements in the sorted output.-> โณ๐˜—๐˜ณ๐˜ฆ๐˜ฅ๐˜ช๐˜ค๐˜ต๐˜ข๐˜ฃ๐˜ญ๐˜ฆ ๐˜—๐˜ฆ๐˜ณ๐˜ง๐˜ฐ๐˜ณ๐˜ฎ๐˜ข๐˜ฏ๐˜ค๐˜ฆ : It has a consistent and predictable performance of O(n log n) in the worst, average, and best cases, making it efficient for large data sets.-> ๐Ÿ’ช๐˜Œ๐˜น๐˜ต๐˜ฆ๐˜ณ๐˜ฏ๐˜ข๐˜ญ ๐˜š๐˜ฐ๐˜ณ๐˜ต๐˜ช๐˜ฏ๐˜จ : Ideal for situations where data exceeds main memory capacity.-> ๐Ÿ“€๐˜—๐˜ข๐˜ณ๐˜ข๐˜ญ๐˜ญ๐˜ฆ๐˜ญ๐˜ช๐˜ป๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ : The divide and conquer nature of Merge Sort allows for easy parallelization, making it suitable for parallel processing and multi-core systems.๐Ÿšซ ๐‚๐จ๐ง๐ฌ:-> ๐Ÿ“ˆ๐˜š๐˜ฑ๐˜ข๐˜ค๐˜ฆ ๐˜Š๐˜ฐ๐˜ฎ๐˜ฑ๐˜ญ๐˜ฆ๐˜น๐˜ช๐˜ต๐˜บ : The space complexity of Merge sort is O(n) because it requires additional space proportional to the size of the input array for the temporary storage of merged sub-lists. This can be a concern for large data sets with limited memory.-> ๐Ÿ”„๐˜•๐˜ฐ๐˜ต ๐˜ˆ๐˜ฅ๐˜ข๐˜ฑ๐˜ต๐˜ช๐˜ท๐˜ฆ : Even if the list is sorted, the merge sort goes through the entire process.-> ๐Ÿ—‚๏ธ๐˜•๐˜ฐ๐˜ต ๐˜ช๐˜ฏ ๐˜—๐˜ญ๐˜ข๐˜ค๐˜ฆ : Merge sort is not in-place sorting algorithm, as it needs additional space for the merging process. This can be a drawback in situations where memory usage is a critical factor.-> ๐Ÿข๐˜•๐˜ฐ๐˜ต ๐˜ฑ๐˜ณ๐˜ฆ๐˜ง๐˜ฆ๐˜ณ๐˜ข๐˜ฃ๐˜ญ๐˜ฆ ๐˜ง๐˜ฐ๐˜ณ ๐˜ด๐˜ฎ๐˜ข๐˜ญ๐˜ญ ๐˜ฅ๐˜ข๐˜ต๐˜ข๐˜ด๐˜ฆ๐˜ต : While the time complexity is efficient at O(nlog n), the constant factors involved can make it slower for small datasets compared to simpler algorithms like Quick Sort and Insertion Sort. Follow Anshul Dohare for more such a knowledgeable content.#mergesort #dsa #datastructuresandalgorithms #algorithms #jobs #sorting #keepgrowing #computerscience #engineering #engineer #computersciencestudents #softwareengineer #programming #development #softwaredevelopment #learning #student #internships #success

    11

    Like Comment

    To view or add a comment, sign in

  • Suresh Chelani

    CP enthusiast || 3โญ @LEETCODE || Institute Rank ๐ŸŽ–๏ธ1st @GFG || Former Technical J. Secretary - Student Council - ITJ, ICFAI University Jaipur

    • Report this post

    ๐Ÿ”๐‰๐ฎ๐ฌ๐ญ ๐’๐จ๐ฅ๐ฏ๐ž๐ ๐š ๐‚๐จ๐๐ข๐ง๐  ๐‚๐ก๐š๐ฅ๐ฅ๐ž๐ง๐ ๐ž! ๐Ÿ” (With 4 Approaches)๐Ÿงฉ ๐๐ซ๐จ๐›๐ฅ๐ž๐ฆ: "Find the Duplicate Number" (Medium)๐Ÿ“š ๐“๐จ๐ฉ๐ข๐œ๐ฌ: Algorithms, Arrays๐Ÿข ๐‚๐จ๐ฆ๐ฉ๐š๐ง๐ข๐ž๐ฌ: Various๐Ÿ“ ๐ƒ๐ž๐ฌ๐œ๐ซ๐ข๐ฉ๐ญ๐ข๐จ๐ง:You are given an array of integers nums containing n + 1 integers, where each integer is in the range [1, n] inclusive. There's only one repeated number in nums, and your task is to find and return this repeated number.Here's the catch: you must solve the problem without modifying the nums array, and you can only use constant extra space.๐€๐ฉ๐ฉ๐ซ๐จ๐š๐œ๐ก ๐Ÿ:- ๐“๐ข๐ฆ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(n log n)- ๐’๐ฉ๐š๐œ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(1)This approach employs sorting to solve the problem. It sorts the given array of integers and then checks for consecutive duplicates. If found, it returns the duplicate number. While this solution works, it has a time complexity of O(n log n) due to sorting, which may not be optimal for larger input sizes.---๐€๐ฉ๐ฉ๐ซ๐จ๐š๐œ๐ก ๐Ÿ:- ๐“๐ข๐ฆ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(n)- ๐’๐ฉ๐š๐œ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(n)Approach 2 uses a frequency array to count the occurrences of each number in the input array. It then scans this frequency array to identify the repeated number. This approach ensures a linear time complexity, but it uses additional space proportional to the size of the input array, making it less memory-efficient.---๐€๐ฉ๐ฉ๐ซ๐จ๐š๐œ๐ก ๐Ÿ‘:- ๐“๐ข๐ฆ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(n^2)- ๐’๐ฉ๐š๐œ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(1)Approach 3 employs a nested loop to compare each pair of elements in the array, searching for duplicates. Unfortunately, this approach results in a time complexity of O(n^2), which can lead to a "Time Limit Exceeded" error for larger input sizes, making it less efficient than other solutions.---๐€๐ฉ๐ฉ๐ซ๐จ๐š๐œ๐ก ๐Ÿ’:- ๐“๐ข๐ฆ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(n)- ๐’๐ฉ๐š๐œ๐ž ๐‚๐จ๐ฆ๐ฉ๐ฅ๐ž๐ฑ๐ข๐ญ๐ฒ: O(1)Approach 4 uses a clever algorithm known as Floyd's Tortoise and Hare (Cycle Detection) to find the duplicate number efficiently. It has a time complexity of O(n) and constant space complexity, making it the most optimal solution among the presented approaches.---These different approaches provide various trade-offs in terms of time and space complexity. Approach 4, with its linear time complexity and constant space usage, stands out as the recommended solution for efficiently solving this problem. However, it's valuable to explore and understand different approaches to problem-solving in order to enhance your coding skills.Approach1_sol: https://lnkd.in/dzV83SGAApproach2_sol: https://lnkd.in/dRFsuvMTApproach3_sol: https://lnkd.in/dKzX5R2pApproach4_sol: https://lnkd.in/dSAhimCx#CodingChallenge #LeetCode #Algorithms #Programming #ProblemSolving #ConstantSpace #FollowUpQuestions #array

    • Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (31)

    15

    Like Comment

    To view or add a comment, sign in

  • Sujeet Kumar

    Senior Software Engineer | Generative AI | AWS & Azure Certified | Microservices & API Architect | .NET Expert | Python

    • Report this post

    ๐Ÿš€DSA Series - ๐—จ๐—ป๐—น๐—ผ๐—ฐ๐—ธ๐—ถ๐—ป๐—ด ๐—Ÿ๐—ถ๐—ป๐—ธ๐—ฒ๐—ฑ ๐—Ÿ๐—ถ๐˜€๐˜ ๐— ๐˜†๐˜€๐˜๐—ฒ๐—ฟ๐—ถ๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—˜๐˜€๐˜€๐—ฒ๐—ป๐˜๐—ถ๐—ฎ๐—น ๐—ง๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—พ๐˜‚๐—ฒ๐˜€๐Ÿš€Core set of algorithms and techniques to effectively tackle a broad range of linked list problems in technical interviews and software development. ๐Ÿญ. ๐—™๐—น๐—ผ๐˜†๐—ฑโ€™๐˜€ ๐—ง๐—ผ๐—ฟ๐˜๐—ผ๐—ถ๐˜€๐—ฒ ๐—ฎ๐—ป๐—ฑ ๐—›๐—ฎ๐—ฟ๐—ฒ (๐—–๐˜†๐—ฐ๐—น๐—ฒ ๐——๐—ฒ๐˜๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป)๐—ช๐—ต๐˜† ๐—œ๐˜'๐˜€ ๐—˜๐˜€๐˜€๐—ฒ๐—ป๐˜๐—ถ๐—ฎ๐—น: Vital for identifying cycles in a linked list, determining the cycle's starting node, and assessing the cycle's length. Its applications go beyond cycle detection, including problems where detecting repetition or loops in data structures is required.๐Ÿฎ. ๐—ฅ๐—ฒ๐—ฐ๐˜‚๐—ฟ๐˜€๐—ถ๐—ผ๐—ป๐—ช๐—ต๐˜† ๐—œ๐˜'๐˜€ ๐—˜๐˜€๐˜€๐—ฒ๐—ป๐˜๐—ถ๐—ฎ๐—น: Many linked list problems, such as reversing a list, can be elegantly solved using recursion. Recursion simplifies code for operations that involve backtracking or decomposing the problem into smaller, similar subproblems.๐Ÿฏ. ๐— ๐—ฒ๐—ฟ๐—ด๐—ฒ ๐—ฆ๐—ผ๐—ฟ๐˜ ๐—ผ๐—ป ๐—Ÿ๐—ถ๐—ป๐—ธ๐—ฒ๐—ฑ ๐—Ÿ๐—ถ๐˜€๐˜๐˜€๐—ช๐—ต๐˜† ๐—œ๐˜'๐˜€ ๐—˜๐˜€๐˜€๐—ฒ๐—ป๐˜๐—ถ๐—ฎ๐—น: Sorting is a common problem, and Merge Sort is particularly well-suited to linked lists due to its efficient divide-and-conquer approach, which does not require random access to elements. Understanding how to merge two sorted lists is also crucial for solving various problems that involve combining or rearranging data in sorted order.๐Ÿฐ. ๐—ง๐˜„๐—ผ-๐—ฃ๐—ผ๐—ถ๐—ป๐˜๐—ฒ๐—ฟ ๐—ง๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—พ๐˜‚๐—ฒ๐˜€๐—ช๐—ต๐˜† ๐—œ๐˜'๐˜€ ๐—˜๐˜€๐˜€๐—ฒ๐—ป๐˜๐—ถ๐—ฎ๐—น: Beyond cycle detection, two-pointer techniques are invaluable for finding elements at a certain position from the end, detecting palindromes, and solving problems that require simultaneous traversal of a list at different rates or starting points.๐—”๐—ฑ๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ง๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—พ๐˜‚๐—ฒ๐˜€ ๐—ฎ๐—ป๐—ฑ ๐—”๐—น๐—ด๐—ผ๐—ฟ๐—ถ๐˜๐—ต๐—บ๐˜€:-๐—ฅ๐—ฒ๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ฒ ๐—ฎ ๐—Ÿ๐—ถ๐—ป๐—ธ๐—ฒ๐—ฑ ๐—Ÿ๐—ถ๐˜€๐˜: Both iterative and recursive solutions.-๐—™๐—ฎ๐˜€๐˜ ๐—ฎ๐—ป๐—ฑ ๐—ฆ๐—น๐—ผ๐˜„ ๐—ฃ๐—ผ๐—ถ๐—ป๐˜๐—ฒ๐—ฟ ๐—ง๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—พ๐˜‚๐—ฒ: For finding the middle element, detecting cycles, and more.-๐——๐˜‚๐—บ๐—บ๐˜† ๐—›๐—ฒ๐—ฎ๐—ฑ ๐—ก๐—ผ๐—ฑ๐—ฒ: Useful for simplifying operations on the head of the list, such as insertions or deletions.-๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐—น๐—ฒ๐—ฎ๐˜ƒ๐—ถ๐—ป๐—ด ๐—ฎ๐—ป๐—ฑ ๐—ฆ๐—ฝ๐—น๐—ถ๐˜๐˜๐—ถ๐—ป๐—ด: For problems that require rearranging nodes or separating a list into components based on certain criteria.If you found this post helpful, show your support below please! ๐Ÿ‘‡๐Ÿ‘ Like ๐Ÿ”„ Share ๐Ÿ’ฌ Comment๐Ÿ‘ค Follow Sujeet Kumar๐Ÿ”” Stay tuned for more updates#softwareengineering #softwaredevelopment #dsa #interview

    • Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (34)

    5

    Like Comment

    To view or add a comment, sign in

Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (36)

Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (37)

669 followers

  • 26 Posts

View Profile

Follow

Explore topics

  • Sales
  • Marketing
  • Business Administration
  • HR Management
  • Content Management
  • Engineering
  • Soft Skills
  • See All
Ali ELJALAOUI on LinkedIn: #cprogramming #bitmanipulation #codingtips #linkedinlearning (2024)

References

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6012

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.