The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Hi there, Ive been stuck on this bubblesort java project for quite some time now.. I have attached the word document with the assignment as well as my java document. I seem to be having problems getting my return statement to match what my professor wants in the assignment.
thanks!
Â
COSC 111 – Assignment7Bubble SortIn this assignment you will be sorTng an array of numbers using the bubble sort algorithm.Youmust be able to sort both integers and doubles, and to do this you must overload a method.Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be outof order, you swap those two numbers.±his can be done by looping unTl there are no more swaps being made, or using a nested forloop, meaning the array of length n is checked n2Tmes.Sample Run #1Please enter the # of numbers to be sorted: 4Enter 1 for int's or 2 for doubles: 2Enter number: 4Enter number: 3Enter number: 2Enter number: 13.0, 4.0, 2.0, 1.03.0, 2.0, 4.0, 1.02.0, 3.0, 4.0, 1.02.0, 3.0, 1.0, 4.02.0, 1.0, 3.0, 4.01.0, 2.0, 3.0, 4.0
import java.util.Scanner;public class BubbleSort{public static void main(String[]args ){Scanner input = new Scanner(System.in);System.out.print("Please enter the # of numbers to be sorted:");int num1 = input.nextInt();System.out.print("Enter 1 for int's or 2 for doubles:");int num2 = input.nextInt();int[] arrayInt;double[] arrayDouble;if(num2 == 1){arrayInt = new int[num1];for(int x = 0; x < num1; x++){System.out.print("Enter a number:");arrayInt[x] = input.nextInt();}bubbleSort(arrayInt);System.out.print(sortArray(arrayInt));}if(num2 == 2){arrayDouble = new double[num1];for(int x = 0; x < num1; x++){System.out.print("Enter a number:");arrayDouble[x] = input.nextDouble();}}}public static int sortArray(int[] arrayInt){int temp = 0;for(int x = 0; x < arrayInt.length; x++){for(int y = 1; y < arrayInt.length - x; y++){if(arrayInt[y - 1] > arrayInt[y]){temp = arrayInt[y - 1];arrayInt[y - 1] = arrayInt[y];arrayInt[y] = temp;}return arrayInt[y - 1];}}return temp;}}
Attachments: