import java.util.Arrays; public class SortingDemo { public static void main(String... args){ int a[]={2,1,3,5,4,8,7,9,6}; System.out.println("Before Sorting :"+ Arrays.toString(a)); insertionSort(a); System.out.println("After Sorting :"+ Arrays.toString(a)); } public static void selectionSort(int... arr){ for(int i=0;i<arr.length-1;i++){ for(int j=i+1;j<arr.length;j++){ if(arr[i]>arr[j]){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } } public static void bubbleSort(int... a){ for(int i=0;i<a.length;i++){ for(int j=0;j<a.length-i-1;j++){ if(a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } public static void insertionSort(int... arr){ for(int i=1;i<arr.length;i++){ int j=i-1; int temp=arr[i]; while(j>=0 && temp<arr[j]){ arr[j+1]=arr[j]; j--; } arr[j+1]=temp; } } }
Friday, April 29, 2016
Selection, Bubble, Insertion Sort in Java
Subscribe to:
Post Comments (Atom)
Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0...
-
Installation of MongoDB The executable file needs to be downloaded from: https://www.mongodb.com/download-center?jmp=nav#communit...
-
Form to add multiple rows: <! DOCTYPE html> <%@ page language = "java" contentType = "text/html; cha...
No comments:
Post a Comment