-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsort1.c
73 lines (56 loc) · 1.35 KB
/
bsort1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int compar ( void *p1, void *p2 )
{
if(*(int*)p1 > *(int*)p2)
return 1;
return 0;
}
int comparstr ( void *p1, void *p2 )
{
return strcmp ( (char*)p1, (char*)p2 );
}
void swap ( void *a , void *b, int size, void *temp )
{
memcpy ( temp, a, size );
memcpy ( a, b, size );
memcpy ( b, temp, size );
}
void bsort ( void *base, int num, int size, int (*compar) ( void*, void * ))
{
int i, j;
void *temp;
temp=malloc(sizeof(size));
for ( i = 0 ; i < num ; i++ )
{
for ( j = 0 ; j < num - 1 - i ; j++ )
{
if ( compar ( (char*)base+j*size, (char*)base+(j+1)*size ) > 0 )
swap ( (char*)base+j*size, (char*)base+(j+1)*size, size, temp );
}
}
free ( temp );
}
int main()
{
int n, a [ 10 ], i;
char s [ 10 ] [ 10 ];
char **s;
printf ( "\nEnter the number of elements: " );
scanf( "%d", &n );
/*printf ( "\nEnter the elements: " );
for ( i = 0 ; i < n ; i++ )
scanf ( "%d", &a [ i ] ); */
printf ( "\nEnter the strings: " );
for ( i = 0 ; i < n ; i++ )
scanf ( "%s", s [ i ] );
/*bsort ( a, n, sizeof(int), compar ); */
bsort ( s, n, sizeof(s[0]),comparstr );
/*printf ( "\nThe sorted elements are: " );
for ( i = 0 ; i < n ; i++ )
printf ( "%d\n", a [ i ] );*/
printf ( "\nThe sorted strings are: " );
for ( i = 0 ; i < n ; i++ )
printf ( "%s\n", s [ i ] );
}