# Answer to Question #16161 in Java | JSP | JSF for Lala Davies

Question #16161

write a Java program that sorts a sequence of numbers using two different algorithms: insertInOrder and bubbleSort.

input file 1 - 12 23 5 17 6 19 13 3 41 21 11 22 30 31 16

input file 2 - 58 120 121 69 138 171 36 175 31 115 104 22

input file 1 - 12 23 5 17 6 19 13 3 41 21 11 22 30 31 16

input file 2 - 58 120 121 69 138 171 36 175 31 115 104 22

Expert's answer

import java.util.Random;

public class main {

/**

* @param

args

*/

public static void main(String[] args)

{

// int[] arr=new

int[10];

// Random ran=new Random();

// for (int i = 0; i < arr.length;

i++)

// {

// arr[i]=ran.nextInt(100);

// }

int[] arr={12, 23, 5,

17, 6, 19, 13, 3, 41, 21, 11, 22, 30, 31, 16};

System.out.println("Unsorted

array");

for (int i = 0; i < arr.length; i++)

{

System.out.print(arr[i]+"

");

}

System.out.println();

System.out.println("Sorted

array(insertion sort)");

int[] ins=insertionSort(arr, 0, arr.length);

for

(int i = 0; i < arr.length; i++)

{

System.out.print(ins[i]+"

");

}

System.out.println();

int[]

sorted=bubbleSort(arr);

System.out.println("Sorted array(bubble

sort)");

for (int i = 0; i < arr.length; i++)

{

System.out.print(sorted[i]+" ");

}

}

public static int[]

insertionSort (int[] arr, int a, int b)

{

int[] m=arr.clone();

int t;

int i, j;

for ( i=a; i < b; i++)

{

t = m[i];

for ( j=i-1; j>=a && m[j] >

t; j--)

m[j+1] = m[j];

m[j+1] = t;

}

return m;

}

public static int[]

bubbleSort(int[] arr)

{

int buf;

int[] a=arr.clone();

for (int i =

0; i < a.length-1; i++)

{

for (int j = i+1; j < a.length; j++)

{

if(a[i]>a[j])

{

buf=a[i];

a[i]=a[j];

a[j]=buf;

}

}

}

return

a;

}

}

public class main {

/**

* @param

args

*/

public static void main(String[] args)

{

// int[] arr=new

int[10];

// Random ran=new Random();

// for (int i = 0; i < arr.length;

i++)

// {

// arr[i]=ran.nextInt(100);

// }

int[] arr={12, 23, 5,

17, 6, 19, 13, 3, 41, 21, 11, 22, 30, 31, 16};

System.out.println("Unsorted

array");

for (int i = 0; i < arr.length; i++)

{

System.out.print(arr[i]+"

");

}

System.out.println();

System.out.println("Sorted

array(insertion sort)");

int[] ins=insertionSort(arr, 0, arr.length);

for

(int i = 0; i < arr.length; i++)

{

System.out.print(ins[i]+"

");

}

System.out.println();

int[]

sorted=bubbleSort(arr);

System.out.println("Sorted array(bubble

sort)");

for (int i = 0; i < arr.length; i++)

{

System.out.print(sorted[i]+" ");

}

}

public static int[]

insertionSort (int[] arr, int a, int b)

{

int[] m=arr.clone();

int t;

int i, j;

for ( i=a; i < b; i++)

{

t = m[i];

for ( j=i-1; j>=a && m[j] >

t; j--)

m[j+1] = m[j];

m[j+1] = t;

}

return m;

}

public static int[]

bubbleSort(int[] arr)

{

int buf;

int[] a=arr.clone();

for (int i =

0; i < a.length-1; i++)

{

for (int j = i+1; j < a.length; j++)

{

if(a[i]>a[j])

{

buf=a[i];

a[i]=a[j];

a[j]=buf;

}

}

}

return

a;

}

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment