Docsity

Notes on Arrays - Computer Programing and Problem Solving | CS 111, Study notes of Computer Science

Partial preview of the text

Download Notes on Arrays - Computer Programing and Problem Solving | CS 111 and more Study notes Computer Science in PDF only on Docsity!

Arrays

CS111 Lecture 17

Tuesday, April 4, 2000

Arrays: Motivation

  • Lists are great for representing sequences, but can only

efficiently access elements in order from front to back.

  • We often want an indexed sequence where we can

efficiently access an element at any index.

Array Diagrams

Array Operations

  • Return contents of variable an index:

a[2] โ†’ 17 if (b[1]) { then part } else { else part } c[0].x โ†’ 5

Notes:

โ™ฆ a[2] is pronounced โ€œa sub 2โ€

โ™ฆ ArrayIndexOutOfBoundsException for indices out of range

  • Update contents of variable at an index:

a[0] = a[1] + a[2]; (* a[0] is now 33 *)

  • Return length of array (number of slots, not highest index) a.length โ†’ 5 b.length โ†’ 3 c.length โ†’ 2

Arrays of Arrays

Arrays can contain other arrays as their elements. E.g.:

picture will be drawn in class

Array Functions: Summation

Typically manipulate arrays using for loops (but can also use while

loops, tail recursion, non-tail recursion). E.g:

// Return the sum of all elements in integer array a. public static int sum (int [ ] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; } return sum; }

Array Functions: MinIndex

// Return the index of the minimum integer in the array, or -1 if the array // is empty. public static int minIndex (int [ ] a) { int minVal = Integer.MAX_VALUE; int minInd = -1; for (int i = 0; i < a.length; i++) { if (a[i] < minVal) { minVal = a[i]; minInd = i; } } return minInd; }

Array Functions: minIndexBetween

// Return the index of the minimum integer in a[lo..hi], // or -1 if the segment is empty. public static int minIndexBetween (int [ ] a, int lo, int hi) { int minVal = Integer.MAX_VALUE; int minInd = -1; for (int i = lo; i <= hi; i++) { if (a[i] < minVal) { minVal = a[i]; minInd = i; } } return minInd; }

Array Functions: Selection Sort

public static void selectionSort (int [ ] a) {

int hi = a.length - 1;

// Loop invariant:

// All elements in a[0..i-1] are in their final sorted positions.

for (int i = 0; i < hi; i++) {

swap(a, i, minIndexBetween(a, i, hi));

// Last slot is not visited. Can you explain why?

Array Functions: Insertion

// Assume a[lo..hi-1] is in sorted order. // Insert a[hi] into the segment a[lo..hi] in such a way to make it sorted. public static void insertBetween(int [ ] a, int lo, int hi) { int val = a[hi]; int index = hi; // Loop invariants: // All elements in a[lo..index-1] are sorted. // All elements in a[index+1..hi] are sorted and are > val. while ((index > lo) && (a[index-1] > val)) { // Order of tests is crucial! a[index] = a[index-1]; // Shift value right = shift hole left. index = index - 1; } // Here, either (index == lo) or ((index > lo) && (a[index-1] <= val)) a[index] = val; }