Posts

Merge Sort

Image
 Merge Sort We need to break the array into two halves. Once u break the array into two halves, u need to break those two separate halves into 2 more halves, a total of 4.  Like this, we need to break the array, where the individual array will have only one element using recursion. Once the array is broken into tiny pieces, we will join them again by merging them together. To achieve this, first we need to write a program to merge two sorted arrays.

Selection Sort

  Selection Sort Finding(selecting) the smallest element in the loop and swapping it to the starting of the unsorted array. This is best used for finding the smallest element in the array. Let's take an array We will write the outer loop Let's assume the first index of outer loop as smallest(lowest) number Now write the inner loop, and start the inner loop as i+1. Because we need to compare two adjacent elements, hence inner loop starts with i+1 Now after the inner loop, compare the lowest element with current inner loop element. If the inner loop element is lower than lowest element, then reassign lowest number index to j value. After completing the inner loop, check the lowest index value is changed or not If it changed, swap the lowest element index with outer loop index. function selectionSort (arr) {     for (let i = 0; i < arr.length; i++) {         var lowest = i;         for (let j = i + 1; j < arr.length; j++) { ...

Array logics

 Array logics: 1. Write a program to print the smallest number in an array? function lowestElement(arr) {         for (let j = arr.length-1; j > 0; j--) {             if(arr[j] < arr[j-1]) {                 var temp = arr[j];                  arr[j] = arr[j-1];                 arr[j-1] = temp;             }                     }     console.log(arr[0]);      } The above program is based on the bubble sorting logic, sending the lower element to first place. function smallestNumberWithSelectionSort(arr) {     var lowest = 0;     for (let i = 1; i < arr.length; i++) {         if(arr[i] < arr[lowest]) {             lowest = i...

Bubble sort

Image
 Bubble sort.  https://www.youtube.com/watch?v=p__ETf2CKY4 We need to compare the elements in the array and send the biggest element to the end of the array. So if we take the inner for loop mentioned in the code, it will check the first and second elements in the array.  If the first element is greater than the second one then it will swap the elements' position in the array. By performing the same operation again and again with other elements in the array, the biggest element in the array will be pushed to the last position in the array. So with the inner for loop, we will push the largest number to the end position in the array. With the outer loop, we will perform the same operation until all the numbers are sorted. function bubbleSort (arr) {          for (let i = 0; i < arr.length; i++) {                  for (let j = 0; j < arr.length-1-i; j++) {            ...

Numbers programs

 1. Write a program to find the sum of natural numbers? function addUpToNumber(n) {     var total = 0;     for(let i = 1; i <= n; i++) {         total = total + i;     }     return total; }     addUpToNumber(4); 2. Write a program to find the sum of natural numbers without loop? function addUpToNumber(n) {     return n * (n+1)/2 } addUpToNumber(4); 3. Write a program to find the sum of even numbers? function sumOfFirstEvenNumbers(n) {     return n * (n+1) }     sumOfFirstEvenNumbers (4);  4. Write a program to find the sum of even numbers in a range?   function sumOfEvenNumbersBetweenRange(i, n) {     var result = 0;     for (let index = i; index <= n; index++) {         if(index % 2 == 0) {             result = result + index;             console.log(result);   ...