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

  • 2. Write a Java program to implement Bucket Sort and write a driver to test it....

    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 this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...

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

  • Programming: Programmatically generate three type of arrays (or vectors) of size 10,000: an array that is...

    Programming: Programmatically generate three type of arrays (or vectors) of size 10,000: an array that is sorted in ascending order, a reversed array (an array that is sorted in descending order), and a random array. • Programmatically sort the three arrays using bubble sort, selection sort, insertion sort, shell sort, merge sort, and quick sort (the regular one and the improved one ) algorithms. For each sorting algorithm, calculate the number of exchanges (or shifts) and the number of comparisons...

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