Hey all,
I have an assignment that was assigned for the week that I could use some help on (not asking for someone to do it for me, just help with the step-by-step). Below is the requirements:
1) Create a new Java Project named whatever you'd like in your existing Eclipse Workspace.
2) In this project create a class named SortIntArray in the com.abc.sort package.
3) Paste in the code below overwriting everything in your initial SortIntArry.java file.
4) Except for the sort(int[]) method, all the other methods are complete as is and should NOT be altered.
5) The only method to change is sort(int[]). Within that method, write code to sort the int[] in ascending order. You cannot call out to other methods or classes from within the sort(int[]) method. You cannot use Arrays.sort(int).
6) When you have correctly written the body of the sort(int[]) method, your output will look like this:
Original int[]: 5, 4, 3, 2, 1
after sort: 1, 2, 3, 4, 5
----------
Original int[]: 1, 2, 3, 4, 5
after sort: 1, 2, 3, 4, 5
----------
Original int[]: 5, 5, 4, 4, 3, 3, 2, 2, 1, 1
after sort: 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
----------
Original int[]: 9, 7, 5, 3, 1, 8, 6, 4, 2
after sort: 1, 2, 3, 4, 5, 6, 7, 8, 9
----------
Original int[]: 10, 5, 6, 1, 4, 1, 5, 5, 9, 2
after sort: 1, 1, 2, 4, 5, 5, 5, 6, 9, 10
----------
7) Here is the code that you should paste into your SortIntArray.java file as a starting point for this assignment:
Here is the code that you should paste into your SortIntArray.java file as a starting point for this assignment:
package com.abc.sort;
public class SortIntArray {
// this method needs your code to be added to work
public static void sort(int[] list) {
// Use the Bubble Sort algorithm to sort. Do NOT use any external
// utilities to sort (such as Arrays.sort(int[])). Do not call
// any other methods from within this method.
//
// When using Bubble Sort, after each pass, an additional int
// at the end is in the correct position, so we don't have to
// compare it again (see the example below).
//
// For example, if we had an array with 5 int's in it, we'll need
// 4 passes, like this:
// Pass 1:
// compare list[0] and list[1] and swap if out of order
// compare list[1] and list[2] and swap if out of order
// compare list[2] and list[3] and swap if out of order
// compare list[3] and list[4] and swap if out of order
// (now, list[4] has the largest, no need to look at it again)
// Pass 2:
// compare list[0] and list[1] and swap if out of order
// compare list[1] and list[2] and swap if out of order
// compare list[2] and list[3] and swap if out of order
// (now, list[3] has the second largest, no need to look at it again)
// Pass 3:
// compare list[0] and list[1] and swap if out of order
// compare list[1] and list[2] and swap if out of order
// (now, list[2] has the third largest, no need to look at it again)
// Pass 4:
// compare list[0] and list[1] and swap if out of order
// (now, list[1] has the fourth largest, and list[0] has the smallest)
//
// And again with specific data to further illustrate the example:
// 3, 5, 4, 3, 1 (initial state, 5 int's -> 4 passes)
// Pass 1:
// 3, 5, 4, 3, 1 (compared 3 and 5, no swap needed)
// 3, 4, 5, 3, 1 (compared 5 and 4, and swapped them)
// 3, 4, 3, 5, 1 (compared 5 and 3, and swapped them)
// 3, 4, 3, 1, 5 (compared 5 and 1, and swapped them)
// now 5 is in the right position
// Pass 2:
// 3, 4, 3, 1, 5 (compared 3 and 4, no swap needed)
// 3, 3, 4, 1, 5 (compared 4 and the second 3, and swapped them)
// 3, 3, 1, 4, 5 (compared 4 and 1, and swapped them)
// now 4 and 5 are in the right positions
// Pass 3:
// 3, 3, 1, 4, 5 (compared 3 and 3, no swap needed)
// 3, 1, 3, 4, 5 (compared the second 3 and 1, and swapped them)
// now 3, 4, and 5 are in the right positions
// Pass 4:
// 1, 3, 3, 4, 5 (compared 3 and 1, and swapped them)
// Now all done!
//
// your code goes here -------------
}
// this method is complete, do not alter it
public static String format(int[] set) {
StringBuilder sb = new StringBuilder();
for ( int i = 0; i < set.length; i++ ) {
if ( i > 0 ) {
sb.append(", ");
}
sb.append(set[i]);
}
return sb.toString();
}
// this method is complete, do not alter it
public static void process(int[] set) {
System.out.println("Original int[]: " + format(set));
sort(set);
System.out.println(" after sort: " + format(set));
System.out.println("----------");
}
// this method is complete, do not alter it
public static void main(String[] args) {
process(new int[] { 5, 4, 3, 2, 1 });
process(new int[] { 1, 2, 3, 4, 5 });
process(new int[] { 5, 5, 4, 4, 3, 3, 2, 2, 1, 1 });
process(new int[] { 9, 7, 5, 3, 1, 8, 6, 4, 2 });
process(new int[] { 10, 5, 6, 1, 4, 1, 5, 5, 9, 2 });
}
}
I just need help figuring out bubble sort algorithm, as I don't have any examples in my text and I missed my last class due to vehicle not starting.
I appreciate any help!