-
Notifications
You must be signed in to change notification settings - Fork 0
/
revise.cpp
105 lines (95 loc) · 1.93 KB
/
revise.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
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
if(a==0){
return b;
}
return gcd(b%a, a);
}
void selectionSort(vector<int> &arr, int n){
int min;
for(int i=0; i<n-1; i++){
min = i;
for(int j=i+1; j<n; j++){
if(arr[min]>arr[j]){
min = j;
}
}
if(min!=i){
swap(arr[min], arr[i]);
}
}
}
void bubbleSort(vector<int> &arr, int n){
for(int i=0; i<n; i++){
for(int j=0; j<n-i-1; j++){
if(arr[j]>arr[j+1]){
swap(arr[j], arr[j+1]);
}
}
}
}
void stringMatch(string s, string p){
int n = s.size();
int m = p.size();
for(int i=0; i<=n-m; i++){
int j = 0;
int count = 0;
while(j<m && s[i+j]==p[j]){
j++;
}
if(j==m){
cout<<"Pattern is at "<<j<<" "<<j+i<<endl;
return;
}
}
cout<<"not found";
}
void merge(vector<int> &arr, vector<int> a, vector<int> b, int n){
int i=0, j=0, k=0;
int lena = n/2;
int lenb = n-(n/2);
while(i<lena && j<lenb){
if(a[i]<b[j]){
arr[k] = a[i];
i++;
}else{
arr[k] = b[j];
j++;
}
k++;
}
while(i<lena){
arr[k] = a[i];
k++;i++;
}
while(j<lenb){
arr[k] = b[j];
k++;j++;
}
}
void mergeSort(vector<int> &arr, int n){
if(n<=1){
return;
}
vector<int> a(n/2), b((n-(n/2)));
for(int i=0; i<n/2; i++){
a[i] = arr[i];
}
for(int i=0; i<n-(n/2); i++){
b[i] = arr[i+(n/2)];
}
mergeSort(a, n/2);
mergeSort(b, n-(n/2));
merge(arr, a, b, n);
}
int main(){
vector<int> arr = {5,8,3,7,2,0,10,4,2,9,1,6};
int n = arr.size();
mergeSort(arr, n);
for(int i: arr){
cout<<i<<"\t";
}
cout<<endl;
return 0;
}