-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorts.cpp
129 lines (118 loc) · 2.85 KB
/
sorts.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
// CODE FOR MENU DRIVEN SORTING METHODS
// INCLUDES SELECTION, BUBBLE, INSERTION AND QUICK SORTING METHODS
#include <bits/stdc++.h>
using namespace std;
void sel_sort(vector<int> &v, int n){
for(int i = 0; i < n-1; i++){
int min_indx = i;
for(int j = i; j < n; j++)
if(v[min_indx] > v[j])
min_indx = j;
swap(v[i], v[min_indx]);
}
}
void bub_sort(vector<int> &v, int n){
for(int i = 0; i < n-1; i++){
bool flag = false;
for(int j = 0; j < n-1-i; j++)
if(v[j] > v[j+1]){
swap(v[j], v[j+1]);
flag = true;
}
if(!flag)
break;
}
}
void ins_sort(vector<int> &v, int n){
for(int i = 1; i < n; i++){
int key = v[i];
int j = i - 1;
while(j > -1 && v[j] > key){
v[j+1] = v[j];
j--;
}
v[j+1] = key;
}
}
void swap_Elements(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int pviot_Fun(vector<int> &v, int min, int max)
{
int pivot = v[max];
int i = (min - 1);
for (int j = min; j <= max - 1; j++)
{
if (v[j] < pivot)
{
i++;
swap_Elements(&v[i], &v[j]);
}
}
swap_Elements(&v[i + 1], &v[max]);
return (i + 1);
}
void quick_Sort(vector<int> &v, int min, int max)
{
if (min < max)
{
int pi = pviot_Fun(v, min, max);
quick_Sort(v, min, pi - 1);
quick_Sort(v, pi + 1, max);
}
}
void display_Array(vector<int> &v ,int n)
{
cout << "After Sorting: ";
for(int i=0;i<n;i++){
cout << v[i] << " ";
}
}
int main(){
int n, ch, x;
vector<int> v(n);
do{
cout << "\n\tMENU\n1. Selection Sort\n2. Bubble Sort\n3. Insertion Sort\n4. Quick Sort\n5. Exit\nEnter your choice: ";
cin >> ch;
if(ch != 5){
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter Elements: ";
for(int i=0;i<n;i++){
cin >> x;
v.push_back(x);
}
}
if(ch == 1)
{
sel_sort(v, n);
display_Array(v,n);
}
else if(ch == 2)
{
bub_sort(v, n);
display_Array(v,n);
}
else if(ch == 3)
{
ins_sort(v, n);
display_Array(v,n);
}
else if(ch == 4)
{
quick_Sort(v, 0, n - 1);
display_Array(v,n);
}
else if(ch == 5)
break;
else
{
cout<<"Invalid Choice\n";
}
cout << "\n";
} while(ch != 5);
return 0;
}