public class sortdemo {//private static long[] arr = {6,5,2,7,1,8,4,3} ;p rivate static long[] arr = {1,2,3,4,5,6,7,8};p rivate static int count = 0;/** * Bubble sort * o (n2) */public static void bubble () { Long temp = long.min_value;int len = arr.length;for (int j = 0;j <len;j++) {for (int i = 0;i<len-1;i++) {if (arr[i]>arr[i+1]) {Temp = arr[i+1];arr [I+1] = arr[i];arr[i] = temp;}}} /** * Select Sort * o (n2) */public static void select () {int len = arr.length;long temp = long.min_value;int index = 0;for (int j = 0;j<len;j++) {index = j;for (int i = j;i<len;i++) {if (Arr[i]<arr[index]) { Index = i;}} Temp = arr[j];arr[j] = arr[index];aRr[index] = temp;print ();}} /** * Insert Sort * o (n2) */public static void insert () {int len = arr.length;long temp;for (int i=1;i<len;i++) {Int j = i-1;temp = arr [I];while (J>=0 && temp<arr[j]) {arr[j+1] = arr[j];j--;} Arr[j+1] = temp;print ();}} /** * Quick Sort * o (N*LONGN) */public static void quick () {quick (arr);p rint ( );} Private static long[] quick (Long[] a) {if (a.length<=1) return a;long temp = 0;int index = 0;for (int i = 0;i<a.length-1;i++) {if (a[i]<a[ A.length-1]) {temp = a[i];a[i] = a[index];a[index++] = temp;}} Temp = a[a.length-1];a[a.length-1] = a[index];a[index] = temp;if (index==0) return a;long[] temp1 = new long[index];long[] temp2 = new&nbsP;LONG[A.LENGTH-INDEX-1]; System.arraycopy (A, 0, temp1, 0, index); System.arraycopy (a, index+1, temp2, 0, a.length-index-1); System.arraycopy (Quick (TEMP1), 0, a, 0, index); System.arraycopy (Quick (TEMP2), 0, a, index+1, a.length-index-1); return a;} /** * merge sort * o (N*LONGN) */public static void merge () {arr = merge (arr);p rint ();} Private static long[] merge (Long[] a) {long[] c = new long[a.length]; int mid = a.length/2;long[] temp1 = new long[mid];long[] temp2 = new long[a.length-mid];if (mid>1) {system.arraycopy (a, 0, temp1, 0, mid ); System.arraycopy (A, mid, temp2, 0, a.length-mid); Temp1 = merge (TEMP1); temp2 = merge (TEMP2); System.arraycopy (Temp1, 0, a, 0, mid); System.arraycopy (TEMP2,&NBSP;0,&NBSP;A,&NBSP;MID,&NBSP;A.LENGTH-MID);} Int i=0,j=mid,index=0;while (i<mid && j<a.length && index< A.LENGTH-1) {if (A[i]<a[j]) {c[index++] = a[i++];} Else{c[index++] = a[j++];}} while (Index<a.length && i<mid) {c[index++] = a[i++];} while (index<a.length && j<a.length) {c[index++] = a[j++];} Return c;} Public static void print () {for (Long v:arr) {System.out.print (v);} System.out.println ();} Public static void main (String[] args) {quick ();}}
Java Implementation sequencing