Post on 19-Dec-2015
Rules of Inference – Valid Arguments in Propositional Logic
04/18/23 Prepared by Dr. Zakir H. Ahmed 2
Argument: An argument is a sequence of statements that end with a conclusion.
Valid: An argument is valid if and only if it is impossible for all premises (preceding statements) to be true and the conclusion to be false. By valid, we mean that the conclusion of the argument must follow from the truth of the premises of the argument.
Consider the arguments:“If you have a current password, then you can log onto the network.”“You have a current password.”Therefore,“You can log onto the network.”
Rules of Inference – Valid Arguments in Propositional Logic
04/18/23 Prepared by Dr. Zakir H. Ahmed 3
The conclusion “You can log onto the network” must be true when the premises “If you have a current password, then you can log onto the network” and “You have a current password” are true.
Let p= “You have a current password.”and q=“You can log onto the network.” Then the argument has the form
where is the symbol that denotes “therefore.” The statement ((p → q) ⌃p) → q is a tautology.
p → q p q
Rules of Inference – Rule for Propositional Logic
04/18/23 Prepared by Dr. Zakir H. Ahmed 4
The argument form with premises p1, p2, …., pn and conclusion q is valid, when (p1⌃p2⌃….⌃pn) → q is a tautology.
To show that an argument is valid, instead of showing by truth table, we can establish the validity of some relatively simple argument forms, called rules of inference, which can be used as building blocks to construct more complicated valid argument forms.
The tautology ((p → q) ⌃p) → q is the basis of the rule of inference called modus ponens or law of detachment.
Rules of Inference – Rules for Propositional Logic
."4
9
2
32)2(,
.2
32.
2
3)2(,
2
32"
22
22
lyConsequent
thatknowWethenIf
04/18/23 Prepared by Dr. Zakir H. Ahmed 5
Example: Suppose that the conditional statement “If it snows today, then we will go skiing” and its hypothesis “It is snowing today”, are true. Then by modus ponens, it follows that the conclusion of the conditional statement, “We will go skiing” is true.
Q1: Determine whether the argument given here is valid and determine whether its conclusion must be true because of the validity of the argument.
Rules of Inference – Rule for Propositional Logic
04/18/23 Prepared by Dr. Zakir H. Ahmed 7
Example: State which rule of inference is the basis of the following argument: “It is below freezing now. Therefore, it is either below freezing or raining now”.
Sol: Let p= “It is below freezing now.” and q = “It is raining now.” Then this argument is of the form:
p p v qThis is an argument that uses the addition rule.
Q2: State which rule of inference is the basis of the following argument: “It is below freezing and raining now. Therefore, it is below freezing”.
Introduction to Proofs
A theorem is a statement that can be shown to be true (usually important statement)
Less important theorem sometimes are called propositions
A proof is a sequence of statements (valid argument) to show that a theorem is true
The statements to be used in proofs include:Axioms (statement assumed to be true
without proof)Ex: If x is positive integer then x+1 is positive
integer.Hypothesis (premises) of the theoremPreviously proven theorems Rules of inference used to draw conclusions
and to move from one step to another04/18/23 Prepared by Dr. Zakir H. Ahmed 8
Introduction to Proofs
A less important theorem that is helpful in the proof of other results is called a lemma
A corollary is a theorem that can be established directly from a theorem that has been proved
A conjuncture is a statement that is being proposed to be a true statement, usually on the basis of some partial evidence
When a proof of a conjuncture is found, the conjuncture becomes a theorem
04/18/23 Prepared by Dr. Zakir H. Ahmed 9
Rules of inference
AxiomsHypothesis
proven theoremsNew theorem
Introduction to Proofs
Example 1: If I have a car (C) I will drive to Makkah (M). My boss gave me 60,000 (G) or Fired me (F). If I have SR60,000 (H) then I have a car (C). My boss did not fire me. Therefore I will drive to Makkah (M).1. GF Hypothesis 2. F Hypothesis3. G Disjunctive syllogism rule using 1 and 24. GH Axiom 5. H C Hypothesis6. G C Hypo. syllogism using 4,57. C Modus ponens using 3 and 68. C M Hypothesis9. M Modus ponens using 7 and 8
04/18/23 Prepared by Dr. Zakir H. Ahmed 10
Methods of proving theorems:Direct proofs
A direct proof of a conditional statement pq is constructed when the first step is the assumption that p is true; subsequent steps are constructed using rules of inference, with the final step showing that q must also be true
In a direct proof, we assume that p is true and use axioms, definitions, and previously proven theorems, together with rules of inference, to show that q must also be true
Def: The integer n is even if there exists an integer k such that n = 2k, and n is odd if there exists an integer k such that n = 2k + 1.
04/18/23 Prepared by Dr. Zakir H. Ahmed 11
Methods of proving theorems:Direct proofs
Example 2: Use a direct proof to show that “if n is even then n2 is even”
Proof: Assume that n is even (hypothesis)=> n = 2k where k is integer (definition of even number)=> n2 = (2k)2 = 4k2 = 2(2k2) (By squaring)Since r = 2K2 is integer (Axiom) => n2 = 2r is even
Q 3: Use a direct proof to show that “if n is odd then n2 is odd”
04/18/23 Prepared by Dr. Zakir H. Ahmed 12
Methods of proving theorems:Proof by contraposition
An indirect proof of a conditional statement p q is a direct proof of its contraposition q p.
Example 3: Use an indirect proof to show that if a and b are integers, and (a + b) ≥ 15, then a ≥ 8 or b ≥ 8.
Proof: The contraposition of (a + b ≥ 15) (a ≥ 8) v (b ≥ 8) is (a < 8) (b < 8) (a + b < 15) Suppose (a < 8) (b < 8) (hypothesis).=> (a ≤ 7) (b ≤ 7),=> (a + b) ≤ 14,=> (a + b) < 15.
04/18/23 Prepared by Dr. Zakir H. Ahmed 13
Methods of proving theorems:Proof by contraposition
Example 4: Use an indirect proof to show that “if n2 is even then n is even”
Proof: The contraposition is “if n is not even then n2 is not even”Assume that n is not even i.e., n is odd (hypothesis)=> n = 2k+1 where k is integer (definition of odd number)=> n2 = (2k + 1)2
= 4k2 + 4k + 1 = 2(2k2 + 2k) + 1 = 2r + 1, where r = 2k2
+ 2kSince r is integer (Axiom) => n2 is not even
Q 4: Use an indirect proof to show that “if n is odd then n2 is odd”
04/18/23 Prepared by Dr. Zakir H. Ahmed 14
Methods of proving theorems:Proof by contraposition
Example 5: Prove that if n = ab then a n or b n where a and b are positive integers
Proof: Let p=“an”, q=“bn” and r=“n=ab”We want to prove that r pq=> The contraposition is (pq ) r (By definition)=> p q r (De Morgan’s law)Now, assume that an and bn (p q)=> a.b n.n = n (by multiplying above twos)=> ab n => r
04/18/23 Prepared by Dr. Zakir H. Ahmed 15
Methods of proving theorems:Vacuous Proofs
Vacuous Proofs: A conditional statement p q is TRUE if p is FALSE. If we can show that p is False, then we have a proof, called vacuous proof, of the conditional statement p q
Example 6: Prove that if x2 0 then 1=2 where x is a real number
Proof: Since x2 0 for every real number then the implication is vacuously true
Example 7: Prove that if he is alive and he is dead then the sun is ice cold.
Proof: Since the hypothesis is always false the implication is vacuously true.
pqp q
F
F
T
T
F
T
F
T
T
T
F
T
04/18/23 Prepared by Dr. Zakir H. Ahmed 16
Methods of proving theorems:Trivial Proofs
Trivial Proofs: A conditional statement p q is TRUE if qis TRUE. If we can show thatq is TRUE, then we have aproof, called trivial proof, ofthe conditional statement p q
Example 8: Prove that if x=2 then x2 0 for all real numbers
Proof: Since x2 0 is true then the implication is trivially true. (we didn’t use the fact x=2)
Q 5: Use a trivial proof to show that if n > 1 then n2 ≥ n for all integers
04/18/23 Prepared by Dr. Zakir H. Ahmed 17
pqp q
F
F
T
T
F
T
F
T
T
T
F
T
Methods of proving theorems:Proofs by Contradiction
Proof by Contradiction: To prove a proposition p, assume not p and show a contradiction.
Example 9: Use a proof by contradiction to show that 2 is irrational
Proof: Let 2 is rational=> 2 = a/b for some integers a and b (b0) (relatively prime). (Definition of rational numbers)=> 2 = a2/b2 (Squaring both sides)=> 2b2 = a2
=> a2 is even (Definition of even numbers)=> a is even (a = 2k for some k)=> 2b2 = a2 = (2k)2 = 4k2
=> b is even (Definition of even numbers)But if a and b are both even, then they are not relatively prime!Hence, 2 is irrational04/18/23 Prepared by Dr. Zakir H. Ahmed 18
Methods of proving theorems:Proofs by Contradiction
Why is this method valid ? The contradiction forces us to reject our assumption because our
other steps based on that assumption are logical and justified. The only “mistake” that we could have made was the assumption itself.
Be careful! Sometimes the contradiction comes from a mistake in the
steps of the proof and not from the assumption. This makes the proof invalid.
Example 10: Prove that 1=2 Proof: Suppose that 21 and a=b for some a.
=> 2b b [multiply by b]=> a+b b [2b=b+b=a+b by hypothesis]=> (a-b)(a+b) b(a-b) [multiply by a-b]=> a2-b2 ab-b2
=> a2 ab [subtract b2 from both sides]=> a b which contradicts our assumption that a=bHence it follows that 1=2
Can you find the error 04/18/23 Prepared by Dr. Zakir H. Ahmed 19
Methods of proving theorems:Proofs by Contradiction
To prove a conditional statement p q by contradiction we prove that p q F is true which is equivalent to p q .
Example 11: Use a proof by contradiction to show that If 3n+2 is odd then n is odd
Proof: Suppose that 3n+2 is odd and n is even [p q] => n = 2r [hypothesis q, definition of even numbers]=> 3n = 6r [multiply 1 by 3]=> 3n+2=2+6r [add 2 to both sides] => 3n+2=2(1+3r)=> 3n+2=2k [let k=1+3r]=> Thus 3n+2 is even which is false (a contradiction !) Therefore the implication is true.
04/18/23 Prepared by Dr. Zakir H. Ahmed 20
Methods of proving theorems:Proofs of Equivalence
Proofs of Equivalence: To prove p q we have to prove p q and q p
Example 12: prove “n is even if and only if n2 is even” “if n is even then n2 is even” proved in example 2 “n2 is even then n is even” proved in example 4 Therefore, n is even if and only if n2 is even
Proving equivalence of several propositions: If we want to prove that p1 p2 p3 … pn
Then it is sufficient to prove p1 p2,, p2 p3 … pn p1
Disproof by Counterexample Example 13: Prove that “For all real numbers x2 > x” is false Proof: X=0.5 is a counterexample since 0.52 > 0.5 is not true Q 6: Prove that “If n is not positive, then n2 is not positive” is
false
04/18/23 Prepared by Dr. Zakir H. Ahmed 21
Methods of proving theorems: Proof by Cases
Proof by Cases: A proof by cases must cover all possible cases that arise in a theorem. Each case may cover an infinite number of instances
Example 15: Prove "if n is an integer then n2 ≥ n". Proof: We can prove that n2 ≥ n for every integer by
considering three cases, when n = 0, when n ≥ 1, and when n ≤ -1.Case 1: When n = 0, since 02 = 0, 02 >= 0. So, n2 ≥ n is true.Case 2Case 2: When n ≥ 1, From n ≥ 1, we get n2 ≥ n (by multiplying with n). Case 3: When n ≤ -1, since n is negative, n2 is positive, so n2 ≥ n.Hence, if n is an integer then n2 ≥ n.
Q 7: Prove that |xy| = |x||y|, where x and y real numbers. Hint: Consider all 4 cases: x and y positive or negative Common errors with exhaustive proof and proof by cases:
Draw conclusion from non-exhaustive examples Not covering all possible cases
04/18/23 Prepared by Dr. Zakir H. Ahmed 22
Methods of proving theorems: Existence Proofs
Many theorems state that an object with certain properties exists, i.e., xP(x) where P is a predicate. A proof of such a theorem is called an existence proof. There are two kinds:
Constructive Existence Proof: The proof is established be giving example a such that P(a) is true
Example 16: Prove "there is a positive integer that can be written as the sum of cubes in two different ways.“
Proof: Consider 1729=103+93=123+13. Finding such examples may require computer assistance.
Non-constructive Existence Proof: The proof is established by showing that an object a with P(a) is true must exist without explicitly demonstrating one. Proofs by contradiction are usually used in such cases.04/18/23 Prepared by Dr. Zakir H. Ahmed 23
Methods of proving theorems: Existence Proofs
Example 17: Let x1,x2,..,xn be positive integers such that their average is m. prove that there exists xi such that xi ≥ m
Proof: Suppose that there is no such number, i,e.,x1 m, x2 m, … , xn m
By adding these inequalities we get: x1+ x2+…+ xn nm
Dividing by n: (x1+ x2+…+ xn)/n m
But since the average is defined as (x1+ x2+…+ xn)/n
Then we have m m which is a contradiction.Therefore there must be a number xi such that xi ≥ m. But we can not specify which number is that.
04/18/23 Prepared by Dr. Zakir H. Ahmed 24
Methods of proving theorems: Uniqueness Proofs
Some theorems state that there is exactly one element with a certain property. A proof of such a theorem is called a uniqueness proof.
Strategy here is (1) show that an element x with the desired property exists (2) show that any other y (y != x) does not have the property, i.e., if x and y both have the property, then x must equal y.
Example 18: Prove that the equation 3x+5 = 9 has a unique solution.
Proof: (1) There exists a solution namely x = 4/3(2) Suppose that y and z are solution then3y+5 = 9 = 3z+5So 3y = 3zDividing by 3 we get y = zThis proves that the solution is unique
04/18/23 Prepared by Dr. Zakir H. Ahmed 25
Methods of proving theorems:Mathematical Induction
Principle of Mathematical Induction: Let P(n) be a statement for all the positive integers (n
= 1, 2, 3, . .). If the following two properties hold: P(1) is true. P(k+1) is true if P(k) is true for each positive integer
k. Then P(n) is true for all n. First part is a simple proposition we call the base step Second part is an inductive step. Start by assuming
P(k) is true, and show that P(k+1) is also true The assumption that P(k) is true called the inductive
hypothesis So, we prove that
(P(1) k (P(k) P(k+1))) (n P(n))04/18/23 Prepared by Dr. Zakir H. Ahmed 26
Methods of proving theorems:Mathematical Induction
Example 19: Suppose we have an infinite ladder, and we want to know whether we can reach every step on this ladder. We know two things:1. We can reach the first rung of the ladder2. If we can reach a particular rung of the ladder, then
we can reach the next rung. How does Induction Work? Consider the above example. The rules for reaching
steps can help you remember how induction works Statements (1) and (2) are the basic step and inductive
step respectively of the proof that P(n) is true for all positive integers n, where P(n) is the statement that we can reach the nth rung of the ladder.
Consequently, we can invoke the mathematical induction to conclude that we can reach every rung of the ladder04/18/23 Prepared by Dr. Zakir H. Ahmed 27
Methods of proving theorems:Mathematical Induction
Example 20 (A Summation Problem): Prove that for any integer n ≥ 1: 1 + 2 + 3 + · · · + n = n(n+1)/2 .
Proof: Let P(n) be the proposition that the sum of the first n positive integers is n(n+1)/2. Then to proof that P(n) is true for all n ≥ 1, we have to show that P(1) is true and P(k+1) is true if P(k) is true for k ≥ 1.Basic step: P(1) is true, because 1 = 1.(1+1)/2Inductive step: Let us assume that it is true for n = k, that is, 1+2+3+…..+k = k(k+1)/2Now, if we can prove that it is true for n = k+1 also, then it can be said that it is true for all n.For, 1 + 2 + · · · + k + (k + 1)
= k(k + 1)/2 + (k + 1)= (k + 1)(k/2 + 1) = (k + 1)(k + 2)/2
=> P(k + 1) is also true, hence P(n) is true for all integer n.04/18/23 Prepared by Dr. Zakir H. Ahmed 28
Methods of proving theorems:Mathematical Induction
Example 21: Use induction to prove that the sum of the first n odd integers is n2.
Proof: Let P(n) be the proposition that the sum of the first n odd integers is n2. Then to proof that P(n) is true for all n ≥ 1, we have to show that P(1) is true and P(k+1) is true if P(k) is true for k ≥ 1.Basic step: P(1) is true, because the sum of the first 1 odd integer is 12. Inductive step: Assume P(k): the sum of the first k odd integers is k2, that is, 1 + 3 + … + (2k - 1) = k2
Now, if we can prove that it is true for n = k+1 also, then it can be said that it is true for all n.For, 1 + 3 + … + (2k-1) + (2k+1)
= k2 + (2k + 1)= (k+1)2
=> P(k + 1) is also true, hence P(n) is true for all integer n.
04/18/23 Prepared by Dr. Zakir H. Ahmed 29
Methods of proving theorems:Mathematical Induction
Q 8: Use induction to prove that 11! + 22! + … + nn! = (n+1)! - 1, n
Q 9: Use induction to prove that for all n,
Q 10: Use induction to prove that for all n,
k2
k1
n
n(n 1)(2n 1)
6
.,)(
11
11
0
rwhen
r
raar
nn
i
i
04/18/23 Prepared by Dr. Zakir H. Ahmed 30
Methods of proving theorems:Mathematical Induction
Example 22: Use induction to prove the inequality n < 2n, n > 0.
Proof: Let P(n) be the proposition that n < 2n
Basic step: P(1) is true, because 1 < 21 = 2.Inductive step: Assume P(k) is true, that is, k < 2K
Now, if we can prove that it is true for n = k+1 also, then it can be said that it is true for all n.For, k < 2K
=> k + 1 < 2K + 1≤ 2K + 2K
=2. 2K
= 2K+1
=> k + 1 < 2K+1
=> P(k + 1) is also true, hence P(n) is true for all integer n. Q 11: Use induction to prove the inequality 2n < n! n > 3.
04/18/23 Prepared by Dr. Zakir H. Ahmed 31
Recursion: Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object in terms of itself. This process is called recursion.
Recursive defined functions: We use two steps to define a function with the nonnegative integers as its domain:Basis Step: Specify the value of the function at zero.Recursive Step: Give a rule for finding its value at an integer from its values at smaller integers. Such definition is called a recursive or inductive definition.
Example 25: The definition of factorial function:n! = 1 · 2 · 3 · … · (n-1) · n, n 1
But equivalently, we could define it like this:
04/18/23 Prepared by Dr. Zakir H. Ahmed 32
Methods of proving theorems: Recursive Definitions
n!n (n 1)!, if n 1
1, if n 1
Recursive Step
Base Step
Example 26: The definition of Fibonacci Numbers:
f (n) 0 if n 0
1 if n 1
f (n 1) f (n 2) if n 1
f (n) 1
5
1 5
2
n
1 5
2
n
04/18/23 Prepared by Dr. Zakir H. Ahmed 33
Methods of proving theorems: Recursive Definitions
Recursive Step
Base Step
Is there a non-recursive definition for the Fibonacci Numbers?
Example 27: Suppose that f is defined byf(0) = 3,f(n+1) = 2f(n) + 3.
Find f(1), f(2), f(3), and f(4). Solution: From the recursive definition, it follows that
f(1) = 2f(0) + 3 = 2.3 + 3 = 9,f(2) = 2f(1) + 3 = 2.9 + 3 = 21, f(3) = 2f(2) + 3 = 2.21 + 3 = 45,f(4) = 2f(3) + 3 = 2.45 + 3 = 93.
Example 28: Give a recursive definition of Solution: The first and second part of the recursive definition are
04/18/23 Prepared by Dr. Zakir H. Ahmed 34
Methods of proving theorems: Recursive Definitions
Recursive Step
Base Step
n
kka
0
0
0
0
aak
k
n
n
kk
n
kk aaa
1
00
Example 29: Give an inductive definition of S = {x: x is a multiple of 3}
Solution: 1. 3 S2. x,y S x + y S 3. x,y S x - y S4. No other numbers are in S.
Q 12: Find the Fibonacci numbers f(2), f(3), f(4), f(5), and f(6). Q 13: Give an inductive definition of an.
04/18/23 Prepared by Dr. Zakir H. Ahmed 35
Methods of proving theorems: Recursive Definitions
Recursive Step
Base Step
Recursively Defined Sets and Structures: Sets can be defined recursively. Recursive definition of sets have two part – basis step and recursive step.Basis Step: An initial collection of elements is specified.Recursive Step: Rules for forming new elements in the set from those already known to be in the set are provided.
Example 30: Consider subset S of the set of integers defined by Basis Step: 3 S.Recursive Step: If x, y S then x + y S .
The new elements found to be in S are 3 by the basis step, 3+3=6 at the first application of the recursive step,3+6=6+3=9 at the second application of the recursive step,6+6=12 at the third application of the recursive step, and so on.We will show later that S is set of all positive multiples of 3.
04/18/23 Prepared by Dr. Zakir H. Ahmed 36
Methods of proving theorems: Recursive Definitions
Recursive Definitions:Recursively Defined Sets
04/18/23 Prepared by Dr. Zakir H. Ahmed 37
Example 31: The set of Natural Numbers N can be defined recursively as follows:
1N [basis step] If x N then x+1N [Recursive step]Lets try to constructs the set using this definition 1 in N [basis step]1+1=2 in N [Recursive step]2+1=3 in N [Recursive step]Etc…
Algorithms:Definitions
04/18/23 Prepared by Dr. Zakir H. Ahmed 39
The word ‘algorithm’ comes from the name of a Persian author, Abu Ja’far Mohammad Ibn Musa Al Khowarizmi (825 AD).
Definition: An algorithm is a finite set of instructions that, if followed, carries out a particular task. In addition, all algorithms must satisfy the following criteria: Input: Zero or more quantities are externally supplied. Output: At least one quantity is produced. Definiteness: Each instruction is clear and
unambiguous. Finiteness: If we trace out the instructions of an
algorithm, then for all cases, the algorithm terminates after a finite number of steps.
Effectiveness: Every instruction must be very basic so that it can be carried out, in principle, by a person using only pen and paper. It must be feasible.
Study of Algorithms
04/18/23 Prepared by Dr. Zakir H. Ahmed 40
How to devise algorithms? Creating an algorithm is an art which may never be
fully automated. How to validate algorithms?
Once an algorithm is devised, it is necessary to show that it computes the correct answer for all possible legal inputs.
A program can be written and then be verified. How to test a program?
Debugging is the process of executing programs on sample data sets to determine whether the faulty results occur and, if so, to correct them.
Profiling is the process of executing a correct program on data sets and measuring the time and space it takes to compute the results.
Pseudocode Conventions
04/18/23 Prepared by Dr. Zakir H. Ahmed 41
Comments begin with // and continue until the end of line.
Blocks are indicated with matching braces: {and}. An Identifier begins with a letter: max. Assignment of values to variables is done using
assignment statement : Variable:=expression. Logical operators: and, or and not are provided. Relational operators: <, ≤, =, ≠, ≥ and > provided. Elements of arrays are accessed using: [ and ]. While loop:
while (condition) do{ statements;}
Pseudocode Conventions
04/18/23 Prepared by Dr. Zakir H. Ahmed 42
For loop: for variable:=value1 to value2 step step do
{Statements;
} Repeat-until loop: repeat
{Statements;
} until (condition) Conditional statement:
if (condition) then (statement); if (condition) then (statement 1);
else (statement 2);
Pseudocode Conventions
04/18/23 Prepared by Dr. Zakir H. Ahmed 43
Case statement: case{ : (condition 1): (statement 1);
…………………. : (condition n): (statement n); : else: (statement n+1);}
Input and output are done using: read and write. There is only one type of procedure: Algorithm. An algorithm consists of a heading and a body. The heading of an algorithm takes the form
Algorithm Name ((parameter list))
Finding Maximum Value
04/18/23 Prepared by Dr. Zakir H. Ahmed 44
Input: A sequence of n numbers (a1, a2,…, an).
Output: Maximum of (a1, a2,…, an).
Algorithm Maximum(A, n) { Max:=A[1]; for i:=2 to n do { if ( Max < A[i]) then
Max:=A[i]; }
}
Sorting Problem
04/18/23 Prepared by Dr. Zakir H. Ahmed 45
Input: A sequence of n numbers (a1, a2,…, an).
Output: A permutation of n numbers (reordering) (a'1, a'2,…, a'n) of the input sequence such that a'1≤ a'2 ≤… ≤ a'n.
Bubble Sort:- It is a popular sorting algorithm It swaps adjacent elements that are out of order
Insertion sort:- Insert an element to a sorted array such that the order
of the resultant array be not changed.
Bubble Sort Algorithm
04/18/23 Prepared by Dr. Zakir H. Ahmed 46
Bubble sort: Algorithm BubbleSort (A, n) { for i:=1 to n-1 do {
for j := n downto i+1 do { if ( A[j]<A[j-1]) then
exchange A[j] ↔ A[j-1]; }
} }
Insertion Sort Algorithm
04/18/23 Prepared by Dr. Zakir H. Ahmed 48
Insertion sort: Algorithm InsertionSort (A, n)
{ for i:=2 to n do { key:=A[i]; // Insert A[i] into the sorted sequence A[1…i-1]. j:=i-1; while ( (j>0) and (A[j]>key) ) do { A[j+1]:=A[j];
j:=j-1; } A[j+1]:=key; }}