Packagecom.db2;/*** Array Sort *@authorDenny **/ Public classDemo3 { Public Static voidMain (string[] args) {int[] arr={3,1,8,2,9,100,33,22,11,18,14,17,15,3}; /*Selectsort (arr);//select Sort Printarr (arr);//Traverse*/Bubblesort (arr);//Bubble SortPrintarr (arr);//Traverse } /** Select Sort*/ Public Static voidSelectsort (int[] arr) { //external cycle Control times for(intx=0;x<arr.length-1;x++){ //inner loop control from which element to start comparing for(inty=x+1;y<arr.length;y++){ //Compare, swap if(arr[x]>Arr[y]) { inttemp=Arr[x]; ARR[X]=Arr[y]; Arr[y]=temp; } } } } /** Bubble Sort*/ Public Static voidBubblesort (int[] arr) { //external cycle Control times for(intx=0;x<arr.length-1;x++){ //inner loop control from which element to start comparing for(inty=0;y<arr.length-x-1;y++){ //Compare, swap if(arr[y]>arr[y+1]){ inttemp=Arr[y]; Arr[y]=arr[y+1]; Arr[y+1]=temp; } } } } //iterating through an array Public Static voidPrintarr (int[] arr) {System.out.print ("["); for(inti=0;i<arr.length;i++){ if(i!=arr.length-1) {System.out.print (Arr[i]+","); }Else{System.out.println (Arr[i]+"]"); } } }}
Select sort and Bubble sort