COMPUTER PROGRAMMING UNIT 1 Lecture 5

23
COMPUTER PROGRAMMING Lecture 05 Prepared By Mr. V. S. Patil Dept (CSE)/AEC

Transcript of COMPUTER PROGRAMMING UNIT 1 Lecture 5

Page 1: COMPUTER PROGRAMMING UNIT 1 Lecture 5

COMPUTER PROGRAMMING

Lecture 05

Prepared By Mr. V. S. Patil Dept (CSE)/AEC

Page 2: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Unit 1 Problem SolvingSyllabus

Prepared By Mr. V. S. Patil Dept (CSE)/AEC

Page 3: COMPUTER PROGRAMMING UNIT 1 Lecture 5

2

1Understand insertion sort algorithms

Bubble sort algorithms

Objectives

Page 4: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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

Page 5: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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].

Page 6: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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.

Page 7: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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.

Page 8: COMPUTER PROGRAMMING UNIT 1 Lecture 5

2

1

3

4

6

5

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

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

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

Repeat while TEMP< A[PTR]

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]

Return\Stop

Algorithm Development(Insertion sort) INSERTION (A,N)

This algorithm sort the array A with N element.

Page 9: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numberes

23 17 45 18 12 22

Consider the list of unsorted numbersConsider the list of unsorted numbers

Page 10: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numbered cards

23 17 45 18 12 22

1 2 6543

Unsorted List

Page 11: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numbered cards

23 17 45 18 12 22

1 2 6543

1 2 6543

Unsorted List

Sorted List

Page 12: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numbered cards

23

17 45 18 12 22

1 2 6543

1 2 6543

Unsorted List

Sorted List

Page 13: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numbered cards

2317

45 18 12 22

1 2 6543

1 2 6543

Unsorted List

Sorted List

Page 14: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numbered cards

2317 45

18 12 22

1 2 6543

1 2 6543

Unsorted List

Sorted List

Page 15: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numbered cards

2317 4518

12 22

1 2 6543

1 2 6543

Unsorted List

Sorted List

Page 16: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example: sorting numbered cards

1812 2217 23 45

1 2 6543

1 2 6543

Sorted List

Page 17: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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.

Page 18: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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

Page 19: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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

Continue..

Page 20: COMPUTER PROGRAMMING UNIT 1 Lecture 5

Example of bubble sort

7 2 8 5 4

2 7 8 5 4

2 7 8 5 4

2 7 5 8 4

2 7 5 4 8

2 7 5 4 8

2 7 5 4 8

2 5 7 4 8

2 5 4 7 8

2 5 4 7 8

2 5 4 7 8

2 4 5 7 8

2 4 5 7 8

2 4 5 7 8

(done)

Page 21: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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

Page 22: COMPUTER PROGRAMMING UNIT 1 Lecture 5

11.Repeat step 2and 3for k=1 to

N-1

2Set PTER=1 [Initialize

pass pointer PTER].

3Repeat while

PTR<=N-K [Executes

pass]..

4a)If DATA

[PTR]>DATA[PTR+1]

Interchange

DATA[PTER]

and DATA[PTR+1]

[End of if structure]

5b)Set

PTR:=PTR+1

[End of inner

loop]

[End of step 1

outer loop]

6Exit\

Return\ Stop

(Bubble sort) BUBBLE(Data, N)

Here DATA is an array with N element. This algorithm sort the

elements in DATA.

Page 23: COMPUTER PROGRAMMING UNIT 1 Lecture 5

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