You are on page 1of 3

BUBBLE SORT

import java.util.Arrays;
/**
* @author Crunchify.com
*/
public class sorting1 {
public static void main(String[] args) {
int arrayList[] = { 3,5,7,9,1,8 };
System.out.println("\nFinal
result:"+Arrays.toString(CrunchifyBubbleSortDescMethod(arrayList)));
}
public static int[] CrunchifyBubbleSortDescMethod(int[] arr){
int temp;
for(int i=0; i < arr.length-1; i++){
for(int j=1; j < arr.length-i; j++){
if(arr[j-1] < arr[j]){
temp=arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
System.out.println((i+1)+"th iteration result:
"+Arrays.toString(arr));
}
return arr;
}
}

SELECTION SORT
import java.util.Scanner;
public class sorting1{
public static void main(String a[]){
int i;
int array[] = {12,9,4,99,120,1,3,10};
System.out.println("\n\n RoseIndia\n\n");
System.out.println(" Selection Sort\n\n");
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
selection_srt(array, array.length);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
System.out.println("PAUSE");
}
public static void selection_srt(int array[], int n){
for(int x=0; x<n; x++){
int index_of_min = x;
for(int y=x; y<n; y++){
if(array[index_of_min]<array[y]){
index_of_min = y;
}
}
int temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
}
}
}

MERGE SORT