Language does not matter really, but I primarily work in Java, SDK 8 Submit your
ID: 3713761 • Letter: L
Question
Language does not matter really, but I primarily work in Java, SDK 8
Submit your analysis only. Write a program that creates three identical arrays, array1, array2, and array3, of 5000 elements. o Sort array1 using bubble sort o Sort array2 using selection sort Sort array3 using insertion sort o o Output the number of comparisons and item assignments made by each sorting algorithm. Creates three identical lists, list1, list2, and list3, of 5000 elements. o You can use the built-in list from Java Sorts list1 using quick sort . Sort list2 using insertion sort Sort list3 using merge sort Outputs the number of comparisons and item assignments made by quick sort and insertion sort and the number of comparisons made by merge sort. After running the program, analyze the resul · Create a document explaining the differences in the number of com Why are there differences in the number of assignments and comparisons? parisons and item assignments for each sorting algorithm. o oExplanation / Answer
Solution:
Note: The implementation of Bubble and Selection sort is given combined togather counting the comparison, I have also attached insertion sort in java and Bubble sort in java as well. It is really hard to club this big project in the specified time, please integrate them and then you can see it by yourself, ask for anything in the comments.
codes:
Insertion and Bubble together.
#include<iostream>
#include <cstdlib>
using namespace std;
int SelectionSort(int *array, int size)
{
int m, n, first, temp;
int count=0;
for (m= size - 1; m > 0; m--)
{
first = 0; //The first element's subscript is initialized here
for (n=1; n<=m; n++) // locate smallest between positions 1 and i.
{
if (array[n] < array[first])
first = n;
}
temp = array[first]; // Swap smallest found with element in position i.
array[first] = array[m];
array[m] = temp;
count++;
}
return count;
}
int BubbleSort(int *array, int size)
{
int m, n;
int temp;
int count=0;
for(m = 1; m < size; m++)
{
for (n=0; n < size-1; n++)
{
if (array[n+1] > array[n])
{
temp = array[n];
array[n] = array[n+1];
array[n+1] = temp;
count++;
}
}
}
return count;
}
int main() {
int array1[20], array2[20];
int size=20;
for (int m=0; m<size; m++) {
array1[m]=array2[m]=rand()%100;
}
int countBubble = BubbleSort(array1, size);
int countSelection = SelectionSort(array2, size);
cout<<"Number of comparisons using bubble sort: "<<countBubble<<" ";
cout<<"Number of comparisons using bubble sort: "<<countSelection<<" ";
cout<<" ";
return 0;
}
Insertion sort in java:
package chegg;
public class InsertionSort {
public static void insertSort(int arr[])
{
int vn = arr.length;
for (int vi=1; vi<vn; ++vi)
{
int pivot = arr[vi];
int vj = vi-1;
while (vj>=0 && arr[vj] > pivot)
{
arr[vj+1] = arr[vj];
vj = vj-1;
}
arr[vj+1] = pivot;
}
}
public static void main(String[] args) {
int[] list = { 6, 4, 1, 2, 3, 8, 11};
insertSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
Bubble sort in java:
public class bubbleSort {
public static <E extends Comparable<E>> void bubblesort(E[] list){
int n = list.length;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (list[j - 1].compareTo(list[j])>0) {
E temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
}
public static void main(String[] args) {
Double[] list = { 6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5};
bubblesort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.