Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Data structure

syllabus
• Data-structures: Lists as covered in Class XI
• Sorting and searching elements in list
• Stacks – Push, Pop using a list,
• Queues – Insert, Delete using a list.
What is a data
3

structuire
Data structure is a specific way of storing and
organizing data in memory such that it can
be retrieved and used in a most productive
way
Python data 4

structure
5
list
6

1. List.append(element) 1. del list[index]


2. List.insert(position , element) 2. min(list)
3. List.extend(list) 3. max(list)
4. List.count(element) 4. sum(list)
5. List.index(element) 5. sorted(list)
6. List.sort(reverse=True)
7. list.pop([index])
8. list.pop()
9. list.remove(element)
7

Let us see execute th


ese functions once a
gain
8

Sorting techniques
1. Bubble sort
2. Selection sort
3. Insertion sort
9

Bubble sort
This sorting algorithm is comparison-based algorithm in which
each pair of adjacent elements is compared and the elements
are swapped if they are not in order. 

After comparison the highest element takes correct position in the


list.
10

Let us take an example , arrange element in ascending order

F
I
R
S 1.
2.
Compare first two elements
Swap if first is larger than other otherwise
T move to next comparison

P
A
1. Compare next two elements
S 2. Swap if first is larger than other(33 and 27)
S otherwise move to next comparison
11

F
I 1. Compare next two element - no change as
R 33<35

S
T

P 1. Compare next two elements


2. Swap if first is larger than other(35 and 10)
A otherwise move to next comparison
S After first pass last element or greatest
element is at its correct place.
S
Repeat these steps until the whole list is arranged in ascending order.
12

1. Start Comparison for second pass in


S the same way as done in the first pass
E 1. Skip swapping if first element(14) is smaller than next (27)other
wise swap two elements
C
O
N 2. Skip swapping if first element(27) is smaller than next (33) other
wise swap two elements
D

P 3. Swap if first(33) is greater than (10) next

A
S
S
4. Now second greatest element is its correct position , so
comparing after this will not make changes in the list
13

Still array is not sorted so repeat same steps again for third pass
T
H
1. Skip swapping if first element(14) is smaller than next (27)other
I wise swap two elements
R
D
2. Skip swapping if first element(27) is smaller than next (10)
other wise swap two elements
P
A
S
S 3. Now third greatest element is its correct position , so
comparing after this will not make changes in the list
14

Till now we can conclude that each pass one element is taking its correct position .
F At this stage array elements are :
O
Now start comparison for fourth pass
U
R Compare first two elements again
T
H
Now list is arranged so comparison will not change any values
P
A 1. So for 5 elements four passes required
2. In each pass number of comparison is decreasing one by one i. e
S 3. In the first pass 4 comparisons
S 4. In the second pass 3 comparisons
5. In the third pass 2 comparisons
6. In the forth pass 2comparisons
15

Let us see one see on


e video for bubble so
rt
Python code for 16

bubble sort
Run the
code
17

Prcatice Time
write passes to arrange the given list in ascending order using Bubble sort.

List=[10,4,3,2,8,9,11,10,15,12]
18

Selection sort
This sorting algorithm is comparison-based algorithm in which each
first position elements is taken from list and compared with
elements at rest of position.
In case of ascending order if other elements are less , than swap the
elements.
And do same till all other position elements are compared with other
positional elements.
In this sorting (ascending order) smallest element takes its position
first.
19

F 29 15 10 27 24
I
29>15 , Compare first position element with rest of the
R swap elements Since 29>15 need to swap both elements
S
T 15 29 10 27 24

P as 15>10 swap
A
S
10 29 15 27 24
S
20

10 29 15 27 24
F
I No change as 10<27
R
S 10 29 15 27 24
T
No change as 10<24

P
A 10 29 15 27 24
S
S First pass completed , now start second pass to sort other elements ,
as first shortest element took its correct position.
21

S 10 29 15 27 24
E 29>15 , swap
C
O 10 15 29 27 24
N
15<27 , no need to swap
D

P 10 15 29 27 24
A 15<24 , no need to swap
S
S 10 15 29 27 24

Now start third pass , to sort array completely


22

T 10 15 29 27 24
H
29>27 , swap
I
R 10 15 27 29 24
D
27>24 , swap

P
A 10 15 24 29 27
S
S
23

F 10 15 29 27
24
O
U 29>27 , swap

R
T 10 15 24 27 29
H

FINAL SORTED ARRAY


P So , no. of passes required =n-1
A Comparison in each pass reducing by one
each time
S
S
24

Video example of sel


ection sort
Python code of 25

selection sort
Run the
code
26

Insertion sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained
which is always sorted. For example, the lower part of an array is maintained to
be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find
its appropriate place and then it has to be inserted there. Hence the
name, insertion sort.
How insertion sort 27

works
29 15 10 27 24

Step 1 − If it is the first element, it is already sorted. return 1;


Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
28

29 15 10 27 24

Compare 15 with 29
29 is greater than 15 so shift it and insert
15 at first position.

15 29 10 27 24

Compare 10 with 29,10<29 so shift 29 to next


place and
now compare with 15 ,10<15 so shift 15 also
and insert 10 at first position

10 15 29 27 24
29

10 15 29 27 24

Compare 27 with 29 ,27<29 shift 29 to next position


Compare 27 with 15 , since 27>15 no need to shift

10 15 27 29 24

Compare 24 with 29 ,24<29 shift 29 to next position


Now Compare 24 with 27 , since 24<27 so shift 27 to next
position
Now compare 24 with 15 , since 24>15 ,no need to shift it to
next position
30

Video example of inserti


on sort
Python code for 31

insertion sort

Run the c
ode
32

THANK YOU

You might also like