Create a generic bubble sort method. Refer to the pseudo code below for a descri
ID: 3752082 • Letter: C
Question
Create a generic bubble sort method. Refer to the pseudo code below for a description of the sorting algorithm. Your method must be able to sort an array of any type of objects that implements the Comparable interface. Create a main method that creates an array of 100 integers. The integers are to be randomly selected, ranging from 0 to 999, inclusive. Also, create an array of 100 doubles. The doubles are to be randomly generated and ranging from 0 to 100. Display the contents of both arrays before sorting and after sorting in ascending order.
for (int k=1;k<list.length; k++){
for (int i=0; i<list.length-k;i++){
if(list[i] >list[i+1])
swap list[i] with list[i+1];
}
}
Explanation / Answer
//GenericSort.java
import java.util.Arrays;
import java.util.Random;
public class GenericSort{
/**
* Method for swapping the value
* @param list
* @param i
* @param j
*/
private static <T> void swap(T[] list, int i, int j) {
if (i != j) {
T temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
/**
* Generic method for sorting array
* who implement the comparable interface
* @param list
*/
public static <T extends Comparable<T>> void bubble_sort_(T[] list) {
//used the given algo
for (int k=1;k<list.length; k++){
for (int i=0; i<list.length-k;i++){
//using the compareTo method from comparable interface
if(list[i].compareTo(list[k])>0) {
swap(list,k,i);
}
}
}
}
public static void main(String[] args) {
Random rand = new Random();
//Array for int and double
Integer intArray[ ] = new Integer[100];
Double doubleArray []= new Double[100];
for(int i = 0;i<100;i++) {
intArray[i] = rand.nextInt(1000);
doubleArray[i] = 0 + (1000 - 0) * rand.nextDouble();
}
System.out.println("Integer Array Before Sorting ...");
System.out.println(Arrays.toString(intArray));
bubble_sort_(intArray);
System.out.println("Integer Array After sorting.....");
System.out.println(Arrays.toString(intArray));
System.out.println(" Double array Before Sorting ....");
System.out.println(Arrays.toString(doubleArray));
bubble_sort_(doubleArray);
System.out.println("Double array after sorting ...");
System.out.println(Arrays.toString(doubleArray));
}
}
//OUTPUT
Integer Array Before Sorting ...
[169, 134, 151, 573, 752, 750, 225, 446, 960, 330, 108, 5, 449, 767, 631, 552, 240, 294, 510, 746, 438, 193, 137, 221, 326, 316, 792, 335, 556, 698, 972, 839, 969, 210, 193, 963, 116, 70, 886, 479, 980, 199, 895, 348, 120, 551, 257, 834, 959, 587, 860, 835, 308, 52, 471, 46, 674, 754, 772, 462, 935, 837, 685, 701, 171, 609, 894, 391, 576, 476, 569, 703, 284, 549, 47, 968, 353, 604, 220, 642, 399, 210, 547, 402, 912, 852, 576, 164, 586, 160, 573, 448, 570, 83, 715, 526, 835, 629, 506, 111]
Integer Array After sorting.....
[5, 46, 47, 52, 70, 83, 108, 120, 134, 137, 151, 164, 169, 193, 193, 199, 210, 210, 220, 225, 240, 257, 294, 308, 316, 326, 335, 348, 438, 446, 449, 462, 471, 510, 551, 552, 573, 587, 631, 674, 698, 750, 752, 767, 834, 839, 886, 959, 963, 972, 980, 969, 960, 895, 860, 835, 792, 754, 772, 746, 935, 837, 685, 701, 556, 609, 894, 479, 576, 476, 569, 703, 391, 549, 330, 968, 353, 604, 284, 642, 399, 221, 547, 402, 912, 852, 576, 171, 586, 160, 573, 448, 570, 116, 715, 526, 835, 629, 506, 111]
Double array Before Sorting ....
[824.5958309666328, 740.147263418049, 420.93001750961855, 640.4962771110416, 544.5562843579405, 493.9722695793446, 541.0554572132304, 122.67560593883509, 68.2573719919748, 733.1614528390099, 297.2214029318605, 968.7754824308788, 240.33119632522238, 986.9354936001541, 977.274091833342, 979.6073630794724, 493.1848317384667, 681.7508766772743, 72.20102082158542, 426.39032339391326, 64.71424509264357, 473.3369174004337, 189.150909493888, 363.75937821611024, 169.1356628460533, 12.744172327370462, 574.1562528614793, 641.9055527519984, 741.7265087162218, 720.4770207019949, 37.19706023916547, 110.57033864400078, 241.72102508717475, 541.0413949421251, 660.8862200592393, 258.8716055343928, 447.4232936068611, 618.0154318645427, 533.1462720498077, 174.5602149558888, 109.29042963660929, 453.96165897660444, 605.2278701000315, 625.8962026392618, 620.1848497345133, 658.9316708042868, 480.1142075977113, 457.3685405215863, 631.4524864356714, 526.6327130469529, 682.9561894780747, 582.3133845529175, 387.58923063948095, 761.0705202577643, 564.2625366996357, 183.34010616537333, 804.0161293285142, 983.0647573025013, 107.07161967035594, 374.4532026545638, 289.2795273734494, 621.7467612220588, 329.5776865845718, 412.8372764003774, 566.5808994023707, 309.1524150127312, 832.3693756401523, 496.30547875095556, 316.0594730824988, 486.35959553137576, 81.63260269956696, 464.1228545969043, 109.20145830042915, 59.20031250358593, 852.8192987849702, 100.90854772264746, 595.0200323353969, 244.19492271897337, 599.2066862404013, 789.0705170619927, 213.49586083570736, 276.6691890390698, 431.66655041621374, 575.0846988170196, 328.8213596841001, 140.5690754992921, 584.3979024824904, 216.12504038169712, 801.262890801254, 572.6913722746824, 240.29739592937437, 910.1814524728186, 844.3239665934358, 114.19476110826288, 52.36822551918641, 435.8473623751007, 970.1190033941479, 172.50542970104522, 636.7961779535639, 635.2683485128294]
Double array after sorting ...
[12.744172327370462, 37.19706023916547, 52.36822551918641, 59.20031250358593, 64.71424509264357, 68.2573719919748, 81.63260269956696, 100.90854772264746, 107.07161967035594, 109.20145830042915, 109.29042963660929, 110.57033864400078, 122.67560593883509, 140.5690754992921, 169.1356628460533, 183.34010616537333, 189.150909493888, 213.49586083570736, 240.33119632522238, 241.72102508717475, 258.8716055343928, 289.2795273734494, 297.2214029318605, 316.0594730824988, 329.5776865845718, 374.4532026545638, 387.58923063948095, 420.93001750961855, 447.4232936068611, 453.96165897660444, 473.3369174004337, 480.1142075977113, 493.9722695793446, 526.6327130469529, 533.1462720498077, 541.0554572132304, 544.5562843579405, 574.1562528614793, 605.2278701000315, 618.0154318645427, 625.8962026392618, 640.4962771110416, 658.9316708042868, 660.8862200592393, 681.7508766772743, 720.4770207019949, 740.147263418049, 741.7265087162218, 968.7754824308788, 979.6073630794724, 986.9354936001541, 977.274091833342, 824.5958309666328, 761.0705202577643, 733.1614528390099, 682.9561894780747, 804.0161293285142, 983.0647573025013, 641.9055527519984, 631.4524864356714, 620.1848497345133, 621.7467612220588, 582.3133845529175, 564.2625366996357, 566.5808994023707, 541.0413949421251, 832.3693756401523, 496.30547875095556, 493.1848317384667, 486.35959553137576, 457.3685405215863, 464.1228545969043, 426.39032339391326, 412.8372764003774, 852.8192987849702, 363.75937821611024, 595.0200323353969, 309.1524150127312, 599.2066862404013, 789.0705170619927, 244.19492271897337, 276.6691890390698, 431.66655041621374, 575.0846988170196, 328.8213596841001, 174.5602149558888, 584.3979024824904, 216.12504038169712, 801.262890801254, 572.6913722746824, 240.29739592937437, 910.1814524728186, 844.3239665934358, 114.19476110826288, 72.20102082158542, 435.8473623751007, 970.1190033941479, 172.50542970104522, 636.7961779535639, 635.2683485128294]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.