I'm new to java programming and trying to write a function that sorts Arrays. I know my code is pretty robust for such a simple task, but it's the first way I thought about sorting, so I just went with it. My logic is to take an array, make an array of the same length with all 0's. Then use a for loop to find the minimum of the unsorted array, place it at the beginning of the new array, and then replace the min of the unsorted array with the max of the unsorted array until the entire array is all maxes and I've filled the to be sorted array. I'll hand trace a few examples below because it's a little hard to explain:
-Starting Array:[2, 3, 1, 4]
-1st Execution: [2, 3, 1, 4] [0, 0, 0, 0]
-2nd: [2, 3, 4, 4] [1, 0, 0, 0]
-3rd: [4, 3, 4, 4] [1, 2, 0, 0]
-4th: [4, 4, 4, 4] [1, 2, 3, 0]
-5th: [4, 4, 4, 4] [1, 2, 3, 4]
I have written code that returns the min of an array, finds the index of the min, and returns the max of the array. My though was to continue this process and keep a counter of how many time the for loop has found the min and then stop when it is equal to the array length-1. I am running into problems in my last method - sortMe - because my return won't compile and the error reads:
Error: incompatible types
found : int[]
required: java.util.Arrays
I've Attached all of my code below:
import java.util.Arrays;
public class Homework4 {
public static void main (String[] args) {
int[] a = {20,2,5};
System.out.println(Homework4.minArray(a));
System.out.println(Homework4.maxArray(a));
System.out.println(Homework4.minIndex(a));
System.out.println( Arrays.toString(a));
}
/* 1 This method will mimic the sort methond for Arrays
* It will be called sortMe and will take in an array and produce a sorted array
* In order to do this I will also create two methods: min and max
* The numbers in the array will be of type Int
* Homework4.sortMe([0 , 3, 4, 2, 1, 7]) -> [0 , 1, 2, 3, 4, 7]
* Homework4.sortMe([0]) -> [0]
* Homework4.sortMe([3, 8, 2, 14, 1)] -> [1, 2, 3, 8, 14]
* Template:*/
public static int minArray (int[] x) { //Produces the Minimum of an Array
int minVal = x[0];
for (int i = 0; i < (x.length - 1); i = i + 1) {
if( x[i] < minVal) {
minVal = x[i];}
}
return minVal;}
public static int minIndex (int[] x) { //Returns the index of the Minimum
int minVal = x[0];
int index = 0;
for (int i = 0; i < (x.length - 1); i = i + 1) {
if( x[i] < minVal) {
minVal = x[i];
index = i;}
}
return index;}
public static int maxArray (int[] x) { //Produces the Maximum of an Array
int maxVal = x[0];
for (int i = 0; i < (x.length - 1); i = i + 1) {
if( x[i] > maxVal) {
maxVal = x[i];}
}
return maxVal;}
public static Arrays sortMe (int[] x) { //Sorts an Array
int[] sortedArray = new int [x.length];
int minIterations = 0;
while (minIterations < x.length-1) {
for(int i = 0; i < x.length-1; i = i +1){
sortedArray[i] = Homework4.minArray(x);
x[Homework4.minIndex(x)] = Homework4.maxArray(x);
minIterations++;
}}
return sortedArray; }
}
Thank you!
public static Arrays sortMeYour method says it returns typeArraysbut you try to return typeint[].for(int i = 0; i < x.length-1; i = i +1)with this condition you will never get the last indexed value in array