Question

In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

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 number of swaps it takes to sort the array.

Define method SelectionSort() to implement selection sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.

Now, develop a test method that generates 50 integer values between 0 and 100, store them in an array, and then pass the array to those three sort methods. Document your code and organized your outputs as follows:

Arrays Values:

Bubble Sorted values:

Bubble Sort Swaps:

Insertion Sorted values:

Insertion Sort Swaps:

Selection Sorted values:

Selection Sort Swaps:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN JAVA:

SimpleSort.java file:

public class SimpleSort{
   public static int bubbleSort(int arr[]) {
       int n = arr.length;
       int swaps = 0;
       for (int i = 0; i < n; i++) {
           for (int j = 0; j < n - 1; j++) {
               if (arr[j] > arr[j + 1]) {
                   int temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
                   swaps += 1;
               }
           }
       }
       return swaps;
   }

   public static int insertionSort(int arr[]) {
       int n = arr.length;
       int swaps = 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;
               swaps += 1;
           }
           arr[j + 1] = key;
       }
       return swaps;
   }

   public static int selectionSort(int arr[]) {
       int n = arr.length;
       int swaps = 0;

       for (int i = 0; i < n - 1; i++) {
           int minInd = i;
           for (int j = i + 1; j < n; j++)
               if (arr[j] < arr[minInd])
                   minInd = j;
           int temp = arr[minInd];
           arr[minInd] = arr[i];
           arr[i] = temp;
           swaps += 1;
       }
       return swaps;
   }

   public static void main(String[] args) {
       int arr[] = { 12, 34, 65, 98, 32, 45, 12, 8, 4, 1, 47 };
       System.out.print("Array values:");
       for (int val : arr)
           System.out.print(val + " ");
       System.out.println("");
       int count = bubbleSort(arr);
       System.out.print("Bubble sorted values:");
       for (int val : arr)
           System.out.print(val + " ");
       System.out.println("");
       System.out.println("Bubble sort swaps:" + count);
       int arr1[] = { 12, 34, 65, 98, 32, 45, 12, 8, 4, 1, 47 };
       System.out.print("Array values:");
       for (int val : arr1)
           System.out.print(val + " ");
       System.out.println("");
       count = insertionSort(arr1);
       System.out.print("Insertion sorted values:");
       for (int val : arr1)
           System.out.print(val + " ");
       System.out.println("");
       System.out.println("Insertion sort swaps:" + count);
       int arr2[] = { 12, 34, 65, 98, 32, 45, 12, 8, 4, 1, 47 };
       System.out.print("Array values:");
       for (int val : arr2)
           System.out.print(val + " ");
       System.out.println("");
       count = selectionSort(arr2);
       System.out.print("Selection sorted values:");
       for (int val : arr2)
           System.out.print(val + " ");
       System.out.println("");
       System.out.println("Selection sort swaps:" + count);
   }

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Search & Sort

    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...

  • in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement...

    in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    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...

  • Task is to implement the following algorithms in Assembly language for x86 processor

    Task is to implement the following algorithms in Assembly language for x86 processor1) Insertion sort Demonstrate Sorted Array of 10 elements in Watch Window for each one Running time of each algorithmsample bubble sort code:;----------------------------------------------------------BubbleSort PROC USES eax ecx esi,pArray:PTR DWORD, ; pointer to arrayCount:DWORD ; array size;; Sort an array of 32-bit signed integers in ascending; order, using the bubble sort algorithm.; Receives: pointer to array, array size; Returns: nothing;-----------------------------------------------------------mov ecx,Countdec ecx ; decrement count by 1L1: push ecx ; save outer...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them...

    DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    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...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT