Skip to main content

Posts

Showing posts from January, 2023

Bubble Sort | Algorithm | MyCodingNetwork

Bubble Sort Bubble sort is a sorting algorithm which is used to sort the elements of an array and its time complexity O(N 2 ).  This algorithm’s time complexity is not as efficient as Merge Sort but still it is one of the simplest sorting algorithms. Source Code: Output: The highlighted part is the sorted array (ascending order). Working: In the program shown above, we have used bubble sort technique to sort the array in ascending order, given by the user as the input ( in the main( ) function ). The entire bubble sorting algorithm ( in the program ) takes place in the function bubblesort(), the function takes two parameters.  When an array undergoes bubble sort, continuous swapping takes place (if the array is not in desired order). The program given above is to arrange the array in ascending order, it can be changed to descending order, simply by changing “greater than” symbol to “less than” symbol on line 33. Swapping takes place from line 36( swapping takes place only if the elemen

Merge Sort | Algorithm | MyCodingNetwork

Merge Sort Hi there, I am back with another article on one of the most important algorithms in the world of DSA. In this article we would be covering Merge Sort and talk about its various aspects. So, without wasting much time let's jump into the topic right away. Merge sort is another type of sorting algorithm whose time complexity is O(N*logN) . The worst, average and best time complexity of merge sort always remains same , that is, O(N*logN) . You must be thinking why the time complexity is same for all cases. Well, the answer to that we will get as we move further in our discussion. It works on divide and conquer technique, where the given array is divided into smaller somebodies of two halves and then sorting is done. Source Code: conquer()  function: divide( ) function: main( ) function: Output: #1  In the above example, user first enters the length of the array, and then the array elements. The highlighted part are the elements of array after sorting. #2 Let's take anot

Binary Search | The-Algorithm-Drive | MyCodingNetwork

Binary Search The next algorithm, we are going to study is Binary Search . As the name suggests, it is a searching algorithm. It is one of the very famous searching techniques. Unlike its counterpart linear search, it is way more advanced.  Time complexity of linear search is O(N) whereas that of binary search is O (log N). Source Code: Output Number Found at index =5 Working Let’s come on to the working of the algorithm, as you can see in the above program, we need a sorted array to perform binary search algorithm. 1.    Here, bin function in the above program it accepts 4 parameters, that is, the array arr , lower index low , higher index high and the searching number sn . 2.    Next, we have a do-while loop to check if high is greater than or equal to low (high>=low). If at any point of iteration this condition is not satisfied, it means the searching number sn does not exist in the array. Following is the algorithm for binary search: while (high >= low)