Algorithm
Algorithm
Time Complexity:
O(n) :
> it have loop. run time of loop depends on n
>ex : n =10;
for(int j=0;j<n; j= j++){
........
}
O(n^c):
> it have nested loops. no of nested is (n^c)
> ex : for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
........
}
}
O(logn) :
> in a loop, if iteration includes in multiplication or division
>ex :
for(int j=0;j<10;j= j*2){
........
}
O(nlogn):
> nested loop & one of loop have multiplication or division in each iteration
> ex : for(int i=0;i<10;i++){
for(int j=0;j<10;j= j*2){
........
}
}
O(loglogn) :
> in a loop, if iteration includes exponential.
> ex : Math.pow()
Searching Algorithms:
--> to search an element based on complexity there are two ways :
--> Linear Search
--> Binary Search
Sorting:
--> sorting of elements ascending order
--> two types:
- bubble sort
- merge sort
Bubble Sort:
-> max element in array is arranged in its position for every iteration by using
compareTo method
-> time complexity of bubble sort is : O(n2)
-> each iteration to sort element is called as pass
-> no. of passes cant decide as position of element may vary for each iteration
-> Worst case occurs when the elements are sorted in reverse order because all the
elements should be compared and swapped
Merge Sort:
-> divides unsorted list repeatedly into sublist contains only one element in each
sublist.
-> it is called demerging
-> merges the sublist of divided elements repeatedly to produce ascending order of
sorted list.
-> it is called merging.