JAVA
I got a function that sorts an array of cars (carLot) by MPG. Can someone modify so that it can sort the array of cars by MPG from lowest to highest instead.
private void selectionSort(ArrayList<Car> list) {
for (int i = 0; i < list.size() - 1; i++) {
// Find the minimum in the list[i..list.length-1]
Car currentMax = list.get(i);
int currentMaxIndex = i;
for (int j = i + 1; j < list.size(); j++) {
if (currentMax.getMPG() < list.get(j).getMPG()) {
currentMax = list.get(j);
currentMaxIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMaxIndex != i) {
list.set(currentMaxIndex, list.get(i));
}
list.set(i, currentMax);
}
}Complete java program:
import java.util.ArrayList;
public class Car
{
private int MPG;
//constructor
Car(int MPG)
{
this.MPG=MPG;
}
public Car()
{
}
//getter and setter methods
public int getMPG()
{
return MPG;
}
public void setMPG(int mPG)
{
MPG = mPG;
}
private void selectionSort(ArrayList<Car> list)
{
for (int i = 0; i < list.size() - 1; i++)
{
// Find the minimum in the list[i..list.length-1]
Car currentMax = list.get(i);
int currentMaxIndex = i;
for (int j = i + 1; j < list.size(); j++)
{
//just make here greater than(>) symbol
if (currentMax.getMPG() > list.get(j).getMPG())
{
currentMax = list.get(j);
currentMaxIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMaxIndex != i)
{
list.set(currentMaxIndex, list.get(i));
}
list.set(i, currentMax);
}
//loop to print the MPG from lowest to highest instead.
for(int i=0;i<list.size();i++)
System.out.println(list.get(i).MPG);
}
//driver method to test the car
public static void main(String[] args)
{
//creating arrayList of Cars
ArrayList<Car> carsList= new ArrayList<Car>();
//adding Cars objects to list
carsList.add(new Car(120));
carsList.add(new Car(180));
carsList.add(new Car(150));
carsList.add(new Car(110));
carsList.add(new Car(160));
//calling the method
Car obj = new Car();
obj.selectionSort(carsList);
}
}
Output:
110
120
150
160
180
JAVA I got a function that sorts an array of cars (carLot) by MPG. Can someone...
I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...
b) Prove, using induction, that the following SomeSort function sorts the part of an array from index first to index last in ascending order. You only need to apply induction to the outer for loop. The swap function used in the code below is the same one from part a. (20 marks) 1 void Some Sort (int arr [], int first, int last) { for (int i = first; i < last; i++) { int minpos = i; for (int...
This is the question:
These are in Java format.
Comments are required on these two Classes (Student.java
and StudentDemo.java) all over the coding:
Provide proper comments all over the
codings.
---------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
class Student {
private String name;
private double gradePointAverage;
public Student(String n , double a){
name = n;
gradePointAverage = a;
}
public String getName(){
return name;
}
public double getGradePointAverage(){
return gradePointAverage;
}
...
please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following 1) print out the original data in the file without doing anything to it. 2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...
Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner; public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1) bubbleSort(array); if (ch==2) insertion(array); if (ch==3) Selection(array); if (ch==4) break; print(array); System.out.println(); } }...
I am currently using eclipse to write in java.
A snapshot of the output would be greatly appreciated to verify
that the program is indeed working. Thanks in advance for both your
time and effort.
Here is the previous exercise code:
/////////////////////////////////////////////////////Main
/*******************************************
* Week 5 lab - exercise 1 and exercise 2: *
* ArrayList class with search algorithms *
********************************************/
import java.util.*;
/**
* Class to test sequential search, sorted search, and binary search
algorithms
* implemented in...
Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { int temp = numbers[i]; /* put numbers[i] somewhere to keep it safe */ numbers[i] = numbers[j]; /* put numbers[j] at index i */ numbers[j] = temp; /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...
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...
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....