Skip to main content

Posts

Showing posts with the label sorting

Merge Sort Implementation in JAVA

Merge Sort  is a technique of sorting data with below steps:- 1) Divide the data recursively until it can not  divide more. 2)  Compare the data and sort it. 3) Merge the data again . /* * Merge Sort is based on the concept below points:- * 1) Divide array until it can not be divide. * 2) Merge it again in sorted pattern * * */ public class MergeSort { public static int [] mergeSort ( int arr []) { //check for size if it is one then already sorted int size = arr . length ; if ( size == 1 ) { return arr ; } //find middle (divide an array into two half) int mid = size / 2 ; //first half int [] fhalf = new int [ mid ]; //second half int [] shalf = new int [ size - mid ]; // Copy first half of the array for ( int i = 0 ; i < mid ; i ++) { fhalf [ i ] = arr [ i ]; } // Copy second half of the array for ( int i = mid...

Insertion Sort in JAVA

What is insertion sort? It is a sorting technique that is based on the partitioning of array into two parts like selection sort, sorted and unsorted. The process is:- compare adjacent elements. if(left > right) Swap the element { check for sorted array whether it is still in sort manner(put j=i and loop in until j is not equal to zero) - No check for if(right<left) swap do it until at the end of position. } Implementation:- Underline eleemnts are part of sorted array inside main array loop 14 27 10 33 35 19 42 4 ( 14 10 27 33 35 19 42 4 (here sorted array is distorted hence sorting has been done inside the loop) inside sorted array loop 10 14 27 33 35 19 42 4 inside main array loop 10 14 27 33 35 19 42 4 inside main array loop 10 14 27 33 35 19 42 4 inside main array loop 10 14 27 33 35 19 42 4 inside sorted array loop 10 14 27 19 33 35 42 4 inside sorted array loop 10 14 19 27...

Selection Sort in JAVA

What is selection sort? It is a sorting technique that is based on the partitioning of array into two parts, sorted and unsorted. The process is:- 1) Find minimum element in unsorted array. 2) Swap the element at the end of sorted array. when i=0 we don't have any sorted array so element will be replaced from first element later the sorted array end will growas the index. Steps:- underline elements are sorted array, the length is increasing with each minimum element added at the end. minimum element11 11 25 12 22 64 minimum element12 11 12 25 22 64 minimum element22 11 12 22 25 64 minimum element25 11 12 22 25 64 minimum element64 11 12 22 25 64 Program:- package sorting ; public class SelectionSort { //function to print array public static void print ( int [] arr ) { for ( int i = 0 ; i <= arr . length - 1 ; i ++) System . out . print ( " " + arr [ i ]); System . out . printl...

Bubble Sort in JAVA

What is bubble sort ? It is a sorting technique that is based on the comparison.Here we compare adjacent element, if the first element is larger than the second we swap each other. We do the same procedure again and again until array do not sort completely. Example:- 5 1 4 2 8 5 1 4 2 8   here pass is nothing but iterating the loops equal to number of elements in the array but if it already sorted before then we can break the loop anddo exist PASS1 case0 1 //check 0 and first element 1 5 4 2 8 case1 2 //check 1 and 2 element 1 4 5 2 8 case2 3 1 4 2 5 8 case3 4 1 4 2 5 8 swap istrue PASS2 //first pass completed now do second pass case0 1 1 4 2 5 8 case1 2 1 2 4 5 8 case2 3 1 2 4 5 8 case3 4 1 2 4 5 8 swap istrue PASS3 // third pass case0 1 1 2 4 5 8 case1 2 1 2 4 5 8 case2 3 1 2 4 5 8 case3 4 1 2 4 5 8 swap isfalse since swap is false we break from the loop and d...

.