CS 100: Lecture L17
March 30
// Illustrates nested loop concept.
import java.io.*;
import java.awt.*;
public class CUCSDrawing extends Frame
{
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,1000,800);
// Draw a collection of blue dots.
int[] hBlue = {100, 100, 80, 80, 70, 70, 80, 80, 100, 100};
int[] vBlue = { 80, 130, 180, 230, 280, 330, 380, 430, 480, 530};
g.setColor(Color.blue);
int nBlue = hBlue.length;
for(int k=0;k<nBlue;k++)
g.fillOval(hBlue[k]-10,vBlue[k]-10,20,20);
// Draw a collection of red dots.
int[] hRed = { 600, 620, 630, 640, 630, 620, 600};
int[] vRed = { 100, 170, 240, 310, 380, 450, 520};
g.setColor(Color.red);
int nRed = hRed.length;
for(int k=0;k<nRed;k++)
g.fillOval(hRed[k]-10,vRed[k]-10,20,20);
// Now draw a line that connects each blue dot with every red dot.
g.setColor(Color.yellow);
for(int i=0;i<nBlue;i++)
{
// Connect the ith blue dot with every red dot
for(int j=0;j< nRed;j++)
{
// Draw a line to the jth red dot.
g.drawLine(hBlue[i],vBlue[i],hRed[j],vRed[j]);
}
}
}
}
public class L17A
{
public static void main(String args[])
{
CUCSDrawing d = new CUCSDrawing();
d.resize(1000,800);
d.move(0,75);
d.setTitle("Connecting dots");
d.show();
d.toFront();
}
}
// Linear Search
import java.io.*;
public class L17B
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
int i;
int[] f = {28,18,30,87,22,13,37,84,70,91,15,34,62,98,30,55};
SearchNSort.print(f);
i = SearchNSort.LinSearch(f,15); System.out.println("x = 15, i = " + i);
i = SearchNSort.LinSearch(f,19); System.out.println("x = 19, i = " + i);
SearchNSort.sort(f);
SearchNSort.print(f);
i = SearchNSort.BinSearch(f,5); System.out.println("x = 5, i = " + i);
i = SearchNSort.BinSearch(f,10); System.out.println("x = 10, i = " + i);
i = SearchNSort.BinSearch(f,22); System.out.println("x = 22, i = " + i);
i = SearchNSort.BinSearch(f,91); System.out.println("x = 91, i = " + i);
i = SearchNSort.BinSearch(f,98); System.out.println("x = 98, i = " + i);
i = SearchNSort.BinSearch(f,99); System.out.println("x = 99, i = " + i);
String[] s = { "p","i", "a", "k", "x" , "t", "w", "d"};
SearchNSort.print(s);
SearchNSort.sort(s);
SearchNSort.print(s);
int[]x = {160 ,150, 140, 130, 120, 110 ,100, 90, 80, 70, 60, 50 ,40 ,30, 20, 10};
SearchNSort.print(x);
SearchNSort.MergeSort(x);
SearchNSort.print(x);
in.waitUntilEnter();
}
}
public class SearchNSort
{
// Yields the index of the first occurence of x in the array a.
// Yields -1 if no array value equals x.
public static int LinSearchBook(int[]a,int x)
{
int n = a.length;
for (int k=0;k<n;k++)
{
if (a[k]==x)
{
return k;
}
}
return -1;
}
// Yields the index of the first occurence of x in the array a.
// Yields -1 if no array value equals x.
public static int LinSearch(int[]a,int x)
{
int n = a.length;
int k=0;
while (k<a.length && x!=a[k])
k++;
if (k==a.length)
return -1;
else
return k;
}
// Yields -1 if no array value equals x.
public static int BinSearch(int[] a,int x)
{
int n = a.length;
int L = 0;
int R = n-1;
int m;
//
while (L<=R)
{
m = (L+R)/2; // The midpoint index.
if (x==a[m])
return m;
if (x>a[m])
// x is not in a[0..m]
L = m+1;
else
// x is not in a[m..n-1]
R = m-1;
}
return -1;
}
// Insertion sort for integers.
// Permutes the values in a so that a[0]<=a[1]<=...<=a[n-1]
// where n = a.length
public static void sort(int[] a)
{
int n = a.length;
int temp; // For swapping array elements
int j;
for (int k=1;k<n;k++)
{
// a[0..k-1] is sorted. Now sort a[0..k] by
// "pushing" it left as long as
// it is smaller than its "left neighbor".
j=k;
while(j>=1 && a[j]<a[j-1])
{
temp = a[j-1]; a[j-1] = a[j]; a[j] = temp;
j=j-1;
}
}
}
// Insertion sort for strings.
// Permutes the values in a so that a[0]<=a[1]<=...<=a[n-1]
// where n = a.length
public static void sort(String[] a)
{
int n = a.length;
String temp; // For swapping array elements.
int j;
for (int k=1;k<n;k++)
{
// a[0..k-1] is sorted. Now sort a[0..k] by
// "pushing" it left as long as
// it is smaller than its "left neighbor".
j=k;
while(j>=1 && a[j].compareTo(a[j-1])<0)
{
temp = a[j-1]; a[j-1] = a[j]; a[j] = temp;
j=j-1;
}
}
}
// Merge sort for integers.
// Permutes the values in a so that a[0]<=a[1]<=...<=a[n-1]
// where n = a.length
public static void MergeSort(int[] a)
{
int n = a.length;
if (n==1)
// Nothing to do
return;
else
{
// Split the array in half
int m = n/2;
int[] aLeft = new int[m];
int[] aRight = new int[m];
for(int i=0;i<m;i++)
{
aLeft[i] = a[i];
aRight[i] = a[i+m];
}
// Sort the left and right halves
MergeSort(aLeft);
MergeSort(aRight);
// Merge the results
int iLeft=0;
int iRight=0;
for (int k=0;k<n;k++)
if (iLeft==m) { a[k] = aRight[iRight]; iRight++;}
else if (iRight==m) { a[k] = aLeft[iLeft]; iLeft++; }
else if (aLeft[iLeft] <= aRight[iRight]) { a[k] = aLeft[iLeft]; iLeft++;}
else { a[k] = aRight[iRight]; iRight++;}
}
}
// Print the values of a reasonably short integer array.
public static void print(int[] a)
{
int n = a.length;
System.out.println("\n");
for(int k=0;k<n;k++)
System.out.print(" " + a[k]);
System.out.println("\n ");
}
// Print the values of a reasonably short string array.
public static void print(String[] a)
{
int n = a.length;
System.out.println("\n");
for(int k=0;k<n;k++)
System.out.print(" " + a[k]);
System.out.println("\n ");
}
}