
Patrick B. answered 03/16/21
Math and computer tutor/teacher
class BubbleSort
{
public int[] bubbleSort( int array[])
{
//deep copies
int N = array.length;
int A[] = new int[N];
for (int iLoop=0; iLoop<N; iLoop++)
{
A[iLoop]=array[iLoop];
}
for (int iLoop=N-1; iLoop>0; iLoop--)
{
for (int jLoop=0; jLoop<iLoop; jLoop++)
{
if (A[jLoop]>A[jLoop+1])
{
int iTemp = A[jLoop+1];
A[jLoop+1]=A[jLoop];
A[jLoop]=iTemp;
}
} //for jLoop
} //for iLoop
return(A);
} //bubbleSort
public void arrayDump(int A[], String strMsg)
{
if (strMsg!=null)
{
System.out.println("************************************************");
System.out.println(strMsg);
}
System.out.println("****************************************************");
for (int iLoop=0; iLoop<A.length; iLoop++)
{
System.out.println(A[iLoop]);
}
}
public static void main(String args[])
{
BubbleSort mySort = new BubbleSort();
int a[] = {4,7,1};
int A[] = {80,6,6,8,2};
int iNums[] = {5,23,16,34,9,36,24,12,18,19,27,21,4,10,3,31,42,37,6,33,8,25,32,39 };
int aSort[] = mySort.bubbleSort(a);
int Asort[] = mySort.bubbleSort(A);
int iSort[] = mySort.bubbleSort(iNums);
mySort.arrayDump(aSort,"aSort");
mySort.arrayDump(Asort,"Asort");
mySort.arrayDump(iSort,"iSort");
}
}