-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ques-2.cpp
208 lines (178 loc) · 5.17 KB
/
Ques-2.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
QUESTION-2:
Write a program using templates to sort a list of elements. Give user the option to perform sorting using Insertion sort, Bubble sort or Selection sort.
SOLUTION:
*/
#include<iostream>
using namespace std;
template <class X_Type> class SortingOfArray
{
public:
X_Type *arr;
int size;
X_Type temp , a , b ;
X_Type input();
X_Type choice();
X_Type selection_sort();
X_Type bubble_sort();
X_Type insertion_sort();
};
template <class X_Type> X_Type SortingOfArray<X_Type> :: choice()
{
int c;
char ch;
SortingOfArray<int> a;
SortingOfArray<char> b;
SortingOfArray<float> f;
do
{
cout<<"1. For Integer type "<<endl;
cout<<"2. For Character type "<<endl;
cout<<"3. For float type "<<endl;
cout<<endl<<"Enter your choice : ";
cin>>c;
switch(c)
{
case 1 :
{
a.input();
a.selection_sort();
break;
}
case 2 :
{
b.input();
b.bubble_sort();
break;
}
case 3 :
{
f.input();
f.insertion_sort();
break;
}
default : cout<<"Enter correct choice "<<endl;
}
cout<<endl<<"Do you want to continue for other data types press (y/n) "<<endl;
cin>>ch;
}
while(ch=='y' || ch=='Y');
}
template <class X_Type> X_Type SortingOfArray<X_Type> :: input()
{
cout<<"Enter the size of an array : "<<" ";
cin>>size;
arr=new X_Type[size];
cout<<"Enter the element of array : "<<endl;
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
}
//selection sort
template <class X_Type> X_Type SortingOfArray<X_Type> ::selection_sort()
{
for(int i=0;i<size;i++)
{
temp = arr[i];
a = i;
for(int j=i+1;j<size;j++)
{
if(arr[j]<temp)
{
temp=arr[j];
a=j;
}
}
b = arr[i];
arr[i] = arr[a];
arr[a] = b;
}
cout<<"Sorted array is : ";
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
}
//bubble sort
template <class X_Type> X_Type SortingOfArray<X_Type> :: bubble_sort()
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size-1-i;j++)
{
if(arr[j]>=arr[j+1])
{
b = arr[j];
arr[j] = arr[j+1];
arr[j+1] = b;
}
}
}
cout<<"Sorted array is : ";
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
}
//insertion sort
template <class X_Type> X_Type SortingOfArray<X_Type> :: insertion_sort()
{
for(int i=1;i<size;i++)
{
temp=arr[i];
int j=i-1;
while((arr[j]>temp)&&(j>=0))
{
arr[j+1] = arr[j];
j--;
}
arr[j+1]=temp;
}
cout<<"Sorted array is : ";
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int c;
char ch;
cout << "\n\t ~~~~~~~~~~~~~~~~~~~~~~~Practical 2~~~~~~~~~~~~~~~~~~~~\n\t\t\t\t \n";
do
{
cout<<"Press 1 for selection sort --"<<endl;
cout<<"Press 2 for bubble sort --"<<endl;
cout<<"Press 3 for insertion sort --"<<endl;
cout<<endl<<"Enter your choice : ";
cin>>c;
switch(c)
{
case 1:
{
SortingOfArray<int>ob1;
ob1.choice();
break;
}
case 2:
{
SortingOfArray<int>ob2;
ob2.choice();
break;
}
case 3:
{
SortingOfArray<int>ob3;
ob3.choice();
break;
}
default:
cout<<endl<<"!!!!!!Wrong choice"<<endl;
break;
}
cout<<endl<<"Do you want to continue for sorting by other types , enter (y/e/s) : " ;
cin>>ch;
}while(ch=='y'||ch=='e'||ch=='s');
return 0;
}