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
85 changes: 79 additions & 6 deletions ArrayMethods.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/********************************************
* AUTHOR: <name>
* COLLABORATORS: <names>
* AUTHOR: Tony Vera
* COLLABORATORS: N/A
* COURSE: CS 111 Intro to CS I - Java
* LAST MODIFIED: <date>
* LAST MODIFIED: 4/6/2025
********************************************/

/********************************************
Expand Down Expand Up @@ -31,32 +31,105 @@
public class ArrayMethods
{
/**DESCRIPTION: */
/*
* Method used to take an array of integers and will output a String of the array as a horizontal list,
* separated by commas and enclosed with curly brackets
*
* @param Array to be converted into a readable String
*
* @return String of desired readable format of array
*/
public static String arrayString(int[] a)
{
return ""; //STUB to keep compiler happy
String arrayDisplay ="{ ";
for(int i =0; i < a.length ; i++)
{
arrayDisplay += a[i];
//The last value of the array will not require a comma
if(i < a.length -1)
{
arrayDisplay += ", ";
}
}
arrayDisplay += " }";
return arrayDisplay;
}

/**DESCRIPTION: */
/*
* Method to swap two elements in an int array given the indexes where the swap should occur.
*
* @param Array with at least two values
* @param value of the two indexes that are to be swapped in the array
*/
public static void swap(int[] array, int a, int b)
{
int valueHolder;
valueHolder = array[a];
array[a]= array[b];
array[b] = valueHolder;

}

/**DESCRIPTION: */
/*
* Method to find the index of the minimum element in the array, starting from that specific index.
* This method loops through the array and return the index of the minimum element, beginning from the start index
* (ignores all earlier indices).
*
* @param array with at least one value.
* @param int value of the desired index used to start to search for the min element in the array.
*
* @return the int value of the index holding the min value in the array
*/
public static int indexOfMin(int[] array, int startIndex)
{
return 0; //STUB to keep compiler happy
int minIndex=startIndex;

for(int i = startIndex; i < array.length - 1; i++)
{
if(array[startIndex] > array[i+1])
{
startIndex= i+1;
minIndex = startIndex;
}
}
return minIndex;
}

/**DESCRIPTION: */
/*
* Method reverses the order of a given array.
*
* @param Array to be reversed
*/
public static void reverse(int[] array)
{

for(int i = 0; i < array.length/2; i++)
{
ArrayMethods.swap(array, i, array.length-1-i);
}
}

/**DESCRIPTION: */
/*
* Method takes an array and sorts it into ascending order (least to greatest)
*
* @param Array to be sorted
*/
public static void selectionSort(int[] array)
{
int minIndex;
for(int i = 0; i < array.length - 1; i++)
{
minIndex = ArrayMethods.indexOfMin(array, i);
if(minIndex >i)
{
ArrayMethods.swap(array, minIndex, i);
}

}
System.out.println(ArrayMethods.arrayString(array));

}

Expand Down
51 changes: 48 additions & 3 deletions Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
/********************************************
* AUTHOR: <name>
* COLLABORATORS: <names>
* AUTHOR: Tony Vera
* COLLABORATORS: N/A
* COURSE: CS 111 Intro to CS I - Java
* LAST MODIFIED: <date>
* LAST MODIFIED: 4/6/2025
********************************************/

public class Main
{
public static void main(String[] args)
{
// Array to test Step 1 & 2
int[] testValues = {12, 16, 26, 42, 53, 77, 84};

// Empty Array to test Step 1 to ensure proper spacing between brackets
int[] emptyTestValues ={};

// Array to test Step 3
int[] minIndexTestValues = {42, 16, 84, 12, 77, 26, 53};

// Array to test Step 4
int[] reverseTestValues = {42, 16, 84, 12, 77, 26, 53};

// Array to test Step 5
int[] selectionTestValues = {42, 16, 84, 12, 77, 26, 53};


System.out.println("Step 1 Test Will print to Console array with the desired format:");
System.out.println(ArrayMethods.arrayString(testValues));
System.out.println(ArrayMethods.arrayString(emptyTestValues));
System.out.println("End of Step 1 Test:\n");

System.out.println("Step 2 Test will demonstrate the swaping of elements in index 0 and index 6 of the array:");
System.out.println(ArrayMethods.arrayString(testValues));
ArrayMethods.swap(testValues, 0, 6);
System.out.println(ArrayMethods.arrayString(testValues));
System.out.println("End of Step 2 Test:\n");

System.out.println("Step 3 Test outputs to the console the array being used to test search\n"+
"for the index holding the min value in the array and the results of \n" +
"the search when the start indices are 0 and 4:");
System.out.println(ArrayMethods.arrayString(minIndexTestValues));
System.out.println(ArrayMethods.indexOfMin(minIndexTestValues, 0));
System.out.println(ArrayMethods.indexOfMin(minIndexTestValues, 4));
System.out.println("End of Step 3:\n");

System.out.println("Step 4 Test ouputs the Array that is to be reveresed and the array once it's reversed:");
System.out.println(ArrayMethods.arrayString(reverseTestValues));
ArrayMethods.reverse(reverseTestValues);
System.out.println(ArrayMethods.arrayString(reverseTestValues));
System.out.println("End of Step 4:\n");

System.out.println("Step 5 Test Outputs the array in intial order and then in ascending order after sorting has occured:");
System.out.println(ArrayMethods.arrayString(selectionTestValues));
ArrayMethods.selectionSort(selectionTestValues);
System.out.println("End of Step 5:\n");

}
}
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=19021378)
# Lab 15 - Selection Sort + Array Methods

**Implement the Selection Sort Algorithm Using Array Methods**
Expand Down
Loading