Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix this code so that it sequentially sorts the array from right to left numerically from least to largest and prints off each change

Please fix this code so that it sequentially sorts the array from right to left numerically from least to largest and prints off each change in the code this is a java program. I believe the method call to findPos needs to be edited as I don't have any code right now. The sorting algorithm is called a bubble sort

// Course: CPS 151 // Semester: 2022 Spring // Name: Josh Neils // Section: 2 // Assignment: Lab 2 // Purpose: Sorting a list of unsorted arrays. // Date completed: 1/20/2022

import java.util.Random;

public class cps151lab2 {

final static Random rand = new Random(); final static int MAX_SIZE = 10; // amount of storage for the intList static int size = 0; // number of values actually in the intList static int[] intList = new int[MAX_SIZE];

/** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("CPS 151 In-class assignment 2 by Josh Neils"); setRandom(intList, 100); printList(intList, MAX_SIZE, " Array before sorting: "); sort(intList); printList(intList, MAX_SIZE, " Array after sorting: "); } // end main

// prints partially filled array with a legend private static void printList(final int[] arr, final int size, final String legend) { System.out.println(legend); for (int k = 0; k < size; k++) { System.out.print(" " + arr[k]); } System.out.println(); } // end printList

// move items from pos:size-1 one position down (higher subscripts) private static void shiftDown(int[] arr, int size, int pos) { for (int i= 0; i< arr.length; i++) { for (int j=i; j if (arr[i]>arr[j]) { pos=arr[i]; arr[i]=arr[j]; arr[j]=pos; } } } } // end shiftDown

private static void setRandom(int[] arr, final int range) { for (int k = 0; k < arr.length; k++) { arr[k] = rand.nextInt(range); } } // end setRandom

private static void sort(int[] arr) { for (int k = 1; k < arr.length; k++) { // assume arr[0:k-1] sorted int save = arr[k]; // arr[k] may be overwritten later, so save that value int pos = findPos(arr, k, save); // find where to insert save in arr[0:k-1] shiftDown(arr, k, pos); arr[pos] = save; // arr[0:k] is now sorted with k+1 values printList(arr, MAX_SIZE, " After pass " + k); } // end for } // end sort

// find the right place for item to be inserted within arr[0:size-1] private static int findPos(int[] arr, int size, int item) { return 0; // stub, keeps compiler happy } // end findPos }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

Define separation.

Answered: 1 week ago

Question

Compare the current team to the ideal team.

Answered: 1 week ago

Question

a. Do team members trust each other?

Answered: 1 week ago