
using Buble sort algorithmm arrange EvenArray and PrimeArray in descending order.

import java.util.*;
public class bubbleSortExample{
public static void main(String a[]){
ArrayList<Integer> list1=new ArrayList<Integer>();
ArrayList<Integer> list2=new ArrayList<Integer>();
int i,j,count1=0,count2=0;
int array[] = {12,9,4,5,120,1,3,10};
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
bubble_srt(array, array.length);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++){
if(array[i]%2==0){
list1.add(new Integer(array[i]));
}
if(isPrime(array[i])){
list2.add(new Integer(array[i]));
}
System.out.print(array[i]+" ");
}
System.out.println();
int evenArray[]=toIntArray(list1);
int primeArray[]=toIntArray(list2);
System.out.print("Even Array in decreasing order:\n");
for(int k = evenArray.length-1; k >=0; k--){
System.out.print(evenArray[k]+" ");
}
System.out.println();
System.out.print("Prime Array in decreasing order:\n");
for(int k = primeArray.length-1; k >=0; k--){
System.out.print(primeArray[k]+" ");
}
System.out.println();
}
public static void bubble_srt( int a[], int n ){
int i, j,t=0;
for(i = 0; i < n; i++){
for(j = 1; j < (n-i); j++){
if(a[j-1] > a[j]){
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
}
static boolean isPrime(int number){
boolean isPrime=false;
int i=(int) Math.ceil(Math.sqrt(number));
while (i>1){
if ((number!=i) && (number%i==0)){
isPrime=false;
break;
}
else if (!isPrime)
isPrime=true;
--i;
}
return isPrime;
}
static int[] toIntArray(List<Integer> integerList) {
int[] intArray = new int[integerList.size()];
for (int i = 0; i < integerList.size(); i++) {
intArray[i] = integerList.get(i);
}
return intArray;
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.