Skip to content

Commit 97c6855

Browse files
authored
Merge pull request #82 from harshchan/master
Add files via upload to SPOJ
2 parents 06c0ce2 + b423bc4 commit 97c6855

File tree

5 files changed

+410
-0
lines changed

5 files changed

+410
-0
lines changed

Algorithms/Sorting/comb_sort.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// C++ implementation of Comb Sort
2+
3+
//facebook,netflix
4+
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
// To find gap between elements
9+
int getNextGap(int gap)
10+
{
11+
// Shrink gap by Shrink factor
12+
gap = (gap*10)/13;
13+
14+
if (gap < 1)
15+
return 1;
16+
return gap;
17+
}
18+
19+
// Function to sort a[0..n-1] using Comb Sort
20+
void combSort(int a[], int n)
21+
{
22+
// Initialize gap
23+
int gap = n;
24+
25+
// Initialize swapped as true to make sure that loop runs
26+
bool swapped = true;
27+
28+
// Keep running while gap is more than 1 and last iteration caused a swap
29+
while (gap != 1 || swapped == true)
30+
{
31+
// Find next gap
32+
gap = getNextGap(gap);
33+
34+
// Initialize swapped as false so that we can check if swap happened or not
35+
swapped = false;
36+
37+
// Compare all elements with current gap
38+
for (int i=0; i<n-gap; i++)
39+
{
40+
if (a[i] > a[i+gap])
41+
{
42+
swap(a[i], a[i+gap]);
43+
swapped = true;
44+
}
45+
}
46+
}
47+
}
48+
49+
int main()
50+
{
51+
int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
52+
int n = sizeof(a)/sizeof(a[0]);
53+
54+
combSort(a, n);
55+
56+
printf("Sorted array: \n");
57+
for (int i=0; i<n; i++)
58+
printf("%d ", a[i]);
59+
60+
return 0;
61+
}// C++ implementation of Comb Sort
62+
#include<bits/stdc++.h>
63+
using namespace std;
64+
65+
// To find gap between elements
66+
int getNextGap(int gap)
67+
{
68+
// Shrink gap by Shrink factor
69+
gap = (gap*10)/13;
70+
71+
if (gap < 1)
72+
return 1;
73+
return gap;
74+
}
75+
76+
// Function to sort a[0..n-1] using Comb Sort
77+
void combSort(int a[], int n)
78+
{
79+
// Initialize gap
80+
int gap = n;
81+
82+
// Initialize swapped as true to make sure that loop runs
83+
bool swapped = true;
84+
85+
// Keep running while gap is more than 1 and last iteration caused a swap
86+
while (gap != 1 || swapped == true)
87+
{
88+
// Find next gap
89+
gap = getNextGap(gap);
90+
91+
// Initialize swapped as false so that we can check if swap happened or not
92+
swapped = false;
93+
94+
// Compare all elements with current gap
95+
for (int i=0; i<n-gap; i++)
96+
{
97+
if (a[i] > a[i+gap])
98+
{
99+
swap(a[i], a[i+gap]);
100+
swapped = true;
101+
}
102+
}
103+
}
104+
}
105+
106+
int main()
107+
{
108+
int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
109+
int n = sizeof(a)/sizeof(a[0]);
110+
111+
combSort(a, n);
112+
113+
printf("Sorted array: \n");
114+
for (int i=0; i<n; i++)
115+
printf("%d ", a[i]);
116+
117+
return 0;
118+
}

