Professional Documents
Culture Documents
Session – 31 to 32
1
Problem
Results of VIT entrance exam has been released. Given the
details of the students such as name, address and marks scored
in entrance, write a program to sort the student details so that it
will be convenient to call for counselling.
Sorting means . . .
Sorting rearranges the elements into either
ascending or descending order within the
array (we’ll use ascending order).
36 6
24 10
10 12
6 24
12 36 3
Sorting also means…
There are several sorting algorithms available like bubble sort,
selection sort, insertion sort, quick sort, merge sort, radix sort etc.
For example listing all the product in the increasing order of their
names or decreasing order of supplier names
List containing exam scores sorted from lowest to highest or vice versa
Compares neighboring
values [ 0 ] 36 pairs of array elements,
starting with the last array
[1] element, and swaps
24
neighbors whenever they
[2] are not in correct order.
10
[3] On each pass, this causes
6 the smallest element to
[4] “bubble up” to its correct
12 place in the array.
5
Bubble Sort Pseudo Code
6
Bubble Sort: Pass One
values [ 0 ] 36 U
[1] N
24 S
[2] O
10 R
[3]
6 T
E
[4] D
12
7
Bubble Sort: Pass One
values [ 0 ] 36 U
[1] N
24 S
O
[2]
10 R
T
[3]
6 E
D
[4]
12
8
Bubble Sort: Pass One
values [ 0 ] 36 U
[1] N
24 S
O
[2]
6 R
T
[3]
10 E
D
[4]
12
9
Bubble Sort: Pass One
values [ 0 ] 36 U
[1] N
6 S
O
[2]
24 R
T
[3]
10 E
D
[4]
12
10
Bubble Sort: End Pass One
values [ 0 ] 6 SORTED
[1] U
36
N
[2] S
24 O
[3] R
10 T
[4] E
12 D
11
Bubble Sort: Pass Two
values [ 0 ] 6 SORTED
[1] U
36
N
[2] S
24 O
[3] R
10 T
[4] E
12 D
12
Bubble Sort: Pass Two
values [ 0 ] 6 SORTED
[1] U
36
N
[2] S
24 O
[3] R
10 T
[4] E
12 D
13
Bubble Sort: Pass Two
values [ 0 ] 6 SORTED
[1] U
36
N
[2] S
10 O
[3] R
24 T
[4] E
12 D
14
Bubble Sort: End Pass Two
values [ 0 ] 6
SORTED
[1] U
10
N
[2] S
36 O
[3] R
24 T
[4] E
12 D
15
Bubble Sort: Pass Three
values [ 0 ] 6
SORTED
[1] U
10
N
[2] S
36 O
[3] R
24 T
[4] E
12 D
16
Bubble Sort: Pass Three
values [ 0 ] 6
SORTED
[1] U
10
N
[2] S
36 O
[3] R
12 T
[4] E
24 D
17
Bubble Sort: End Pass Three
values [ 0 ] 6 S
O
[1] R
10 T
[2] E
12 D
[3]
36
UNSORTED
[4]
24
18
Bubble Sort: Pass Four
values [ 0 ] 6 S
O
[1] R
10 T
[2] E
12 D
[3]
36
UNSORTED
[4]
24
19
Bubble Sort: End Pass Four
values [ 0 ] 6
S
[1] O
10
[2]
R
12 T
[3]
24 E
D
[4]
36
20
A sample Python code for Bubble
Sort
Selection Sort
22
Selection Sort Pseudo
Code
23
Selection Sort: Pass One
values [ 0 ] 36 U
[1] N
24 S
[2] O
10 R
[3]
6 T
E
[4] D
12
24
Selection Sort: End Pass One
values [ 0 ] 6 SORTED
[1] U
24
N
[2] S
10 O
[3] R
36 T
[4] E
12 D
25
Selection Sort: Pass Two
values [ 0 ] 6 SORTED
[1] U
24
N
[2] S
10 O
[3] R
36 T
[4] E
12 D
26
Selection Sort: End Pass Two
values [ 0 ] 6
SORTED
[1]
10
[2] U
24 N
S
[3] O
36 R
T
[4] E
12 D
27
Selection Sort: Pass Three
values [ 0 ] 6
SORTED
[1]
10
[2] U
24 N
S
[3] O
36 R
T
[4] E
12 D
28
Selection Sort: End Pass Three
values [ 0 ] 6 S
O
[1] R
10 T
[2] E
12 D
[3]
36
UNSORTED
[4]
24
29
Selection Sort: Pass Four
values [ 0 ] 6 S
O
[1] R
10 T
[2] E
12 D
[3]
36
UNSORTED
[4]
24
30
Selection Sort: End Pass Four
values [ 0 ] 6
S
[1] O
10
[2]
R
12 T
[3]
24 E
D
[4]
36
31
Selection Sort:
How many comparisons?
35
Insertion Sort
37
Insertion Sort
38
Insertion Sort
39
Insertion Sort
40
Insertion Sort
41
Insertion Sort: Pass One
values [ 0 ] 36 SORTED
[1] U
24
N
[2] S
10 O
[3] R
6 T
[4] E
12 D
42
Insertion Sort: Pass Two
values [ 0 ] 24
SORTED
[1]
36
U
[2] N
10 S
[3] O
6 R
[4] T
12 E
D
43
Insertion Sort: Pass Three
values [ 0 ] 10 S
O
[1] R
24 T
[2] E
36 D
[3]
6
UNSORTED
[4]
12
44
Insertion Sort: Pass Four
values [ 0 ] 6 S
[1] O
10 R
T
[2]
24 E
D
[3]
36
[4]
12 UNSORTED
45
Insertion Sort: Pass Five
values [ 0 ] 6
[1] S
10
O
[2] R
12 T
[3] E
24 D
[4]
36
46
A sample Python code for Insertion
Sort
Exercise 1
Given a sorted list with an unsorted number V
in the rightmost cell, can you write some
simple code to insert V into the array so that it
remains sorted? Print the array every time a
value is shifted in the array until the array is
fully sorted.
Guideline: You can copy the value of V to a
variable and consider its cell "empty". Since
this leaves an extra cell empty on the right,
you can shift everything over until V can be
inserted.
Exercise 2
Using the same approach as exercise 1, sort an entire unsorted
array?
Guideline: You already can place an element into a sorted array.
How can you use that code to build up a sorted array, one
element at a time? Note that in the first step, when you consider
an element with just the first element - that is already "sorted"
since there's nothing to its left that is smaller.
In this challenge, don't print every time you move an element.
Instead, print the array after each iteration of the insertion-sort,
i.e., whenever the next element is placed at its correct position.
Since the array composed of just the first element is already
"sorted", begin printing from the second element and on.