1 / 7

CS 112 - Fall 2012 , Lab 03

CS 112 - Fall 2012 , Lab 03. Haohan Zhu. Random Class. http://docs.oracle.com/javase/7/docs/api/java/util/Random.html Create an object for Random() Random r = new Random();

alynch
Download Presentation

CS 112 - Fall 2012 , Lab 03

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 112 - Fall 2012, Lab 03 Haohan Zhu

  2. CS 112 - Fall 2012, Lab 03 Random Class • http://docs.oracle.com/javase/7/docs/api/java/util/Random.html • Create an object for Random() • Random r = new Random(); • Use method .nextInt(n) to generate a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value n (exclusive)

  3. CS 112 - Fall 2012, Lab 03 Evaluation by Time • http://docs.oracle.com/javase/7/docs/api/java/lang/System.html • System.nanoTime(); • System.currentTimeMillis();

  4. CS 112 - Fall 2012, Lab 03 Practice: Inversion • Let int A[] be an array of distinct integers. There are len = A.length distinct integers. • If i<j and A[i]>A[j], then the pair (i, j) is called an inversion of A. • There exists many such pairs in A. Compute how many inversions in A.

  5. CS 112 - Fall 2012, Lab 03 Practice: Inversion • Intuitive method: O(n^2) 1 3 2 5 7 4 6 8 1 3 2 5 7 4 6 8 1 3 2 5 7 4 6 8 1 3 2 5 7 4 6 8

  6. CS 112 - Fall 2012, Lab 03 Practice: Inversion • Maximal number of inversions ? • Find an efficient algorithm • When the array is partly sorted, to determine how many inversions are caused by a number, we do not need to compare every pair of numbers. For instance, in array {1,3,5,7,2,4,6,8}, consider the number of inversions caused by 2.

  7. CS 112 - Fall 2012, Lab 03 Practice: Inversion • Use the process similar to merge sort 1 3 2 5 7 4 6 8 1 3 2 5 7 4 6 8 1 3 2 5 7 4 6 8 1 3 2 5 7 4 6 8

More Related