Question

  Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

  1.   Given a bubble sort and the following array:

[10,7,19,5,16]

What are the values after the first iteration?

  1. Given an insertion sort and the following array:

[50,5,35,70,6]

What does the array look like after the first insertion:

  

  1. Fill in the missing code for InsertionSort method

// Insertion Sort method

//sorts an array of Auto objects

public static void insertionSort(Auto [] arr)

{

Auto temp;

int j;

for (int i=1; i<arr.length; i++)

{

    j=i;

    temp=arr[i];

               while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)

                    {

                      arr[j] = arr[j-1];

                      j--;

                    }//end while

                 arr[j] = // your code goes here;

}//end for loop

}//end method

  1.   Write the code for doing a selection sort on an array; test your code and show a screenshot of it working – you must output values of an array before and after the selection sort to prove it is working. The array must have at least 5 values.

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

MISSING CODE FOR INSERTIONSORT METHOD: (A SMALL CHANGE IN CODE)

// Insertion Sort method

//sorts an array of Auto objects

public static void insertionSort(Auto [] arr)

{

Auto temp;

int j;

for (int i=1; i<arr.length; i++)

{

    j=i;

    temp=arr[i];

               while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)

                    {

                      arr[j] = arr[j-1];

                      j--;

                    }//end while

                 arr[j] = temp; // your code goes here;

}//end for loop

}//end method

SELECTION SORT CODE :

public class SelectionSort
{
   void sort(int arr[])
   {

       for (int i = 0; i < arr.length-1; i++)
       {
           int minIndex = i;
           for (int j = i+1; j < arr.length; j++)
               if (arr[j] < arr[minIndex])
                   minIndex = j;
           int temp = arr[minIndex];
           arr[minIndex] = arr[i];
           arr[i] = temp;
       }
   }

  
   public static void main(String args[])
   {
       SelectionSort ob = new SelectionSort();
       int arr[] = {50,5,35,70,6,66,3};
       System.out.println("Before sorting array");
       for(int j=0; j<arr.length; j++)
       {
       System.out.print( arr[j] + " ");
       }
       ob.sort(arr);
       System.out.println("\nAfter sorting array");
       for(int j=0;j<arr.length;j++)
       {
       System.out.print( arr[j] + " ");
       }
   }
}

Screenshot: Selection Sort

Add a comment
Know the answer?
Add Answer to:
  Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...
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
  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

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

  • Question 3 QUESTION //Sort the array arr[] for (int i = 0; i< arr.length - 1;...

    Question 3 QUESTION //Sort the array arr[] for (int i = 0; i< arr.length - 1; i++) { for (int j = 1; i < arr.length - i; j++) { if (array[i-1] > array[i]) { // swap (j-1, ) }//if }//for i }//for i In the array= 19 7 23 12 4 8 17 158, after the completion of the outer iteration #4 , the array becomes = Answer

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++)...

    QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++) { //outer int index = i; for (int j = i + 1; j < arr.length; i++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; }//for i In the array= 16 13 15 14 19 24 9 3, the index of the smallest number at the outer iteration 4 = Answer

  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

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

  • Student Name Student ID CS209 Data Structures and Algorithms - Quiz 1/2e Friday, Feb 22, 2019...

    Student Name Student ID CS209 Data Structures and Algorithms - Quiz 1/2e Friday, Feb 22, 2019 1. (25 points) Here is an array of five integers: 5,3,4,2,1 Please draw this array after EACH iteration of the large loop (outer loop) in a Bubble sort (sorting smallest to largest). (25 points) Here is an array of five integers: 5,3,4,2,1 Please draw this array after EACH iteration of the large loop (outer loop) in a Selection sort (sortin from smallest to largest)....

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