CSCE145 F2026 04 Arrays Part 02
Watch on YouTube →
Overview
This CSCE145 lesson explains how Java arrays store fixed-size collections of one type, then demonstrates searching and sorting array values. It covers linear search and maximum-value tracking, implements selection sort with index tracking and temporary-variable swaps, and implements bubble sort with adjacent comparisons and repeated passes until no swaps occur.
Key takeaways
- Java arrays have a fixed size after construction, so the program must know the requested length before creating an array with `new int[size]`.
- A reliable maximum search starts with an actual array element such as `a[0]`; an arbitrary initial value can incorrectly become the reported maximum even if it is not present in the array.
- Selection sort records the index of the smallest value in the unsorted region, then swaps that value into the current position.
- Bubble sort can stop once an entire pass makes no swaps, because that pass confirms adjacent values are already ordered.
- When bubble sort compares `a[i]` with `a[i + 1]`, the loop must stop before the final index; using `i < a.length - 1` prevents an index-out-of-bounds error.
Chapters
0:00
Java Arrays: Fixed-Size Containers and Initialization
- A Java array holds values of one type in a fixed-size block; its declared identifier refers to the array object.
- Create an array with `new` and a specified size, such as `new int[size]`, or initialize known values in curly braces.
- Array indices begin at 0: an array of five integers has valid indices 0 through 4.
3:00
Linear Search and Finding an Array Maximum
- Use a `for` loop to inspect each array element when searching for a target value; track whether it was found with a Boolean such as `found`.
- To find a maximum, initialize the recorded maximum to an existing array value, such as `a[0]`, rather than an arbitrary number.
- Compare each later element with the current maximum and update it when a larger value appears; for `[0, 1, 2, 3, 4]`, the result is `4`.
7:50
Selection Sort: Repeatedly Place the Next Smallest Value
- Selection sort uses outer index `i` to mark the position for the next smallest value and searches the remaining array for that value.
- Track the candidate position with `smallestIndex`, initially set to `i`, and scan later indices with `j`.
- After each scan, swap the candidate into position `i` if a smaller value was found; repeat until the array is in ascending order.
11:00
Implementing Selection Sort in a Java Console Program
- The program uses `Scanner` with `System.in` to ask for an array size, rejects sizes less than or equal to zero, and reads one integer per index.
- Nested loops find the minimum remaining value: `i` marks the destination and `j` scans from `i + 1` to `a.length`.
- A temporary variable preserves `a[i]` while swapping it with `a[smallestIndex]`; a test with `5, 4, 3, 2, 1` prints `1, 2, 3, 4, 5`.
20:20
Bubble Sort: Swap Neighbors Until a Pass Makes No Changes
- Bubble sort compares adjacent values and swaps them when `a[i] > a[i + 1]`, moving larger values toward the end of the array.
- A Boolean `hasSwapped` controls a `while` loop: reset it to `false` at each pass and set it to `true` whenever a swap occurs.
- The inner loop stops at `a.length - 1` so that accessing `a[i + 1]` does not exceed the array’s last valid index.
- The Java example sorts `5, 4, 3, 2, 1` into ascending order and shows that separate classes with `main` methods can coexist in one project.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.