Skip to content

Commit

Permalink
fix(7.5): i reaches 2nd-to-last index of Array/ArrayList
Browse files Browse the repository at this point in the history
-  i (the current index) for selection sort doesn't need to reach the last index of the array
  • Loading branch information
101zh committed Jan 9, 2024
1 parent a4fa079 commit 6a6afd0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/Unit7/U7_L5_Activity_One.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class U7_L5_Activity_One {
public static void sortAndPrintReverse(String[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int i = 0; i < arr.length - 1; i++) {
int high = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j].compareTo(arr[high]) > 0) {
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/Unit7/U7_L5_Activity_Two.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

import java.util.ArrayList;

public class U7_L5_Activity_Two
{

public static void selectSortReverse(ArrayList<Integer> list){

for(int i=0; i<list.size(); i++){
int largestIndex=i;
for(int j=i+1; j<list.size(); j++){
if(list.get(j)>list.get(largestIndex)){
largestIndex=j;
public class U7_L5_Activity_Two {

public static void selectSortReverse(ArrayList<Integer> list) {

for (int i = 0; i < list.size() - 1; i++) {
int largestIndex = i;
for (int j = i + 1; j < list.size(); j++) {
if (list.get(j) > list.get(largestIndex)) {
largestIndex = j;
}
}
int temp = list.get(i);

list.set(i, list.get(largestIndex));
list.set(largestIndex, temp);
}

System.out.println(list);

}

}

0 comments on commit 6a6afd0

Please sign in to comment.