Bubble sort is one of the simplest sorting algorithms and it works on the principle of swapping two elements. Think of the bubble sort as a water bubble that arises from the bottom of the ocean. As it rises up, it grows in size and its the maximum when it reaches the surface. Similarly, in bubble sort, we arrange items from smallest to the largest at the end one by one in each iteration.The algorithm for bubble sort is as follows:- We start from the bottom of the array and …
nikoo28
nikoo28
a tech-savvy guy and a design buff... I was born with the love for exploring and want to do my best to give back to the community. I also love taking photos with my phone to capture moments in my life. It's my pleasure to have you here.
-
-
As the name suggests SELECTION SORT involves selecting an element. Now the question arises, as to how should we select this element. What is the criteria? Where should we put it? All these answers are given in the algorithm for SELECTION SORT. In selection sort, what we do is:- Start from the first position in the array. Traverse the remaining array to find the smallest number Swap this smallest number with the number we selected in the first place. Repeat steps 2 and 3 with the next position. Let us …
-
Insertion sort is the most generic example of a sorting algorithm, and the one which is used by one of the most naive users. Often we must have also used this algorithm in our real life examples unknowingly. One of the favorite example is:- This is a very common scenario and we must have used it. Suppose you get a hand of cards. Our general approach is that we start scanning the cards from starting and if we find a card out of place, we remove it from there and …
-
What is sorting? Sorting is an algorithm that arranges the elements of a list in a certain order (either ascending or descending, as per the requirement). The output is simply a permutation of the input data. Why sorting? Sorting is one of the most important categories of algorithms in computer science. Sometimes sorting significantly reduces the problem complexity. We can use sorting as a technique to reduce the search complexity. Great research went into this category of algorithms because of its importance. These algorithms are very much used in many …
-
ITERATIVE VERSION The iterative version can be done in this way:- Input: num Initialize rev_num = 0 Loop while num > 0 Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; Divide num by 10 Return rev_num Here is an implementation of the same. RECURSIVE WAY
-
We will discuss how to calculate the result when a number ‘x’ is raised to the power ‘y’. Originally C provides a standard function that allows us to directly use the power function. It can be used in the following manner. Now suppose that we need to write a custom function by ourselves. Here are the sample methods by which we can do so. Simple Iterative method Recursive Method But in both these methods, the time complexity is of O(n), and this can take a long time in operation. We …
-
Here is an implementation of a simple algorithm that can be used to convert any number in decimal format to a number to any other base ‘b’. The base ‘b’ can be octal, binary or even any other arbitrary base like 7.
-
In some situations we might need to find the minimum/maximum element among a collection of elements. Priority Queue ADT is the one which supports these kind of operations. A priority queue ADT is a data structure that supports the operations Insert and DeleteMin (which returns and removes the minimum element) or DeleteMax (which returns and removes the maximum element). These operations are equivalent to EnQueue and DeQueue operations of a queue. The difference is that, in priority queues, the order in which the elements enter the queue may not be …
-
We generally see cases like:-Here ptr is a pointer to a memory location of the variable x. What if we do something likeWhat we did just now was made a pointer to a pointer. Now the question arises, what happens behind the scene? Let’s assume an 8 bit computer with 8 bit addresses (and thus only 256 bytes of memory). This is part of that memory (the numbers at the top are the addresses): What we can see here, is that at address 63 the string “hello” starts. So in …
-
Here what we are trying to learn is the difference between:- and The difference is that const char * is a pointer to a const char, while char * const is a constant pointer to a char. const char * :- In this, the value being pointed to can’t be changed but the pointer can be. charĀ * const :- In this, the value being pointed at can change but the pointer can’t. The third type is which is a constant pointer to a constant char (so nothing about it …