DSA Basics(my dsa jouney)
In basics, there are searching and sorting algorithms given below in the java code
Bubble sort: take two loops and compare the adjacent element and when required swap them
Selection sort: basically using this algorithm you sort the initial part of the array means in the first completion of the outer loop the lowest element is at the first position so there we have to swap the first element with the smallest of the element in the remaining array. The second j loop does the same it finds the smallest element.
for(int i =0 ; i<arr.length-1;i++){
int mini = i;
for(int j =i+1; j<arr.lenght ; j++){
if(arr[j]>arr[i]){
mini =j;
}
}
// swap arr[mini ] and arr[i]
Insertion sort: here we have to keep sorting the elements to the left of the ith element and in the first loop we set that ith element equal to the current like in the while loop we are just sorting all the numbers before the ith number in the ascending order
for(int i =1;i<arr.length;i++){
int current = arr[i];
int j = i-1;
while(j>=0 && arr[j]>current){
swap arr[j+1] and arr[j];
j--;
}
arr[j+1] = current;
}
Also today I did one question about finding the same tree the concept is that if the traversal of the tree is the same then the trees are the same
Today I learned about merge sort and finding maximum depth in binary search tree
the code of merge sort is as follows
static int[] mergesort(int[] arr){
if(arr.length ==1){
return arr;
} int mid = arr.length/2;
int[] left = mergesort(Array.copyOfRange(arr,0,mid));
int[] right = mergesort(Array.copyOfRange(arr,mid,arr.length));
return merge(left,right);
}
static int[] merge(int[] first, int[] second){
int mix[] = new int[first.length+second.length];
int =0;
int j =0;
int k =0;
while(i<first.length&& j<second.length){
if(arr[i]<arr[j]){
mix[k] = arr[i];
i++;
}else{
mix[k]= arr[j];
j++:
}
k++;
} while(i<first.length){
mix[k] = arr[i];
i++;
k++;
}while(j<second.length){
mix[k] = arr[j];
j++;
k++;
}
return mix
}
Now note that how in the binary search the number of searches we performed were log n times same way there is also there will this dividing and all log n time and in every pass, we are doing n comparisons so the time complexity will be NlogN
now moving towards the Depth in bst is as follows
public int maxheight(TreeNode root){
if(root == null){
return 0;
}
int lh= maxheight(root.left);
int rh = maxheight(root.right);
return 1+Math.max(lh,rh);
}
Also there was a question where I applies insertion sort and the answer got accepted
for(int i =1 ; i<arr.length;i++){
int current = arr[i];
int j = i-1;
while(j>=0 && arr[j]>current){
// here swap
arr[j+1] and arr[j];
j--;
}
arr[j+1] = current;
}
Along with that, I recalled inorder traversal using the iterative method and the code goes as follows :
public int<>ArrayList inorder(TreeNode root){
create new arraylist inorder;
create new stack s;
while(true){
if(root != null){
stack.add(root);
root = root.left;
}else{
if(stacl.isEmpty())break;
root = stack.peek;
arraylist.add(root.val);
stack.pop();
root = root.right;
}
return arraylist;
}
So the next thing in the list is in place merge sorting and Balanced binary tree