Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
Time Complexity: O(N^2)
for i ← 1 to length(A) - 1 j ← i while j > 0 and A[j-1] > A[j] swap A[j] and A[j-1] j ← j - 1
Bubble sort has worst-case and average complexity both О(n2)
First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) ( 1 4 2 5 8 )
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
1 procedure bubbleSort( A : list of sortable items ) 2 n = length(A) 3 repeat 4 swapped = false 5 for i = 1 to n-1 inclusive do 6 /* if this pair is out of order */ 7 if A[i-1] > A[i] then 8 /* swap them and remember something changed */ 9 swap( A[i-1], A[i] ) 10 swapped = true 11 end if 12 end for 13 until not swapped 14 end procedure
selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists
Time Complexty: O(N^2)
1 public class MySelectionSort { 2 3 public static int[] doSelectionSort(int[] arr){ 4 5 for (int i = 0; i < arr.length - 1; i++) 6 { 7 int index = i; 8 for (int j = i + 1; j < arr.length; j++) 9 if (arr[j] < arr[index]) 10 index = j; 11 12 int smallerNumber = arr[index]; 13 arr[index] = arr[i]; 14 arr[i] = smallerNumber; 15 } 16 return arr; 17 } 18 19 public static void main(String a[]){ 20 21 int[] arr1 = {10,34,2,56,7,67,88,42}; 22 int[] arr2 = doSelectionSort(arr1); 23 for(int i:arr2){ 24 System.out.print(i); 25 System.out.print(", "); 26 } 27 } 28 }
or
1 /* a[0] to a[n-1] is the array to sort */ 2 int i,j; 3 int iMin; 4 5 /* advance the position through the entire array */ 6 /* (could do j < n-1 because single element is also min element) */ 7 for (j = 0; j < n-1; j++) { 8 /* find the min element in the unsorted a[j .. n-1] */ 9 10 /* assume the min is the first element */ 11 iMin = j; 12 /* test against elements after j to find the smallest */ 13 for ( i = j+1; i < n; i++) { 14 /* if this element is less, then it is the new minimum */ 15 if (a[i] < a[iMin]) { 16 /* found new minimum; remember its index */ 17 iMin = i; 18 } 19 } 20 21 if(iMin != j) { 22 swap(a[j], a[iMin]); 23 } 24 25 }
Descending Order:
1 public class descending { 2 3 public static int[] doSelectionSort(int[] arr){ 4 5 for (int i = arr.length-1; i >= 1; i--) 6 { 7 int index = i; 8 for (int j = 0; j < i; j++) 9 if (arr[j] < arr[index]) 10 index = j; 11 12 int smallerNumber = arr[index]; 13 arr[index] = arr[i]; 14 arr[i] = smallerNumber; 15 } 16 return arr; 17 } 18 19 public static void main(String[] args){ 20 21 int[] arr1 = {10,34,2,56,7,67,88,42}; 22 int[] arr2 = doSelectionSort(arr1); 23 for(int i:arr2){ 24 System.out.print(i); 25 System.out.print(", "); 26 } 27 } 28 }
Merge Sort:
1 package ArrayMergeSort; 2 3 import java.util.Arrays; 4 5 public class Solution { 6 public int[] mergeSort(int[] arr) { 7 if (arr.length == 1) return arr; 8 else { 9 int[] arr1 = Arrays.copyOfRange(arr, 0, arr.length/2); 10 int[] arr2 = Arrays.copyOfRange(arr, arr.length/2, arr.length); 11 return merge(mergeSort(arr1), mergeSort(arr2)); 12 } 13 } 14 15 public int[] merge(int[] arr1, int[] arr2) { 16 int len1 = arr1.length; 17 int len2 = arr2.length; 18 int[] res = new int[len1+len2]; 19 int i = 0, j=0, cur=0; 20 while (ilen2) { 21 if (arr1[i] <= arr2[j]) { 22 res[cur++] = arr1[i++]; 23 } 24 else { 25 res[cur++] = arr2[j++]; 26 } 27 } 28 while (i<len1) { 29 res[cur++] = arr1[i++]; 30 } 31 while (j<len2) { 32 res[cur++] = arr2[j++]; 33 } 34 return res; 35 } 36 37 38 39 /** 40 * @param args 41 */ 42 public static void main(String[] args) { 43 // TODO Auto-generated method stub 44 Solution sol = new Solution(); 45 int[] arr = sol.mergeSort(new int[]{6,5,4,8,2,1}); 46 System.out.println(Arrays.toString(arr)); 47 } 48 49 }