Insertion Sort is simple sort algorithm. In insertion sort, we pickup element and find correct place of element where it needs to be inserted and insert that element at correct place by moving elements to right side.We will keep doing same until we have all sorted elements available on left side.
Quick Sort Algorithm using Java
Quick Sort works on Divide and Conquer algorithm. Here, given array is divided into two parts, left and right. and both array are being sorted individually. After then we combine and will have sorted array.
How it works:
How it works:
- Pick up any element in Array. ex. middle element of array. we called this one as Pivot.
- All elements which are smaller than Pivot are placed in one array and All elements which are larger than pivot are placed in another array
- Using recursion sort both array using quicksort
- Combine array as mentioned below.
Labels:
CoreJava,
Java-Algorithms
Bubble Sort Algorithm using Java
In Bubble Sort, smaller elements bubbles to top of list in ascending sorting hence it's called Bubble Sort.
How it works :
How it works :
- In Bubble Sort, You take first element and second element initially and compare both elements.
- If second element is smaller than first element then you swipe places. how? using temp :) as below
- You keep doing same meaning stepping through array and comparing element with pair of adjacent elements until no swapping required and you have sorted array
Labels:
CoreJava,
Java-Algorithms
Binary Search Algorithm using Java
Binary search works faster than sequential search.It search on sorted elements.
In Binary Search, Let's say You have an array and you want to search specific item in array
How it Works :
In Binary Search, Let's say You have an array and you want to search specific item in array
How it Works :
- Initialize low=0 and high as max length of search array - 1.
- while low<=high each item you, take middle element of array and if given searchItem is less than middle element then you need to search in array of elements array which are less than middle element. Hence, high = middle - 1
Labels:
CoreJava,
Java-Algorithms
Sequential Search Algorithm using Java
Sequential Search is simplest algorithm. You try to search given element in given collections sequentially and return only if you found element.
Labels:
CoreJava,
Java-Algorithms