Question

In: Computer Science

For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...

For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array passed are non-numeric. Create a main method that demonstrates this generic array receiving numeric arrays and sorting them appropriately. The package name should be genericSort.

Solutions

Expert Solution

CODE IN JAVA:

import java.util.Scanner;

public class Sortings {

static void bubblesort(int arr[]) {

int n = arr.length;

int temp;

boolean swap ;

int swapCount = 0 ;

for(int i=0;i<n;i++) {

swap = true;

for(int j=0;j<n-1;j++) {

if(arr[j]>arr[j+1]) {

temp = arr[j];

arr[j]= arr[j+1];

arr[j+1]=temp;

swap = false;

swapCount += 1 ;

}

}

if(swap)

break;

}

System.out.println("Total number of swappings done by bubblesort:"+swapCount);

}

static void selectionsort(int arr[]) {

int n = arr.length;

int swapCount = 0 ;

// One by one move boundary of unsorted subarray

for (int i = 0; i < n-1; i++)

{

// Find the minimum element in unsorted array

int min_idx = i;

for (int j = i+1; j < n; j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

swapCount += 1 ;

}

  

}

// Swap the found minimum element with the first

// element

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

}

System.out.println("Total number of swappings done by insertionSort:"+swapCount);

}

static void insertionsort(int arr[]) {

int n = arr.length;

int swapCount = 0 ;

for (int i=1; i<n; ++i)

{

int key = arr[i];

int j = i-1;

while (j>=0 && arr[j] > key)

{

arr[j+1] = arr[j];

j = j-1;

swapCount += 1 ;

}

arr[j+1] = key;

}

System.out.println("Total number of swappings done by insertionSort:"+swapCount);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

System.out.print("Enter the size of the array:");

int n = sc.nextInt();

System.out.println("Enter the elements of the array:");

int arr[] = new int[n];

for(int i=0;i<n;i++)

arr[i] = sc.nextInt();

System.out.println("\n1.Bubblesort");

System.out.println("2.Insertionsort");

System.out.println("3.Selectionsort");

System.out.println("\nEnter your choice:");

int choice = sc.nextInt();

switch(choice) {

case 1:

bubblesort(arr);

System.out.println("Now your array is:");

for(int i=0;i<n;i++)

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

System.out.println();

break;

case 2:

insertionsort(arr);

System.out.println("Now your array is:");

for(int i=0;i<n;i++)

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

System.out.println();

break;

case 3:

selectionsort(arr);

System.out.println("Now your array is:");

for(int i=0;i<n;i++)

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

System.out.println();

break;

default:

System.out.println("invalid choice....");

}

}

}

OUTPUT:


Related Solutions

Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both...
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both are O(n^2) however it may be possible to classify one algorithm as being more efficient than the other. You are to discuss which algorithm you feel is the most efficient and in what cases it will be more efficient. Provide any relevant test cases and code to support your belief. Submit a pdf containing your findings and test results along with any relevant code...
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT