From 689a4d441403c3d74553f90481b15dbd7b6062ac Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Wed, 21 May 2025 00:39:56 +0000 Subject: [PATCH 1/3] Setting up GitHub Classroom Feedback From ae075e2828b838be96ecc3befe9abcdfdf83dd65 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Wed, 21 May 2025 00:39:58 +0000 Subject: [PATCH 2/3] add online IDE url --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8f88a51..92aa0ab 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Open in Codespaces](https://classroom.github.com/assets/launch-codespace-2972f46106e565e64193e422d61a12cf1da4916b45550586e14ef0a7c637dd04.svg)](https://classroom.github.com/open-in-codespaces?assignment_repo_id=19591134) # Lab 15 - Selection Sort + Array Methods **Implement the Selection Sort Algorithm Using Array Methods** From 0625739041e796d90c02fed9b9365055fb0b4610 Mon Sep 17 00:00:00 2001 From: michllina Date: Wed, 21 May 2025 20:42:06 +0000 Subject: [PATCH 3/3] Sorted arrays in various ways! --- ArrayMethods.java | 80 ++++++++++++++++++++++++++++++++++++++--------- Main.java | 33 +++++++++++++++++-- 2 files changed, 96 insertions(+), 17 deletions(-) diff --git a/ArrayMethods.java b/ArrayMethods.java index 1c0c553..03fad1c 100644 --- a/ArrayMethods.java +++ b/ArrayMethods.java @@ -1,8 +1,8 @@ /******************************************** -* AUTHOR: -* COLLABORATORS: +* AUTHOR: Michelle +* COLLABORATORS: None * COURSE: CS 111 Intro to CS I - Java -* LAST MODIFIED: +* LAST MODIFIED: 5/20/25 ********************************************/ /******************************************** @@ -12,7 +12,6 @@ * Collection of useful methods for int arrays. ********************************************* * ALGORITHM: -* TODO: ********************************************* /* UML CLASS DIAGRAM: @@ -30,34 +29,87 @@ public class ArrayMethods { - /**DESCRIPTION: */ + /**DESCRIPTION: + * Returns Arrays contents in String form + * + * @param a int[] of values, must be >= 1 length + * + * @return array contents in String, separated by commas + */ + public static String arrayString(int[] a) { - return ""; //STUB to keep compiler happy + String result = "{ " + a[0]; + //concotenates on each int value + for(int i = 1; i < a.length; i++){ + result += ", " + a[i]; + } + result += " }"; + + return result; } - /**DESCRIPTION: */ + /**DESCRIPTION: + * Swaps two values in the array at the given indices + * + * @param array int[] that will be modified + * @param a index of first element to swap (must be valid index!) + * @param b index of second element to swap (must be valid index!) + */ public static void swap(int[] array, int a, int b) { - + int temp = array[a]; + array[a] = array[b]; + array[b] = temp; } - /**DESCRIPTION: */ + /**DESCRIPTION: + * Finds index of min value of an array at start index + * + * @param array int[] to search + * @param start index to begin search for min + * + * @return index of min value, from start index to end of array + */ + public static int indexOfMin(int[] array, int startIndex) { - return 0; //STUB to keep compiler happy + int minValue = array[startIndex]; + int minIndex = startIndex; + + for(int i = startIndex; i < array.length; i++){ + if(minValue > array[i]) { + minValue = array[i]; + minIndex = i; + } + } + return minIndex; } - /**DESCRIPTION: */ + /**DESCRIPTION: + * + * Reverses contents of array + * + * @param int[] array will be changed into reverse order + */ public static void reverse(int[] array) { - + for(int i = 0; i < array.length/2; i++){ + ArrayMethods.swap(array, i, array.length-1-i); + } } - /**DESCRIPTION: */ + /**DESCRIPTION: + * Sorts array in ascending order (least to greatest), using selection sort algorithm + * + * @param array int[] + */ public static void selectionSort(int[] array) { - + for(int i = 0; i < array.length-1; i++){ + int indexMinValue = ArrayMethods.indexOfMin(array, i); + ArrayMethods.swap(array, i, indexMinValue); + } } } \ No newline at end of file diff --git a/Main.java b/Main.java index 3990323..2b3371f 100644 --- a/Main.java +++ b/Main.java @@ -1,14 +1,41 @@ /******************************************** -* AUTHOR: -* COLLABORATORS: +* AUTHOR: Nichelle Mendoza +* COLLABORATORS: None * COURSE: CS 111 Intro to CS I - Java -* LAST MODIFIED: +* LAST MODIFIED: 5/20/25 ********************************************/ public class Main { public static void main(String[] args) { + int[] values = { 12, 16, 26, 42, 53, 77, 84 }; + System.out.println("TEST array contents " + + ArrayMethods.arrayString(values)); + + + ArrayMethods.swap(values, 0, 6); + + System.out.println("TEST: array after swap of 12 and 84 = " + + ArrayMethods.arrayString(values)); + + int[] numbers = {42, 16, 84, 12, 77, 26, 53, 96}; + int minIndex1 = ArrayMethods.indexOfMin(numbers, 0); + int minIndex2 = ArrayMethods.indexOfMin(numbers, 4); + + System.out.println("TEST: min indices should be 3 and 5, we got: " + minIndex1 + " and " + minIndex2); + + System.out.println("TEST: array before reversal = " + ArrayMethods.arrayString(numbers)); + + ArrayMethods.reverse(numbers); + + System.out.println("TEST: array after reversal = " + ArrayMethods.arrayString(numbers)); + + System.out.println("TEST: array before sorting = " + ArrayMethods.arrayString(numbers)); + + ArrayMethods.selectionSort(numbers); + + System.out.println("TEST: array after sorting = " + ArrayMethods.arrayString(numbers)); } } \ No newline at end of file