Vous êtes sur la page 1sur 23

COMPUTER

PROGRAMMING
Lecture 05
Unit 1 Problem Solving
Syllabus
Objecti
ves

2
Bubble sort algorithms
1
Understand insertion sort algorithms
Sorting

Sorting is defined as arranging the record in some logical manner.


Sorted = ordered based on a particular way.
Generally, collections of data are presented in a sorted manner.
Examples of Sorting:
Words in a dictionary are sorted (and case distinctions are ignored).
Files in a directory are often listed in sorted order.
The index of a book is sorted (and case distinctions are ignored).

Insertion Sort
Bubble sort
Insertion sort

Suppose an array A with n element A[1], A[2], A[1],A[n] is in


the memory.
The insertion sort algorithm scans A from A[1] to A[n]
inserting each element A[k] into its proper position in the
previously Sorted sub array A[1], A[2],. A[k-1].
Cont.

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.

1 Set A[0]= - ~ [Initialize sentinel element]

Repeat step 3 to 5 for k=2,3,n 2

3 Set TEMP:=A[k] and PTR:=k-1

Repeat while TEMP< A[PTR] 4


a)Set A[PTR+1]:=A[PTR] [MOVES ELEMENT
FORWARD]
b)Set PTR:= PTR-1 [End of loop]

Set A[PTR+1]:=TEMP [Insert element in to its proper place]


5
Return\Stop 6
Example: sorting numberes

Consider the list of unsorted numbers

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

Idea: bubble in water.


Bubble in water moves upward. Why?
How?
When a bubble moves upward, the water from above will
move downward to fill in the space left by the bubble.
Bubble sort

Compare each element (except the last two) with its


neighbor to the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final
places
Continue..
Compare each element (except the last one) with its neighbor
to the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last three) with its
neighbor to the right
Continue as above until you have no unsorted elements on
the left
Example of bubble sort

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:-

Let A be the list of N number. Sorting A refers to the


operation of re-arranging the element of A so that they
are in increasing order,
So that :- A[1] <A[2]< A[3] <.A[N].
For example suppose A originally the list.
8, 4, 19, 2, 7, 13, 5, 16.
After sorting A the list is
2, 4, 5, 7, 8, 13, 16, 19.
(Bubble sort) BUBBLE(Data, N)
Here DATA is an array with N element. This algorithm sort the
elements in DATA.

1 3 5
Repeat b)Set
1.Repeat
step 2and while PTR:=PTR+1

3for k=1 to PTR<=N-K [End of inner


N-1 [Executes loop]
pass].
[End of step 1
.
outer loop]

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

1. What is mean by sorting?


2. Draw the flowchart for insertion sort.
3. Write process to sort following list using Bubble sort.
12 45 34 67 98 42 23 7 87

Vous aimerez peut-être aussi