Bubble Sort In bubble sort we compare a adjacent pair of items and swap if they are in incorrect order until all the items are sorted out. Code: public class BubbleSort{ public static void main(String args[]){ int []arr={99,34,23,54,32,10}; bubbleSort(arr); System.out.println("\nSorted Array:"); printArray(arr); } static void bubbleSort(int []arr){ int temp; for(int k=arr.length;k>1;k--){ for(int i=0;i<arr.length;i++){ if(arr[i]>arr[i+1]){ temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } printArray(arr); } } static void printArray(int []arr){ for(int i:arr){ System.out.print(i+", "); } System.out.println(); } } Output: 34, 23, 54, 32, 10, 99, 23, 34, 32, 10, 54, 99, 23, 32, 10, 34, 54, 99, 23, 10, 32, 34, 54, 99, 10, 23, 32, 34, 54, 99, Sorted Array: 10, 23, 32, 34, 54, 99, Linear Search In linear search we gonna take value by value sequentially until we find the item or until all the items a...
Comments
Post a Comment