C Programming Sorting
C Programming Sorting
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2
Problem Specification
Sorting
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5
Problem Abstraction
Sorted Data
9.25 9.0 8.5 8.25 8.0 8.0 7.5 7.5 7.0 6.5
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7
• Selection Sort
• Insertion Sort
• Bubble Sort
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8
Selection Sort
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9
Unsorted Data
7.5 8.0 6.5 9.25 7.5 8.5 9.0 7.0
0 1 2 3 4 5 6 7
Index of Max
After i=0
9.25 8.0 6.5 7.5 7.5 8.5 9.0 7.0
0 1 2 3 4 5 6 7
Index of Max
After i=1
9.25 9.0 6.5 7.5 7.5 8.5 8.0 7.0
0 1 2 3 4 5 6 7
Index of Max
After i=2
9.25 9.0 8.5 7.5 7.5 6.5 8.0 7.0
0 1 2 3 4 5 6 7
Index of Max
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10
C Program
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11
double temp ;
EXCH(cgpa[i], cgpa[max], temp);
}
} // selSort.c
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13
Execution Time
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 15
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16
Execution of selectionSort()
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17
Execution of indexOfMax()
& %
2(n − i − 1) array access.
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18
Execution Time
Execution Time
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 20
Execution Time
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 21
Time Complexity
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 22
Space Complexity
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 23
Space Complexity
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 24
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 25
temp = cgpa[i] ;
max = i ;
for(j = i+1; j < noOfStdnt; ++j)
if(cgpa[j] > temp) {
temp = cgpa[j] ;
max = j ;
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 26
}
EXCH(cgpa[i], cgpa[max], temp);
}
} // selSort1.c
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 27
Space Complexity
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 28
Note
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 29
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 30
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 31
cg(n) f(n)
kh(n)
n1 n0 n
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 32
kh(n)
cg(n)
f(n)
n0 n1 n
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 33
Examples
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 34
Examples
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 35
c0 g(n)
c g(n)
1 f(n)
n0 n
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 37
Examples
n 0 1 2 3 ···
1 5 7 11 17
3 g(n) 3 3 3 3 ···
n2 0 1 4 9 ···
Selection Sort
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 39
Note
Insertion Sort
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 41
Unsorted Data
7.5 8.5 6.5 8.0 7.5 8.5 9.0 7.0
0 1 2 3 4 5 6 7
1st Step 1
7.5 8.5 6.5 8.0 7.5 8.5 9.0 7.0
0 2 3 4 5 6 7
8.5 temp
2nd Step 2
8.5 7.5 6.5 8.0 7.5 8.5 9.0 7.0
0 1 3 4 5 6 7
6.5 temp
2nd Step 1 3
8.5 7.5 6.5 8.0 7.5 8.5 9.0 7.0
0 2 4 5 6 7
8.0 temp
After 2nd Step 3 4
8.5 8.0 7.5 6.5 7.5 8.5 9.0 7.0
0 1 2 3 4 5 6 7
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 42
for i ← 1 to noOfStdnt −1 do
temp ← cgpa[i]
for j ← i-1 downto 0 do
if cgpa[j] < temp
cgpa[j+1] ← cgpa[j]
else go out of loop
endFor
cgpa[j+1] ← temp
endFor
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 43
C Program
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 44
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 45
Execution Time
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 46
Execution Time
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 47
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 48
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 49
Bubble Sort
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 50
i=0 0 1 2 3 4 5 6 7
j=7 53 22 49 15 21 82 37 16
no−exchange
0 1 2 3 4 5 6 7
j=6 53 22 49 15 21 82 37 16
no−exchange
0 1 2 3 4 5 6 7
j=5 53 22 49 15 21 82 37 16
exchange
0 1 2 3 4 5 6 7
j=4 53 22 49 15 82 21 37 16
exchange
0 1 2 3 4 5 6 7
j=3 53 22 49 82 15 21 37 16
exchange
0 1 2 3 4 5 6 7
j=2 53 22 82 49 15 21 37 16
exchange
0 1 2 3 4 5 6 7
j=1 53 82 22 49 15 21 37 16
exchange
0 1 2 3 4 5 6 7
82 53 22 49 15 21 37 16
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 51
for i ← 0 to noOfStdnt −2 do
exchange = NO
for j ← noOfStdnt −1 downto i +1 do
if (cgpa[j-1] < cgpa[j])
cgpa[j-1] ↔ cgpa[j] # Exchange
exchange = YES
endFor
if (exchange == NO) break
endFor
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 52
C Program
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 53
#define EXCHANGE 0
#define NOEXCHANGE 1
#define EXCH(X,Y,Z) ((Z)=(X), (X)=(Y), (Y)=(Z))
void bubbleSort(double cgpa[], int noOfStdnt) {
int i, j, exchange, temp ;
}
} // bubbleSort.c
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 55
Execution Time
& %
Lect 19 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 56
& %
Lect 19 Goutam Biswas