style: format code

This commit is contained in:
yanglbme
2019-08-21 10:10:08 +08:00
parent abc0d365de
commit 69ddc9fc52
101 changed files with 3154 additions and 2984 deletions

View File

@@ -1,5 +1,5 @@
//Kind of better version of Bubble sort.
//While Bubble sort is comparering adjacent value, Combsort is using gap larger than 1
//While Bubble sort is comparering adjacent value, Combsort is using gap larger than 1
//Best case: O(n)
//Worst case: O(n ^ 2)
@@ -10,29 +10,34 @@ using namespace std;
int a[100005];
int n;
int FindNextGap(int x) {
int FindNextGap(int x)
{
x = (x * 10) / 13;
return max(1, x);
}
void CombSort(int a[], int l, int r) {
void CombSort(int a[], int l, int r)
{
//Init gap
int gap = n;
//Initialize swapped as true to make sure that loop runs
bool swapped = true;
//Keep running until gap = 1 or none elements were swapped
while (gap != 1 || swapped) {
while (gap != 1 || swapped)
{
//Find next gap
gap = FindNextGap(gap);
swapped = false;
// Compare all elements with current gap
for(int i = l; i <= r - gap; ++i) {
if (a[i] > a[i + gap]) {
for (int i = l; i <= r - gap; ++i)
{
if (a[i] > a[i + gap])
{
swap(a[i], a[i + gap]);
swapped = true;
}
@@ -40,12 +45,15 @@ void CombSort(int a[], int l, int r) {
}
}
int main() {
int main()
{
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i)
cin >> a[i];
CombSort(a, 1, n);
for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
for (int i = 1; i <= n; ++i)
cout << a[i] << ' ';
return 0;
}