Jan Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg
Jan Outline: Art of programming Computing factorial Sorting numbers Computing primes
Jan Computing Factorial The factorial of an integer is the product of that number and all of the positive integers smaller than it. -5! = 5*4*3*2*1 = ! =
Jan Computing Factorials A simple class to compute factorials: public class Factorial { public static int factorial(int x) { int fact = 1; for (int i =2; i <= x; i ++)//loop fact *= i;//shorthand for: fact=fact*i; return fact; } public class ComputingFactorial { public static void main(String arg[]) { int a = Factorial.factorial(Integer.parseInt(arg[0])); System.out.println(a); }}
Jan Computing Factorials Recursive Factorials /** * This class shows a recursive method to compute factorials. This method * calls itself repeatedly based on the formula: n! = n*(n-1)! **/ public class Factorial2 { public long factorial(long x) { if (x == 1) return 1; else return x*factorial(x - 1); }
Jan Computing Factorials Caching factorials public class Factorial3 { //create an array to cache values 0! Through 20! Static long[] table = new long[21]; Static {table[0] = 1;} //factorial of 0 is 1 //Remember the highest initialized value in the array static int last = 0; public static long factorial(int x) { while (last < x) { table [last + 1] = table[last]*(last + 1); last++; }}
Jan Sorting Numbers Sorting: Input n numbers, sort them such that the numbers are ordered increasingly
Jan Sorting Numbers A simple sorting algorithm main idea: Algorithm Input: an array A containing n integers. Output: sorted array. 1.i := 2; 2.Find the least element a from A(i) to A(n); 3.If a is less than A(i - 1), exchange A(i - 1) and a; 4. i := i + 1; goto step (2).
Jan Sorting Numbers A simple sorting algorithm main idea: 1st step: nd step: …... swap
Jan Sorting Numbers Sorting program: import java.lang.*; public class SortNumber { public static void sort(double[] nums) { for(int i = 0; i < nums.length - 1; i++) { int min = i +1; for (int j = i+2; j < nums.length; j++) { if (nums[j] < nums[min]) min = j; } if (nums[i] > nums[min]) { double tmp; tmp = nums[i]; nums[i] = nums[min]; nums[min] = tmp;} }}
Jan Sorting Numbers public static void main (String[] args) { double[] nums = new double[10]; //Create an array to hold numbers for(int i = 0; i < nums.length; i++) //Generate random numbers nums[i] = Math.random()*100; sort(nums);//Sort them for (int j = 0; j < nums.length; j++) //Print them out System.out.println(nums [j] ); }
Jan Sorting Numbers Quick sort main idea: Algorithm quick_sort(from, center, to) Input: from - pointer to the starting position of array A center - pointer to the middle position of array A to - pointer to the end position of array A Output: sorted array: A’ 1.Find the first element a = A(i) larger than or equal to A(center) from A(from) to A(to); 2.Find the first element b = A(j) smaller than or equal to A(center) from A(to) to A(from); 3.If i < j then exchange a and b; 4.Repeat step from 1 to 3 until j <= i; 5.If from < j then recursive call quick_sort(from,(from + j)/2, j); 6.If i < to then recursive call quick_sort(i, (i+ to)/2, to);
Jan Sorting Numbers main idea: 1st step: nd step: rd step: centerto Smaller than 5 greater than 5 i j The center element is 5. i = j = 5 Quick sort from
Jan Sorting Numbers 4th step: th step: center from to 3 7 from center i = 2 j =
Jan th step: 1 The sequence contains only one element, no sorting. 7th step: 3 4 i = j = 1 8th step: 4 fromto center The center element is 4. The sequence contains only one element, no sorting
Jan
Jan , 19, 18, -quick sorting 3, 4, 6, 1, 10, 9, 5, 20, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 15 3, 4, 6, 1, 10, 9, 5,15, 19, 17, 2, 1, 14, 13, 12, 11, 8, 16,20 18,16,3, 4, 6, 1, 10, 9, 5, 15,17, 2, 1, 14, 13, 12, 11, 8, 20 3, 4, 6, 1, 10, 9, 5, 15, 16, 8,17, 2, 1, 14, 13, 12, 11,19, 20 i=17 j=16 3, 4, 6, 1, 10, 9, 5, 15, 16, 8,17, 2, 1, 14, 13, 12, 11 Sorting Numbers i j
Jan Sorting Numbers Another Java program for the quick sort: public class Sorter { public static void sort (int[] a, int from, int to) { if ((a == null) || (a.length < 2)) return; int i = from, j = to; int center = a[(from + to)/2]; do { while ((i < to) && (a[i] < center)) i++; while ((j > from) && (a[j] > center)) j--; if (i < j) { int tmp =a[i]; a [i] = a[j]; a[j] = tmp;} i++; j--; }while (i <= j);
Jan Sorting Numbers Another Java program for the quick sort: if (from < j) sort(a, from, j); if (i < to) sort(a, i, to); } }
Jan Sorting by merging Merging means the combination of two or more ordered sequence into a single sequence. For example, can merge two sequences: 503, 703, 765 and 087, 512, 677 to obtain a sequence: 087, 503, 512, 677, 703, 765. A simple way to accomplish this is to compare the two smallest items, output the smallest, and then repeat the same process
Jan Merging algorithm Algorithm Merge(s1, s2) Input: two sequences: s1 - x1 x2... x m and s2 - y1 y2... y n Output: a sorted sequence: z1 z2... z m+n. 1.[initialize]i := 1, j := 1, k := 1; 2.[find smaller]if x i y j goto step 3, otherwise goto step 5; 3.[output x i ] z k.:= x i, k := k+1, i := i+1. If i m, goto step 2; 4.[transmit y j ... y n ] z k,..., z m+n := y j,..., y n. Terminate the algorithm; 5.[output y j ] z k.:= y j, k := k+1, j := j+1. If j n, goto step 2; 6.[transmit x i ... x m ] z k,..., z m+n := x i,..., x m. Terminate the algorithm;
Jan Merge-sorting Algorithm Merge-sorting(s) Input: a sequences s = Output: a sorted sequence. 1. If |s| = 1, then return s; 2. k := m/2 ; 3. s1 := Merge-sorting(x 1,..., x k ); 4. s2 := Merge-sorting(x k+1,..., x m ); 5. return(Merge(s1, s2));
Jan Computing Primes Finding the largest prime number smaller than a specified integer: Input integer m, find p m such that p is a prime and if there is prime p’ > p then p’ must be larger m. than m
Jan Computing Primes Algorithm main idea: find primes by eliminating multiples of the form k j, where j is a prime smaller than square-root(m) and k is an integer such that k j m prime 2ij square-root(m) 2222 3333 4444 22 3333 4444 2222 3333 4444 i i i j j j
Jan Computing Primes Import java.lang.*; public class Sieve { public static void main(String[] args) { int max = 100; //Assign a default value try {max = Integer.parseInt(args[0]);} catch (Exception e) {} //Silently ignore exceptions. //Create an array that specifies whether each number is prime or not. boolean[] isprime = new boolean[max+1]; //Assume that all numbers are primes, until proven otherwise. for (int i = 0; i < max; i++) isprime[i] = true; //We know that that 0 and 1 are not prime. Make a note of it. isprime[0] = isprime[1] = false;
Jan Computing Primes //To compute all primes less than max, we need to rule out multiples of all //integers less than the square root of max. int n = (int) Math.ceil(Math.sqrt(max)); for (int i = 0; i <= n; i++) { if (isprime[i]) { int k = 2; for (int j = k*i; j < max; j = (k ++)*i) isprime[j] = false; } } int largest; for (largest = max - 1; !isprime[largest]; largest--); //empty loop body System.out.println(“The largest prime less than or equal to “ + max + “is ” + largest); }}
Jan Assignment #1 (Assignment due: Thu. Feb. 8, 2001) 1. What does the "plateform independence" mean? How is it implemented in Java? 2. Summarize the basic concepts used in Java, such as class, method, variable, Constructor,... (as many as possible) 3. Implement "Caching factorial" and compute 20! using your program. Trace 10 steps of the computation. 4. Implement "quick sort" and sort a sequence containing 20 integers: 3, 4, 6, 1, 10, 9, 5, 20, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 15. Trace 10 steps of the computation. 5. Implement "Sieve" and find all the primes smaller than 200 using your program. Trace 10 steps of the computation.