C Programming Arrays Sorting
Sorting Bubble Sort Bubble sort sinks heaviest element to bottom causing the lighter elements to bubble up. Bubble step is as follow: 1
2 3
Start from the first element in the array, compare adjacent pair of elements. Swap if required to push the lighter of the pair one position up. Repeat the comparison of next pair until 1st and 2nd element have been compared.
Sorting is accomplished by repeating bubble operation n times.
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Bubble Step 7
4
4
4
4
4
4
4
7
6
6
6
6
6
6
6
6
7
3
3
3
3
3
3
3
3
7
1
1
1
1
1
1
1
1
7
2
2
2
2
2
2
2
2
7
5
5
5
5
5
5
5
5
7
0
0
0
0
0
0
0
0
7
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Code for Bubble Sort void bubbleSort ( int a [ ] , int n) { i n t i , j , temp ; f o r ( i = n −1; i > 0 ; i −−) // E x e c u t e b u b b l e o p e r a t i o n n t i m e s f o r ( j = 1 ; j a [ j ] ) { temp = a [ j − 1 ] ; a [ j −1] = a [ j ] ; a [ j ] = temp ; } }
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Putting All Together #i n c l u d e #i n c l u d e < s t d i o . h> #d e f i n e N 10 i n t main ( ) { i n t a [N ] ; i n t n = s i z e o f ( a )/ s i z e o f ( a [ 0 ] ) ; generate (a , n ); p r i n t f ( ” U n s o r t e d i n p u t \n” ) ; printArray (a , n ); bubbleSort (a , n ) ; p r i n t f ( ” S o r t e d o u t p u t \n” ) ; printArray (a , n ); }
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Insertion Sort Based on the act of picking cards and arranging the hand in sorted order. Initially, only the first element of the array is in sorted order. The input element is compared element-wise with sorted part of the array. If the input is smaller than the current element of the sorted part, consider the next element of sorted part. Else position for the input element is found.
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Insertion Step 5
5 compare
7
5
hole created here
drop 2
1
3
6
4
0
7
2
1
3
6
4
0
7
1
3
6
4
0
1
3
6
4
0
shift 2
2 compare
5
7
2
1
compare 3
6
4
0
5
1
2 drop 5
R. K. Ghosh (IIT-Kanpur)
7
1
3
6
4
0
2
C Programming
5
7
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Code for Insertion Insertion step should be performed by each element starting from first. Insertion step at stage i is as follow: /∗ I n s e r t i o n s t e p a s s u m i n g a [ 0 ,
. . . , i −1] f o r m s s o r t e d p a r t ∗/
x = a [ i ] ; // Save n e x t u n s o r t e d e l e m e n t j = i −1; // Get i n d e x o f l a s t e l e m e n t o f s o r t e d p a r t w h i l e ( x < a [ j ] && j >= 0 ) { a [ j +1] = a [ j ] ; // S h i f t t h e l a r g e r e l e m e n t t o r i g h t j −−; // Get i n d e x o f n e x t l a r g e s t i n s o r t e d p a r t } a [ j +1] = x ; // Drop i n p u t e l e m e n t i n c u r r e n t p o s i t i o n
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Insertion Sort insertionSort ( int a [] , int i , j , x ; for ( i = 1; i < n ; x = a[ i ]; j = i −1; while ( x < a [ j ] a [ j +1] = a [ j j −−; } a [ j +1] = x ; } }
R. K. Ghosh (IIT-Kanpur)
int n) { i ++) {
&& j >= 0 ) { ];
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Putting All Together #i n c l u d e < s t d i o . h> #i n c l u d e #d e f i n e N 10 i n t main ( ) { i n t a [N ] ; i n t n = s i z e o f ( a )/ s i z e o f ( a [ 0 ] ) ; generate (a , n ); p r i n t f ( ” \ n U n s o r t e d i n p u t \n” ) ; printArray (a , n ); insertionSort (a , n ); p r i n t f ( ” \n\ n S o r t e d o u t p u t \n” ) ; printArray (a , n ); }
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Counting Sort For each element count the number of other elements which are greater.
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
7
0
6
0
3
0
1
0
2
0
5
0
4
0 1
3
6
7
0
1
0
2
0
5
0
4
1 1
3
6
7
0
2
0
2
0
5
0
4
1 1
3
6
7
0
3
0
2
0
5
0
4
1 4
5
2
R. K. Ghosh (IIT-Kanpur)
1
3
6
7
0
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Counting Sort void countingSort ( int a [ ] , int n) { i n t j , k , b [ n ] , cnt [ n ] ; f o r ( j = 0 ; j < n ; j ++) cnt [ j ] = 0; // C o u n t e r s
initialized
f o r ( j = 0 ; j < n ; j ++) { f o r ( k = 0 ; k < n ; k++) { if (a[ j ] < a[k ]) // a [ k ] s h d c n t [ k ]++; i f ( a [ j ] == a [ k ] && k > j ) // c n t [ k ]++; // } } /∗ R e m a i n i n g p a r t o f f u n c t i o n a p p e a r s
appear a f t e r a [ j ] m a i n t a i n r e l a t i v e pos . of equal elements
i n n e x t s l i d e ∗/
}
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Sorting Counting Sort (contd.) /∗ P l a c i n g a [ j ] i n p o s i t i o n a [ c n t [ j ] ] c o m p l e t e s s o r t i n g . ∗/ f o r ( j = 0 ; j < n ; j ++) b [ cnt [ j ] ] = a [ j ] ;
// Use t e m p o r a r y a r r a y
f o r ( j = 0 ; j < n ; j ++) a[ j ] = b[ j ];
// T r a n s f e r b a c k
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
3/5
C Programming Arrays Sorting
Searching Linear Search Another very frequently used operation. Sequential search: look at each element from the beginning and match with the target x. Returns index of matched element if x is found. Returns -1 if x is does not occur.
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
4/5
C Programming Arrays Sorting
Searching Code for Linear Search int sequentialSearch ( int a [] , int i ;
int n , int x) {
f o r ( i = 0 ; i < n ; i ++) i f ( a [ i ] == x ) b r e a k ; i f ( i < n) return i ; else r e t u r n −1 }
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
4/5
C Programming Arrays Sorting
Searching Code for Linear Search #d e f i n e N 10 i n t main ( ) { i n t a [ ] = {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10}; int x , i ; i n t n = s i z e o f ( a )/ s i z e o f ( a [ 0 ] ) ; p r i n t f (” Enter t a r g e t element : ” ) ; s c a n f ( ”%d” , &x ) ; i = sequentialSearch (a , n , x ); i f ( i == −1) p r i n t f ( ”%d i s n o t f o u n d ! \ n” , x ) ; else p r i n t f ( ”%d f o u n d a t p o s i t i o n %d \n” , x , i ) ; }
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
4/5
C Programming Arrays Sorting
Searching Binary Search l
search element x=12
h
0
1
2
3
4
5
6
7
8
9
6
8
12
14
16
17
20 25
28
30
20
28
30
m=4 6
8
12
14
16
m=2
h
l
17
25
12 = x
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
4/5
C Programming Arrays Sorting
Searching Binary Search int binarySearch ( int a [ ] , int l , int h , int x) { i n t m; w h i l e ( l i n t main ( ) { i n t a [N] = {0 , 1 , 2 , 3 , 5 , 6 , 8 , 9 , 11 , 12}; i n t n = s i z e o f ( a )/ s i z e o f ( a [ 0 ] ) ; int i , x ; p r i n t f ( ” E n t e r t h e number t o be s e a r c h e d : ” ) ; s c a n f ( ”%d” , &x ) ; p r i n t f ( ” I n p u t \n” ) ; printArray (a , n ); i = b i n a r y S e a r c h ( a , 0 , n −1 , x ) ; i f ( i == −1) p r i n t f ( ”%d i s n o t f o u n d ! \ n” , x ) ; else p r i n t f ( ”%d f o u n d a t p o s i t i o n %d \n” , x , i ) ; }
R. K. Ghosh (IIT-Kanpur)
C Programming
February 21, 2011
4/5