You are on page 1of 4

Problem Solving and Python Programming GE8151

Insert a card in a list of sorted cards

Algorithm

Step 1 Start Process


Step 2 Assign mycards with list of cards [4,5,7,8,9]
Step 3 Assign length_of_my_cards is 5
Step 4 Assign in_card as 6 and flag as 0 i and j are 0
Step 5 Assign temp_cards list that contains 6 elements assigned as 0 [0,0,0,0,0,0]
Step 6 Print before inserting my_cards list
Step 7 Assign cur_card as my_cards[i]
Step 8 If cur_card is less than in_card then assign temp_cards[j] equals to my_cards[i]
and goto Step 12
Step 9 Else check for flag
Step 10 If flag is zero then assign temp_cards[j] as in_card and flag as 1 then increment j
by value 1 and goto Step 7
Step 11 Else assign temp_cards[j] equals to my_cards[i] and goto Step 12
Step 12 Increment i and j by value 1
Step 13 If i is less than length_of_my_cards goto Step 7
Step 14 Else assign my_cards as temp_cards
Step 15 Print After inserting my_cards
Step 16 Stop Process

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Flow Chart

start

my_cards = [4,5,7,8,9]
length_of_my_cards = 5

in_card = 6, flag = 0, i=0, j = 0

temp_cards = [0,0,0,0,0,0]

my_cards

cur_card = my_cards[i]

False
cur_card < in_card
?
True False
flag = 0
temp_cards[j] = my_cards[i] ?
True
D A B C

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

D A B C

temp_cards[j] = in_card
flag = 1
J=j+1

temp_cards[j] = my_cards[i]

i = i + 1, j = j + 1

True
i < length_of_my_cards

False
my_cards = temp_cards

my_cards

stop

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Pseudo code

START
SET my_cards = [4,5,7,8,9]
SET length_of_my_cards = 5
SET in_card = 6
SET flag = 0
SET i = 0
SET j = 0
SET temp_cards = [0,0,0,0,0,0]
PRINT “Before Insertion : “ my_cards
WHILE i less than or equal to length_of_my_cards
SET cur_card = my_cards[i]
IF cur_card < in_card THEN
temp_cards[j] = my_cards[i]
ELSE
IF flag = 0 THEN
temp_cards[j] = in_card
flag = 1
INCREMENT j
CONTINUE
ELSE
temp_cards[j] = my_cards[i]
END IF
END IF
INCREMENT i
INCREMENT j
END WHILE
SET my_cards = temp_cards
PRINT “After Insertion : “ my_cards
STOP

Rajasekaran S AP/IT

You might also like