Transcript File
Permutations
and
Combinations
Review
Plus a little Dry Run, Sorting and
Code.
Objectives:
Review Permutations and Combinations
apply fundamental counting principle
compute permutations
compute combinations
distinguish permutations vs combinations
Practice reading methods that use parameters
Review Insertion Sort
Write a program that incorporates the
insertion sort.
Fundamental Counting
Principle
Fundamental Counting Principle can be used
determine the number of possible outcomes
when there are two or more characteristics .
Fundamental Counting Principle states that
if an event has m possible outcomes and
another independent event has n possible
outcomes, then there are m* n possible
outcomes for the two events together.
Fundamental Counting
Principle
Lets start with a simple example.
A student is to roll a die and flip a coin.
How many possible outcomes will there be?
1H 2H
1T 2T
3H
3T
4H
4T
5H
5T
12 outcomes
6H
6T
6*2 = 12 outcomes
Fundamental Counting
Principle
For a college interview, Robert has to choose
what to wear from the following: 4 slacks, 3
shirts, 2 shoes and 5 ties. How many possible
outfits does he have to choose from?
4*3*2*5 = 120 outfits
Permutations
A Permutation is an arrangement
of items in a particular order.
Notice,
ORDER MATTERS!
To find the number of Permutations of
n items, we can use the Fundamental
Counting Principle or factorial notation.
Permutations
The number of ways to arrange
the letters ABC:
____ ____
____
3 ____ ____
Number of choices for second blank? 3 2 ___
Number of choices for third blank?
3 2 1
Number of choices for first blank?
3*2*1 = 6
ABC
ACB
3! = 3*2*1 = 6
BAC
BCA
CAB
CBA
Permutations
To find the number of Permutations of
n items chosen r at a time, you can use
the formula
n!
n pr ( n r )! where 0 r n .
5!
5!
5 * 4 * 3 60
5 p3
(5 3)! 2!
Permutations
Practice:
A combination lock will open when the
right choice of three numbers (from 1
to 30, inclusive) is selected. How many
different lock combinations are possible
assuming no number is repeated?
Answer Now
Permutations
Practice:
A combination lock will open when the
right choice of three numbers (from 1
to 30, inclusive) is selected. How many
different lock combinations are possible
assuming no number is repeated?
30!
30!
30 * 29 * 28 24360
30 p3
( 30 3)! 27!
Permutations
Practice:
From a club of 24 members, a
President, Vice President, Secretary,
Treasurer and Historian are to be
elected. In how many ways can the
offices be filled?
Answer Now
Permutations
Practice:
From a club of 24 members, a
President, Vice President, Secretary,
Treasurer and Historian are to be
elected. In how many ways can the
offices be filled?
24!
24!
24 p5
( 24 5)! 19!
24 * 23 * 22 * 21 * 20 5,100,480
Combinations
A Combination is an arrangement
of items in which order does not
matter.
ORDER DOES NOT MATTER!
Since the order does not matter in
combinations, there are fewer combinations
than permutations. The combinations are a
"subset" of the permutations.
Combinations
To find the number of Combinations of
n items chosen r at a time, you can use
the formula
n!
C
where 0 r n .
n r r! ( n r )!
Combinations
To find the number of Combinations of
n items chosen r at a time, you can use
the formula n!
C
where 0 r n .
n r r! ( n r )!
5!
5!
5 C3
3! (5 3)! 3!2!
5 * 4 * 3 * 2 * 1 5 * 4 20
10
3 * 2 *1* 2 *1 2 *1 2
Combinations
Practice:
To play a particular card game, each
player is dealt five cards from a
standard deck of 52 cards. How
many different hands are possible?
Answer Now
Combinations
Practice: To play a particular card game, each
player is dealt five cards from a
standard deck of 52 cards. How
many different hands are possible?
52!
52!
52 C5
5! (52 5)! 5!47!
52 * 51 * 50 * 49 * 48
2,598,960
5* 4* 3* 2*1
Combinations
Practice:
A student must answer 3 out of 5
essay questions on a test. In how
many different ways can the
student select the questions?
Answer Now
Combinations
Practice: A student must answer 3 out of 5
essay questions on a test. In how
many different ways can the
student select the questions?
5!
5! 5 * 4
10
5 C3
3! (5 3)! 3!2! 2 * 1
Combinations
Practice:
A basketball team consists of two
centers, five forwards, and four
guards. In how many ways can the
coach select a starting line up of
one center, two forwards, and two
guards?
Answer Now
Combinations
Practice:
Center:
A basketball team consists of two centers, five forwards,
and four guards. In how many ways can the coach select a
starting line up of one center, two forwards, and two
guards?
Forwards:
Guards:
2!
5! 5 * 4
4! 4 * 3
C
10
C
2
6
5 2
2
1
4 C2
2!3! 2 * 1
2!2! 2 * 1
1!1!
2
C1 * 5 C 2 * 4 C 2
Thus, the number of ways to select the
starting line up is 2*10*6 = 120.
Summary
Permutations: Order matters
The number of Permutations of n items chosen r at a
time, you can use the formula
n!
n pr ( n r )! where 0 r n .
Combinations: Order does not matter.
To find the number of Combinations of n items
chosen r at a time, you can use the formula
n!
C
where 0 r n .
n r r! ( n r )!
Stats Pack: Write a program with
methods to calculate the following.
• Factorial
•
•
•
– Sent a positive integer, returns its factorial. (Note: 0! = 1)
Combinations
Write a method that will find the number of combinations of n
items taken r at a time.
Combinations of n items take r at a time= n!/((r!)(n-r)!)
–
–
–
•
•
•
Enter the values for n and r in the main body
Calculate and return the number of combinations in the method
Show the number of combinations in the main body.
Permutations
Write a method that will find the number of permutations of n items
taken r at a time.
Permutations of n items taken r at a time= n!/(n-r)!
–
–
–
Enter the values for n and r in the main body
Calculate and return the number of combinations in the method
Show the number of combinations in the main body.
Learning Objectives
• Be able to read a methods that are passed
parameters.
• Be able to write a second program that uses
methods.
CTA9 (none). Consider the following static method
public static double getSomething(int val)
{
val = 2 * val;
val = val + 2 * val;
Multiple
Choice
return val;
}
Which of the following could be used to replace the body of getSomething so that the
modified version will return the same result as the original version for all values of the
parameter val.
A.
B.
C.
D.
E.
return 2 * val;
return 3 * val;
return 4 * val;
return 5 * val;
return 6 * val;
CTA10 (A1.15). Consider the following static method
public static double getSomething(int val)
{
val = 2 + val;
val = val + 3*val;
Multiple Choice
return val;
}
Which of the following could be used to replace the body of getSomething so that the
modified version will return the same result as the original version for all values of the
parameter val.
A.
B.
C.
D.
E.
return 4*val + 2;
return 4*val + 6;
return 4*val + 8;
return 7*val + 6;
return 7*val + 8;
public static void sort(String [] list)
{
for(int start = 0; start < list.length-1; start++)
{
int index = start;
for(int k = start + 1; k < list.length; k++)
{
if(list[k].compareTo(list[index]) > 0)
index = k;
}
String temp = list[start];
list[start] = list[index];
list[index] = temp;
}
}
Note how an array is passed
to a method.
Assume the String array words is initialized as shown below.
word
Bill
Joe
Sue
Ann
Mel
Zeb
What will the array word look like after the third pass through the outer loop
in the call sort(word)?
A20 (A3.32). Consider the incomplete method ChoiceOfThree.
public int choiceOfThree()
{
// missing code
}
Multiple Choice
Method choiceOfThree is intended to return one of the values 1, 2, 3, each with probability 1/3.
Consider the following replacements for //missing code.
I.
II.
III.
return (int)(Math.random() * 3);
return (int)(Math.random() * 3) + 1;
if(Math.random() < 1.0/3.0)
return 1;
else if (Math.random() < 2.0/3.0)
return 2;
else
return 3;
Which if these replacements for //missing code will make choiceOfThree work as intended?
A. I only
B. II only
C. III only D. I and III E. II and III
Insertion sort
•
•
•
•
•
•
•
•
•
How it works
Example
Low
High
8 6 7 3 15 1 5
Stability
Speed
Your turn
High
Low
8 2 5 3 9 4 6 1 7
• Dummy, slide, back
• Stable
• O(n2)
Insertion Sort
// a is the name of the array
// nElems stores the number of elements being sorted
// This example is for sorting an array of ints
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
int dummy = a[out]; // dummy Need to modify for sorting different types
in = out; // start shifts at out
while(in>0 && a[in-1] >= dummy) // until one is smaller, What if sorting Strings?
{
a[in] = a[in-1]; // Slide: shift item right,
in--; // go left one position
}
a[in] = dummy; // Back: insert marked item
} // end for
Second Method Program
• Main Body
– Get 9 scores
– Call methods
– Show results
•
•
•
•
Scores in order Low to High
Mean, median
Push: Mode, standard deviation
Push: Handle getting an unknown number (but less than
100) of values.
• Methods
–
–
–
–
Insertion Sort
Mean
Median
Push: Mode, standard deviation