Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers.
TO Do
Submission
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Sorting.java
public class Sorting {
// global variables to count the number of swaps and comparisons taken by
// each sorting methods
private static int swaps = 0, comparisons = 0;
// method to create and return an array containing size number of random
// doubles
public static double[] generateRandom(int size) {
double array[] = new double[size];
for (int i = 0; i < array.length; i++) {
// generating a number between 0 and 94500
int num = (int) (Math.random() * 94501);
// dividing by 100 to get a double number between 0.0 and 945.00
double x = (double) num / 100.0;
// adding to array
array[i] = x;
}
return array;
}
// method to print the contents of an array, 9 elements at a time
static void print(double array[]) {
for (int i = 0; i < array.length; i++) {
System.out.printf("%6.2f ", array[i]);
if ((i + 1) % 9 == 0) {
System.out.println();
}
}
System.out.println();
}
// method to return a copy of the array passed.
static double[] copyArray(double original[]) {
double[] copy = new double[original.length];
for (int i = 0; i < original.length; i++) {
copy[i] = original[i];
}
return copy;
}
// method to sort numbers using selection sort
static void selectionSort(double[] data) {
// resetting swaps and comparisons to 0
swaps = 0;
comparisons = 0;
int n = data.length;
// using selection sort algorithm to sort elements in data
for (int i = 0; i < n - 1; i++) {
// finding the index of smallest element in the unsorted array
int index_min = i;
for (int j = i + 1; j < n; j++) {
comparisons++;
if (data[j] < data[index_min])
index_min = j;
}
// swapping element at i with element at index_min
double temp = data[index_min];
data[index_min] = data[i];
data[i] = temp;
swaps++;
}
}
// method to sort numbers using bubble sorting
public static void bubbleSort(double[] array) {
// resetting swaps and comparisons to 0
swaps = 0;
comparisons = 0;
int n = array.length;
for (int pass = 1; pass < n; pass++) {
for (int i = 0; i < n - pass; i++) {
if (array[i] > (array[i + 1])) {
// swap elements
double temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swaps++;
}
comparisons++;
}
}
}
// method to sort numbers using insertion sorting
public static void insertionSort(double[] array) {
// resetting swaps and comparisons to 0
swaps = 0;
comparisons = 0;
for (int i = 1; i < array.length; i++) {
double temp = array[i];
int j = i;
comparisons++;
while ((j > 0) && (array[j - 1] > array[j])) {
if (array[j - 1] > array[j]) {
// increasing number of comparisons
comparisons++;
}
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
j--;
// increasing swaps
swaps++;
}
}
}
public static void main(String[] args) {
// generating an aray of 140 doubles
double original[] = generateRandom(140);
// taking a copy
double copy[] = copyArray(original);
// printing array
print(copy);
// performing selection sort, printing array and number of swaps &
// comparisons
System.out.println("Selection Sort\n");
selectionSort(copy);
print(copy);
System.out.println("\n\nNumber of swaps: " + swaps);
System.out.println("Number of comparisons: " + comparisons);
// taking a copy of the original again
copy = copyArray(original);
// performing bubble sort, printing array and number of swaps &
// comparisons
System.out.println("Bubble Sort\n");
bubbleSort(copy);
print(copy);
System.out.println("\n\nNumber of swaps: " + swaps);
System.out.println("Number of comparisons: " + comparisons);
// taking a copy of the original again
copy = copyArray(original);
// performing insertion sort, printing array and number of swaps &
// comparisons
System.out.println("Insertion Sort\n");
insertionSort(copy);
print(copy);
System.out.println("\n\nNumber of swaps: " + swaps);
System.out.println("Number of comparisons: " + comparisons);
}
}
/*OUTPUT*/
556.53 661.73 493.20 254.76 545.33 520.93 17.83 826.65 678.03
762.35 13.66 912.14 174.69 667.91 54.02 31.91 885.21 469.75
496.53 860.40 463.49 114.44 105.20 703.10 597.95 795.34 598.14
728.18 606.84 385.25 126.12 347.10 731.98 587.79 800.63 667.16
81.56 765.71 842.47 42.07 142.79 703.47 450.58 307.40 817.65
29.12 18.87 268.81 310.42 792.37 747.27 679.77 698.50 105.97
592.04 859.19 328.60 87.79 764.09 415.82 733.87 48.65 387.94
798.18 407.93 593.77 283.16 507.61 291.04 66.81 474.24 467.16
209.10 905.86 779.57 15.43 365.66 679.12 449.37 647.33 293.30
322.10 852.51 313.21 145.06 218.95 236.74 398.40 93.34 61.95
592.28 732.21 703.34 782.77 783.33 889.18 220.10 932.56 313.14
830.22 608.23 815.67 78.24 757.45 145.40 797.85 108.18 179.53
586.67 15.62 43.35 943.33 493.35 167.14 303.84 33.13 611.65
893.52 393.92 321.05 508.70 831.28 292.78 124.98 414.50 67.67
218.67 545.83 406.98 665.53 75.16 161.54 488.01 407.75 860.12
23.23 592.24 239.74 569.65 677.60
Selection Sort
13.66 15.43 15.62 17.83 18.87 23.23 29.12 31.91 33.13
42.07 43.35 48.65 54.02 61.95 66.81 67.67 75.16 78.24
81.56 87.79 93.34 105.20 105.97 108.18 114.44 124.98 126.12
142.79 145.06 145.40 161.54 167.14 174.69 179.53 209.10 218.67
218.95 220.10 236.74 239.74 254.76 268.81 283.16 291.04 292.78
293.30 303.84 307.40 310.42 313.14 313.21 321.05 322.10 328.60
347.10 365.66 385.25 387.94 393.92 398.40 406.98 407.75 407.93
414.50 415.82 449.37 450.58 463.49 467.16 469.75 474.24 488.01
493.20 493.35 496.53 507.61 508.70 520.93 545.33 545.83 556.53
569.65 586.67 587.79 592.04 592.24 592.28 593.77 597.95 598.14
606.84 608.23 611.65 647.33 661.73 665.53 667.16 667.91 677.60
678.03 679.12 679.77 698.50 703.10 703.34 703.47 728.18 731.98
732.21 733.87 747.27 757.45 762.35 764.09 765.71 779.57 782.77
783.33 792.37 795.34 797.85 798.18 800.63 815.67 817.65 826.65
830.22 831.28 842.47 852.51 859.19 860.12 860.40 885.21 889.18
893.52 905.86 912.14 932.56 943.33
Number of swaps: 139
Number of comparisons: 9730
Bubble Sort
13.66 15.43 15.62 17.83 18.87 23.23 29.12 31.91 33.13
42.07 43.35 48.65 54.02 61.95 66.81 67.67 75.16 78.24
81.56 87.79 93.34 105.20 105.97 108.18 114.44 124.98 126.12
142.79 145.06 145.40 161.54 167.14 174.69 179.53 209.10 218.67
218.95 220.10 236.74 239.74 254.76 268.81 283.16 291.04 292.78
293.30 303.84 307.40 310.42 313.14 313.21 321.05 322.10 328.60
347.10 365.66 385.25 387.94 393.92 398.40 406.98 407.75 407.93
414.50 415.82 449.37 450.58 463.49 467.16 469.75 474.24 488.01
493.20 493.35 496.53 507.61 508.70 520.93 545.33 545.83 556.53
569.65 586.67 587.79 592.04 592.24 592.28 593.77 597.95 598.14
606.84 608.23 611.65 647.33 661.73 665.53 667.16 667.91 677.60
678.03 679.12 679.77 698.50 703.10 703.34 703.47 728.18 731.98
732.21 733.87 747.27 757.45 762.35 764.09 765.71 779.57 782.77
783.33 792.37 795.34 797.85 798.18 800.63 815.67 817.65 826.65
830.22 831.28 842.47 852.51 859.19 860.12 860.40 885.21 889.18
893.52 905.86 912.14 932.56 943.33
Number of swaps: 5109
Number of comparisons: 9730
Insertion Sort
13.66 15.43 15.62 17.83 18.87 23.23 29.12 31.91 33.13
42.07 43.35 48.65 54.02 61.95 66.81 67.67 75.16 78.24
81.56 87.79 93.34 105.20 105.97 108.18 114.44 124.98 126.12
142.79 145.06 145.40 161.54 167.14 174.69 179.53 209.10 218.67
218.95 220.10 236.74 239.74 254.76 268.81 283.16 291.04 292.78
293.30 303.84 307.40 310.42 313.14 313.21 321.05 322.10 328.60
347.10 365.66 385.25 387.94 393.92 398.40 406.98 407.75 407.93
414.50 415.82 449.37 450.58 463.49 467.16 469.75 474.24 488.01
493.20 493.35 496.53 507.61 508.70 520.93 545.33 545.83 556.53
569.65 586.67 587.79 592.04 592.24 592.28 593.77 597.95 598.14
606.84 608.23 611.65 647.33 661.73 665.53 667.16 667.91 677.60
678.03 679.12 679.77 698.50 703.10 703.34 703.47 728.18 731.98
732.21 733.87 747.27 757.45 762.35 764.09 765.71 779.57 782.77
783.33 792.37 795.34 797.85 798.18 800.63 815.67 817.65 826.65
830.22 831.28 842.47 852.51 859.19 860.12 860.40 885.21 889.18
893.52 905.86 912.14 932.56 943.33
Number of swaps: 5109
Number of comparisons: 5248
Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...
Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...
In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...
Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...
Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble sort, merge sort, quick sort, and radix sort. Your program should test all sort methods for input sizes of 10000, 20000, 30000, 40000, 50000, and 60000. The selection sort, bubble sort, and radix sort should also be tested for input sizes 100000 and 200000. Your program should create the data that is sorted from randomly generated integers and should output the results in a...
Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...
Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...
C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...
**C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...
c++ program that finds the total number of comparisons and total number of moves in insertion sort, bubble sort, selection sort, shell sort, quick sort and merge sort using the same array of numbers.
Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.