Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 66 additions & 14 deletions ArrayMethods.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/********************************************
* AUTHOR: <name>
* COLLABORATORS: <names>
* AUTHOR: Michelle
* COLLABORATORS: None
* COURSE: CS 111 Intro to CS I - Java
* LAST MODIFIED: <date>
* LAST MODIFIED: 5/20/25
********************************************/

/********************************************
Expand All @@ -12,7 +12,6 @@
* Collection of useful methods for int arrays.
*********************************************
* ALGORITHM:
* TODO: <Pseudocode for selection sort here>
*********************************************

/* UML CLASS DIAGRAM:
Expand All @@ -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);
}
}

}
33 changes: 30 additions & 3 deletions Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
/********************************************
* AUTHOR: <name>
* COLLABORATORS: <names>
* AUTHOR: Nichelle Mendoza
* COLLABORATORS: None
* COURSE: CS 111 Intro to CS I - Java
* LAST MODIFIED: <date>
* 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));
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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**
Expand Down