-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
124 lines (119 loc) · 2.61 KB
/
quick_sort.cpp
File metadata and controls
124 lines (119 loc) · 2.61 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
#include <iostream>
#include <string>
using namespace std;
template <typename T>
void MySwap(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
template <typename T>
void quick_sort(T array[], int start, int end)
{
if (start < end)
{
T ptr = array[start];
int i = start;
int j = end;
while (i < j)
{
while (array[i] <= ptr && i < end)
{
i++;
}
while (array[j] >= ptr && j > start)
{
j--;
}
if (i < j)
{
MySwap(array[i], array[j]);
}
}
MySwap(array[j], array[start]);
quick_sort(array, start, j - 1);
quick_sort(array, j + 1, end);
}
}
template <typename T>
void show(T A[], int size)
{
for (int i = 0 ; i < size; ++i)
{
cout << " A[ " << i << " ] = " << A[i] << endl;
}
}
template <typename T>
void array_data(T A[], int size)
{
for (int i = 0; i<size; i++)
{
cout<<"enter data at index "<<i<<" : ";
cin>>A[i];
}
cout<<"Before sorting :\n";
show(A,size);
quick_sort(A,0,size-1);// code // enter the sorting function
cout<<"After sorting :\n";
show(A,size);
}
int main ()
{
char ch = 'y';
while (ch == 'y')
{
int size;
cout<<"enter the size of the array : ";
cin>>size;
cout << "Valid data types are int, float, double, char, string\n";
string datatype;
cout << "Enter data type of the array: ";
cin >> datatype;
if (datatype == "int")
{
int*A = new int[size];
array_data(A,size);
delete[] A;
}
else if (datatype == "char")
{
char*A = new char[size];
array_data(A,size);
delete[] A;
}
else if (datatype == "float")
{
float*A = new float[size];
array_data(A,size);
delete[] A;
}
else if (datatype == "double")
{
double*A = new double[size];
array_data(A,size);
delete[] A;
}
else if (datatype == "string")
{
string*A = new string[size];
array_data(A,size);
delete[] A;
}
else
{
cout << "Invalid data type\n";
}
cout << "Do you want to continue? (y/n): ";
cin >> ch;
}
if(ch == 'n')
{
cout << "Goodbye!\n";
}
else if (ch != 'y' && ch != 'n')
{
cout << "Invalid input\n";
}
return 0;
}