Académique Documents
Professionnel Documents
Culture Documents
PROGRAMMING
Lecture 05
Unit 1 Problem Solving
Syllabus
Objecti
ves
2
Bubble sort algorithms
1
Understand insertion sort algorithms
Sorting
Insertion Sort
Bubble sort
Insertion sort
That is:-
Pass 1: A[1] by itself is trivially sorted.
Pass 2: A[2] is inserted either before or after A[1] so
that : A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1],
A[2], that is before A[1], between A[1] and A[2], or after
A[2] so that A[1] ,A[2], A[3] is sorted.
Continue..
Pass 4: A[4] is inserted into its proper place in A[1]
,A[2] ,A[3] so that A[1] A[2] A[3] A[4] is sorted.
.
.
.
Pass [n]: A[n] is inserted into its proper place in A[1],
A[2], A[3].. A[n-1] so that A[1], A[2].. A[n] is
sorted.
Algorithm Development
(Insertion sort) INSERTION (A,N)
This algorithm sort the array A with N element.
23 17 45 18 12 22
Example: sorting numbered cards
Unsorted List
23 17 45 18 12 22
1 2 3 4 5 6
Example: sorting numbered cards
Unsorted List
23 17 45 18 12 22
1 2 3 4 5 6
1 2 3 4 5 6
Sorted List
Example: sorting numbered cards
Unsorted List
17 45 18 12 22
1 2 3 4 5 6
23
1 2 3 4 5 6
Sorted List
Example: sorting numbered cards
Unsorted List
45 18 12 22
1 2 3 4 5 6
17 23
1 2 3 4 5 6
Sorted List
Example: sorting numbered cards
Unsorted List
18 12 22
1 2 3 4 5 6
17 23 45
1 2 3 4 5 6
Sorted List
Example: sorting numbered cards
Unsorted List
12 22
1 2 3 4 5 6
17 18 23 45
1 2 3 4 5 6
Sorted List
Example: sorting numbered cards
1 2 3 4 5 6
12 17 18 22 23 45
1 2 3 4 5 6
Sorted List
Bubble Sort: Idea
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
Bubble sort:-
1 3 5
Repeat b)Set
1.Repeat
step 2and while PTR:=PTR+1
2 4 6
Set PTER=1 a)If DATA Exit\
[Initialize Return\
[PTR]>DATA[PTR+1]
pass
Interchange Stop
pointer
PTER]. DATA[PTER]
and DATA[PTR+1]
[End of if structure]
Question Bank