Professional Documents
Culture Documents
Study Mat Xii Cs
Study Mat Xii Cs
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
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.
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
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
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
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
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
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
10 15 29 27 24
29
10 15 29 27 24
10 15 27 29 24
insertion sort
Run the c
ode
32
THANK YOU