Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jan. 20041 Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg.

Similar presentations


Presentation on theme: "Jan. 20041 Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg."— Presentation transcript:

1 Jan. 20041 Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg

2 Jan. 20042 Outline: Art of programming Computing factorial Sorting numbers Computing primes

3 Jan. 20043 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 = 120 -50! = 30414093201713378043612608166064768844377641568960512000000000000

4 Jan. 20044 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); }}

5 Jan. 20045 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); }

6 Jan. 20046 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++; }}

7 Jan. 20047 Sorting Numbers Sorting: Input n numbers, sort them such that the numbers are ordered increasingly. 3 9 1 6 5 4 8 2 10 7 1 2 3 4 5 6 7 8 9 10

8 Jan. 20048 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).

9 Jan. 20049 Sorting Numbers A simple sorting algorithm main idea: 1st step:3 9 1 6 5 4 8 2 10 7 2nd step:1 9 3 6 5 4 8 2 10 7 1 2 3 6 5 4 8 9 10 7 …... swap

10 Jan. 200410 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;} }}

11 Jan. 200411 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] ); }

12 Jan. 200412 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);

13 Jan. 200413 Sorting Numbers main idea: 1st step:3 1 6 5 4 8 10 7 2nd step: 3 2 1 5 8 9 10 7 3rd step: 3 2 1 4 5 6 8 9 10 7 centerto 29 64 Smaller than 5 greater than 5 i j The center element is 5. i = j = 5 Quick sort from

14 Jan. 200414 Sorting Numbers 4th step: 4 5 6 10 5th step: 1 2 3 4 center from to 3 7 from center i = 2 j = 2 8 9 2 1

15 Jan. 200415 6th 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. 1 2 3 4 5

16 Jan. 200416 6 7 8 9 10...

17 Jan. 200417 18, 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

18 Jan. 200418 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);

19 Jan. 200419 Sorting Numbers Another Java program for the quick sort: if (from < j) sort(a, from, j); if (i < to) sort(a, i, to); } }

20 Jan. 200420 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. 503703765 087512677 087 503703765 512677 087503 703765 512677

21 Jan. 200421 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;

22 Jan. 200422 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));

23 Jan. 200423 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. 1 2 4 357 68912141516182010 11131719

24 Jan. 200424 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) 2222 3333 4444......... 2222 3333 4444 2222 3333 4444 2 2 2 i i i j j j

25 Jan. 200425 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;

26 Jan. 200426 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); }}

27 Jan. 200427 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.


Download ppt "Jan. 20041 Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg."

Similar presentations


Ads by Google