Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Algorithms with Programming Concepts in Java!

NotificationsYou must be signed in to change notification settings

sb255/Algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Algorithms Topics:

  • Big O Concept
  • Searching Algorithms
  • Sorting Algorithms
  • Recursion
  • BitWise Operation
  • Dynamic Programming

Most Basic types of array declaration:

data-type varName[size];

OR

data-type[size] varName;


Initializing an array

int[] array = new int[size];

OR

int[] array = {0,1,2,3,4,5,6,7,8,9};


Most common methods to find the size or length of an array

arr.length -> for calculating the length of all types of array (int[], String[], char[])

arr.size() -> for calculating the size of an object array(Ex. List array as it stores only objects)


Point to remember

.length -> It is used for calculating the length of all types of array (int[], String[], char[])

.length() -> It is used for calculating the length of a String


Big O:

Run-time: Time taken or the Iteration made by function/algorithm to process the input. If the number of Iteration are equal to the given input x, then the run time will be O(x).

  • O(n): When run-time is proportional to the input size n. In an average case of a linear search algorithm, we have to check each value in an array, the run time in that case is proportional to the size of the array.

  • O(1): When run-time is constant and independent of the input size.

  • O(log(n)): When run-time increases exponentially with the size of input. In case of binary search, the worst case is O(log(n)).


Searching Algorithms:

Linear Search Algorithm:

intlinearSearch(int[]arr,intsearchElement){intarray[] =arr;intsElement =searchElement;for(inti=0;i<array.length;i++){if(array[i]==sElement)returni;        }return -1;    }

Binary Search Algorithm:

/*--We are assuming that the array is sorted and the array has more than one element--*/intbinarySearch(int[]array,intsearchElement,intstartingIndex,intendingIndex){intmiddleIndex = (startingIndex +endingIndex)/2;if(searchElement==array[middleIndex]){returnmiddleIndex;        }elseif(searchElement<array[middleIndex]){returnbinarySearch(array,searchElement,startingIndex,middleIndex-1);        }elseif(searchElement>array[middleIndex])returnbinarySearch(array,searchElement,middleIndex+1,endingIndex);elsereturn -1;    }

Time and Space complexities of search algorithms:

Time Complexity:

Search AlgorithmBest CaseAverage CaseWorst Case
Linear SearchO(1)O(n)O(n)
Binary SearchO(1)O(logn)O(logn)

Space Complexity:

Search AlgorithmSpace Complexity
Linear SearchO(1)
Binary SearchO(1)

Sorting Algorithms:

Bubble Sort:

SWAPS the first two elements of the array and then swaps the adjacent elements, and keeps on SWAPPING until the array is sorted!

Merge Sort:

First DIVIDES the array into two halves, SORT each of them and then MERGE them together!

Quick Sort:

In quick sort, we create a partition known as PIVOT, and then divide the array on the basis of elements less than the PIVOT and the elements larger than the PIVOT element!

Selection Sort:

Performs a LINEAR SCAN, find the smallest element and place it to 0th Index, and then keeps on doing the process until the array is sorted!

Insertion Sort:

In Insertion sort, we COMPARE a element with the elements COMING BEFORE IT in the array, Ex: Element at INDEX=3, will be compared with the elements positioned at INDEX=0,1,2 respectively and then the element is placed at the correct position!

General Sorting Algorithm:

int[]generalSort(int[]arr){int[]ar = {2,3,4,1,8,9,5,6,7};intbuk =0;for(inti=0;i<ar.length;i++){for(intj=i+1;j<ar.length;j++){if(ar[i]>ar[j]) {buk =ar[i];ar[i] =ar[j];ar[j] =buk;                    }                }            }returnar;    }
/*--In built methods for sorting Arrays and Lists--*/Arrays.sort(stringArray);Collections.sort(list);

Time and Space complexities of sorting algorithms:

Time Complexities:

Sorting AlgorithmBest CaseAverage CaseWorst Case
Bubble SortO(n)O(n^2)O(n^2)
Merge SortO(nlog(n))O(nlog(n))O(nlog(n))
Quick SortO(nlog(n))O(nlog(n))O(n^2)
Selection SortO(n^2)O(n^2)O(n^2)
Insertion SortO(n)O(n^2)O(n^2)

Space Complexities:

Sorting AlgorithmSpace Complexity(Worst)
Bubble SortO(1)
Merge SortO(n)
Quick SortO(1)
Selection SortO(1)
Insertion SortO(1)

Recursion:

/*--Printing the elements of a SinglyLinkedList in reverse order using recursion--*/staticvoidprintInReverse(Nodehead) {if(head ==null){        }else{printInReverse(head.next);System.out.println(head.data);        }    }//--Recursion Stack: [printInReverse1(printInReverse2(printInReverse3(...)))]//--Calling Order(LIFO): ..., printInReverse3, printInReverse2, printInReverse1

BitWise Operations:

  • AND | & : It returns true if both the operands are true.
  • OR | | : It returns true if at-least one of the operands is true.
  • XOR | ^ : It returns true if exactly one of the operands is true.

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp