Posts

Showing posts from February, 2016

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 do no