sorting

Insertion Sort(Java Implementation)

public class Sort { public static void main(String[] strr) { int arr[] = {9,8,7,6,5,4,3,2,1}; System.out.println(” Before sorting”); for(int j=0;j<=arr.length-1;j++) { System.out.print(” “+arr[j]); } new Sort().insertionSort(arr); System.out.println(“\nAfter Sorting”); for(int j=0;j<=arr.length-1;j++) { System.out.print(” “+arr[j]); } } public void insertionSort(int arr[]) { int temp; for(int i=1;i<=arr.length-1;i++) { temp=arr[i]; int j; for (j=i-1; j>=0; j– ) { if(arr[j]>temp) arr[j+1]=arr[j]; […]

Insertion Sort(Java Implementation) Read More »

Bubble Sort(Java Implementation)

Bubble sort algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items. public class Sort { public

Bubble Sort(Java Implementation) Read More »

Selection Sort(Java Implementation)

Selection sort is a simple sorting algorithm. It sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted. Initially, the sorted part is

Selection Sort(Java Implementation) Read More »