-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 5
More file actions
181 lines (169 loc) · 3.14 KB
/
Assignment 5
File metadata and controls
181 lines (169 loc) · 3.14 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
ASSIGNMENT NO.5
TITLE: a. Sort the set of strings in ascending order Bubble Sort and descending order using Selection sort or insertion sort. (Display pass by pass output).
b. Search Particular string using binary search with and without recursion.
#include<stdio.h>
#include<string.h>
#define max 100
voidbubblesort(char arr[][max],int n);
voidselectionsort(char arr[][max],int n);
intbinsearchwrec(char arr[][max],intn,char key[]);
intbinsearchrec(char arr[][max],intfirst,intlast,char key[]);
void main()
{
chararr[100][max];
char key[max];
inti,j,ch,n,s,x;
printf("Enter the number of strings=");
scanf("%d",&n);
printf("\nEnter the strings=");
for(i=0;i<n;i++)
{
scanf("%s",&arr[i]);
}
do
{
printf("\n1.bubblesort/n2.selection sort/n3.Binary Search Without rec/n4.Binary Search With rec");
printf("\n Enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: bubblesort(arr,n);
break;
case 2:selectionsort(arr,n);
break;
case 3:bubblesort(arr,n);
printf("\nEnter the string you want to search=");
scanf("%s",&key);
s=binsearchwrec(arr,n,key);
if(s==-1)
{
printf("\nElement found");
}
else
{
printf("\nElement not found");
}
break;
case 4:bubblesort(arr,n);
printf("\nEnter the string you want to search=");
scanf("%s",&key);
x=binsearchrec(arr,1,n,key);
if(x==-1)
{
printf("\nElement found");
}
else
{
printf("\nElement not found");
}
break;
default:printf("\nInvalid choice");
break;
}
}while(i!=0);
}
voidbubblesort(char arr[][max],int n)
{
inti,j,k;
char temp[100];
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{
if(strcmp(arr[i],arr[i+1])>0)
{
strcpy(temp,arr[i]);
strcpy(arr[i],arr[i+1]);
strcpy(arr[i+1],temp);
}
}
printf("\nPASS%d=",j+1);
for(k=0;k<n;k++)
{
printf("\t%s",arr[k]);
}
}
printf("\nBUBBLESORTED SET OF STRINGS=>");
for(i=0;i<n;i++)
{
printf("\n%s",arr[i]);
}
}
voidselectionsort(char arr[][max],int n)
{
inti,j,loc,f,k;
char min[100],temp[100];
strcpy(min,arr[0]);
for(i=0;i<n-1;i++)
{
strcpy(min,arr[i]);
loc=i;
for(j=i+1;j<=n-1;j++)
{
if(strcmp(arr[j],min)>0)
{
strcpy(min,arr[j]);
loc=j;
}
}
if(loc!=i)
{
strcpy(temp,arr[i]);
strcpy(arr[i],arr[loc]);
strcpy(arr[loc],temp);
}
printf("\nPASS%d=",i+1);
for(k=0;k<n;k++)
{
printf("\t%s",arr[k]);
}
}
printf("\nSET OF STRINGS BY SELECTION SORT=>");
for(i=0;i<n;i++)
{
printf("\n%s",arr[i]);
}
}
intbinsearchwrec(char arr[][max],intn,char key[])
{
intmid,pass=0;
int first=1,last=n;
while(last<=first)
{
mid=(first+last)/2;
if(strcmp(key,arr[mid])==0)
{
return(mid);
}
else if(strcmp(key,arr[mid])>0)
{
first=mid+1;
}
else
{
last=mid-1;
}
}
return(-1);
}
intbinsearchrec(char arr[][max],intfirst,intlast,char key[])
{
intmid,pass=0;
while(first>=last)
{
mid=(first+last)/2;
if(strcmp(key,arr[mid])==0)
{
return(mid);
}
else if(strcmp(key,arr[mid])>0)
{
return(binsearchrec(arr,mid+1,last,key));
}
else
{
return(binsearchrec(arr,first,mid-1,key));
}
}
return(-1);
}