CSCE145 F2026 04 Arrays Part 02
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
- 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.
- 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`.
- 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.
- 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`.
- 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.
Transcript excerpt
0:00 Hey everyone, how go? Welcome back to computer science 145. Today we're going to continue to discuss arrays. In a previous lecture, we described this foundational data structure called an array. These are a collection of variables all of the same type. You can think of them as a contiguous block of memory. The size of an array has to be specified initially and also arrays cannot be resized in memory once again because they're a contiguous block of memory. Now inside of Java, arrays are considered to be a special kind of object type. They're a container. So much like other class or object types, the identifier only contains the reference. In other words, the memory
0:48 address to the contents of the array. Now over to the side here is the syntax for how to create an array. Just like other variables, we first must declare the array and we use the square brackets in order to denote that this is going to be an array of this type. When we initialize an array, we have to construct it in memory. This is why we use the reserved word new followed by the type and then the size inside the square brackets. Again, the size has to be specified as an array is a contiguous block of memory. Now, there is an alternate version for how to declare an array. We can both declare it and initialize the values all
1:37 at the same time. If we know the values, we can start out by using the type, the square brackets denoting that this is an array, then the identifier followed by the assignment and each one of the values inside a curly braces like this. Once again, curly braces denote the body of something. And this is the body of
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.