java code please Implement (or use your code from previous practice and/or labs)
ID: 3812233 • Letter: J
Question
java code please
Implement (or use your code from previous practice and/or labs) selection, bubble, and insertion sort. For each algorithm, count the number of times it loops (you can make the sort methods return an integer with this count). Call each sorting algorithms on the following types of arrays: an array of 10 integers already sorted into ascending order an array of 10 integers in the opposite order an array of 10 random integers between 0 and 99 an array of 100 integers already sorted into ascending order an array of 100 integers in the opposite order an array of 100 random integers between 0 and 99 Create a table of the number of times each algorithm looped for each type of array, save the table in a PDF document, and upload that document to Pilot along with your code.Explanation / Answer
Code:-
import java.util.*;
import java.lang.*;
import java.io.*;
class Prog1
{
static int bubbleSort(int arr[]) {
int count=0;
int len = arr.length;
int k;
for (int m = len; m >= 0; m--) {
for (int i = 0; i < len - 1; i++) {
k = i + 1;
if (arr[i] > arr[k]) {
count++;
int temp;
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
}
return count++;
}
static int insertionSort(int[] arr){
int len=arr.length;
int temp;
int count=0;
for (int i = 1; i < len; i++) {
for(int j = i ; j > 0 ; j--){
if(arr[j] < arr[j-1]){
count++;
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
}
return count;
}
static int selectionSort(int[] arr){
int count=0;
int len=arr.length;
for (int i = 0; i < len - 1; i++)
{
int index = i;
for (int j = i + 1; j < len; j++){
if (arr[j] < arr[index]){
count++;
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return count;
}
public static void main (String[] args) throws java.lang.Exception
{
Prog1 obj=new Prog1();
int[] arr={0,1,2,3,4,5,6,7,8,9};
int[] c_arr=new int[10];
int count_bubble;
int count_insert;
int count_selection;
System.out.println(" "+" "+" "+" "+"selection"+ " "+"bubble"+" "+"insertion");
System.arraycopy(arr, 0, c_arr, 0, c_arr.length);
count_selection=obj.selectionSort(c_arr);
System.arraycopy(arr, 0, c_arr, 0, c_arr.length);
count_insert=obj.insertionSort(c_arr);
System.arraycopy(arr, 0, c_arr, 0, c_arr.length);
count_bubble=obj.bubbleSort(c_arr);
System.out.println("10"+" "+"sorted"+" "+count_selection+" "+count_bubble+" "+count_insert);
int[] rarr={9,8,7,6,5,4,3,2,1,0};
System.arraycopy(rarr, 0, c_arr, 0, c_arr.length);
count_selection=obj.selectionSort(c_arr);
System.arraycopy(rarr, 0, c_arr, 0, c_arr.length);
count_insert=obj.insertionSort(c_arr);
System.arraycopy(rarr, 0, c_arr, 0, c_arr.length);
count_bubble=obj.bubbleSort(c_arr);
System.out.println("10"+" "+"opposite"+" "+count_selection+" "+count_bubble+" "+count_insert);
int[] ran_arr={5,7,6,1,8,2,3,9,0,4};
System.arraycopy(ran_arr, 0, c_arr, 0, c_arr.length);
count_selection=obj.selectionSort(c_arr);
System.arraycopy(ran_arr, 0, c_arr, 0, c_arr.length);
count_insert=obj.insertionSort(c_arr);
System.arraycopy(ran_arr, 0, c_arr, 0, c_arr.length);
count_bubble=obj.bubbleSort(c_arr);
System.out.println("10"+" "+"random"+" "+count_selection+" "+count_bubble+" "+count_insert);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.