Algorithms/Sorting/combsort.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// C++ implementation of Comb Sort
2+
3+
//facebook,netflix
4+
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
// To find gap between elements
9+
int getNextGap(int gap)
10+
{
11+
// Shrink gap by Shrink factor
12+
gap = (gap*10)/13;
13+
14+
if (gap < 1)
15+
return 1;
16+
return gap;
17+
}
18+
19+
// Function to sort a[0..n-1] using Comb Sort
20+
void combSort(int a[], int n)
21+
{
22+
// Initialize gap
23+
int gap = n;
24+
25+
// Initialize swapped as true to make sure that loop runs
26+
bool swapped = true;
27+
28+
// Keep running while gap is more than 1 and last iteration caused a swap
29+
while (gap != 1 || swapped == true)
30+
{
31+
// Find next gap
32+
gap = getNextGap(gap);
33+
34+
// Initialize swapped as false so that we can check if swap happened or not
35+
swapped = false;
36+
37+
// Compare all elements with current gap
38+
for (int i=0; i<n-gap; i++)
39+
{
40+
if (a[i] > a[i+gap])
41+
{
42+
swap(a[i], a[i+gap]);
43+
swapped = true;
44+
}
45+
}
46+
}
47+
}
48+
49+
int main()
50+
{
51+
int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
52+
int n = sizeof(a)/sizeof(a[0]);
53+
54+
combSort(a, n);
55+
56+
printf("Sorted array: \n");
57+
for (int i=0; i<n; i++)
58+
printf("%d ", a[i]);
59+
60+
return 0;
61+
}// C++ implementation of Comb Sort
62+
#include<bits/stdc++.h>
63+
using namespace std;
64+
65+
// To find gap between elements
66+
int getNextGap(int gap)
67+
{
68+
// Shrink gap by Shrink factor
69+
gap = (gap*10)/13;
70+
71+
if (gap < 1)
72+
return 1;
73+
return gap;
74+
}
75+
76+
// Function to sort a[0..n-1] using Comb Sort
77+
void combSort(int a[], int n)
78+
{
79+
// Initialize gap
80+
int gap = n;
81+
82+
// Initialize swapped as true to make sure that loop runs
83+
bool swapped = true;
84+
85+
// Keep running while gap is more than 1 and last iteration caused a swap
86+
while (gap != 1 || swapped == true)
87+
{
88+
// Find next gap
89+
gap = getNextGap(gap);
90+
91+
// Initialize swapped as false so that we can check if swap happened or not
92+
swapped = false;
93+
94+
// Compare all elements with current gap
95+
for (int i=0; i<n-gap; i++)
96+
{
97+
if (a[i] > a[i+gap])
98+
{
99+
swap(a[i], a[i+gap]);
100+
swapped = true;
101+
}
102+
}
103+
}
104+
}
105+
106+
int main()
107+
{
108+
int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
109+
int n = sizeof(a)/sizeof(a[0]);
110+
111+
combSort(a, n);
112+
113+
printf("Sorted array: \n");
114+
for (int i=0; i<n; i++)
115+
printf("%d ", a[i]);
116+
117+
return 0;
118+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<iostream>
2+
#include<conio.h>
3+
#include<stdlib.h>
4+
#include<bits/stdc++.h>
5+
6+
#define int long long int
7+
#define pb push_back
8+
#define ps(x,y) fixed<<setprecision(y)<<x
9+
#define mod 1000000007
10+
#define w(x) int x; cin>>x; while(x--)
11+
using namespace std;
12+
13+
14+
int32_t main() {
15+
16+
w(T)
17+
{
18+
int n,c,ans;
19+
cin>>n>>c;
20+
int arr[n];
21+
for(int i=0;i<n;i++) cin>>arr[i];
22+
23+
sort(arr,arr+n);
24+
int low=arr[0],high=arr[n-1]-arr[0];
25+
26+
while(low<=high)
27+
{
28+
int prev=arr[0],mid=(low+high)>>1;
29+
int cow_count=1;
30+
for(int i=1;i<n;i++)
31+
{
32+
if(arr[i]-prev >= mid)
33+
{
34+
cow_count++; prev=arr[i];
35+
if(c==cow_count)break;
36+
}
37+
}
38+
if(cow_count>=c)
39+
{
40+
low=mid+1;ans=mid;
41+
}
42+
else
43+
{
44+
high=mid-1;
45+
}
46+
47+
}
48+
cout<<ans<<endl;
49+
50+
}
51+
52+
53+
return 0;
54+
}
55+
56+
57+
58+

Spoj/Practice/CPP/eko_eko.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include<iostream>
2+
#include<stdlib.h>
3+
#include<bits/stdc++.h>
4+
5+
#define int long long int
6+
#define pb push_back
7+
#define ps(x,y) fixed<<setprecision(y)<<x
8+
#define mod 1000000007
9+
#define w(x) int x; cin>>x; while(x--)
10+
using namespace std;
11+
12+
int mid_wood(int arr[],int m,int mid,int n)
13+
{int wood=0;
14+
cout<<"Inside fn";
15+
for(int i=0;i<n;i++)
16+
{
17+
if(arr[i]-mid>0)
18+
{
19+
wood=wood+arr[i]-mid;
20+
}
21+
}
22+
return wood;
23+
}
24+
25+
int32_t main() {
26+
27+
w(T)
28+
{
29+
int n,m,ans;
30+
cin>>n>>m;
31+
int arr[n];
32+
for(int i=0;i<n;i++) cin>>arr[i];
33+
34+
sort(arr,arr+n);
35+
int low=0,high=arr[n-1];
36+
while(low<=high)
37+
{cout<<"inside while";
38+
int mid=(low+high)/2;6
39+
if(m<=mid_wood(arr,m,mid,n))
40+
{
41+
ans=mid;low=mid+1;
42+
}
43+
else high=mid-1;
44+
}
45+
cout<<ans<<endl;
46+
47+
}
48+
49+
50+
return 0;
51+
}
52+
53+
54+
55+

0 commit comments

Comments
 (0)