sorting fixes for MSVC and CPPLINT

This commit is contained in:
Krishna Vedala 2020-05-26 11:46:24 -04:00
parent bfeffe532a
commit 76a5f572d5
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
3 changed files with 43 additions and 60 deletions

View File

@ -12,8 +12,8 @@ void beadSort(int *a, int len) {
if (a[i] > max) max = a[i];
// allocating memory
unsigned char beads[max * len];
memset(beads, 0, sizeof(beads));
unsigned char *beads = new unsigned char[max * len];
memset(beads, 0, max * len);
// mark the beads
for (int i = 0; i < len; i++)
@ -39,6 +39,7 @@ void beadSort(int *a, int len) {
a[i] = j;
}
delete[] beads;
}
// driver function to test the algorithm

View File

@ -1,42 +1,35 @@
// C++ program to sort an array using bucket sort
#include <iostream>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Function to sort arr[] of size n using bucket sort
void bucketSort(float arr[], int n)
{
void bucketSort(float arr[], int n) {
// 1) Create n empty buckets
vector<float> b[n];
std::vector<float> b[n];
// 2) Put array elements in different buckets
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
int bi = n * arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}
// 3) Sort individual buckets
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());
for (int i = 0; i < n; i++) std::sort(b[i].begin(), b[i].end());
// 4) Concatenate all buckets into arr[]
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
for (int j = 0; j < b[i].size(); j++) arr[index++] = b[i][j];
}
/* Driver program to test above funtion */
int main()
{
int main() {
float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);
cout << "Sorted array is \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
std::cout << "Sorted array is \n";
for (int i = 0; i < n; i++) std::cout << arr[i] << " ";
return 0;
}

View File

@ -1,59 +1,48 @@
//Kind of better version of Bubble sort.
//While Bubble sort is comparering adjacent value, Combsort is using gap larger than 1
//Best case: O(n)
//Worst case: O(n ^ 2)
// Kind of better version of Bubble sort.
// While Bubble sort is comparering adjacent value, Combsort is using gap larger
// than 1 Best case: O(n) Worst case: O(n ^ 2)
#include <iostream>
using namespace std;
int a[100005];
int n;
int FindNextGap(int x)
{
int FindNextGap(int x) {
x = (x * 10) / 13;
return max(1, x);
return std::max(1, x);
}
void CombSort(int a[], int l, int r)
{
//Init gap
void CombSort(int a[], int l, int r) {
// Init gap
int gap = n;
//Initialize swapped as true to make sure that loop runs
// 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)
{
//Find next gap
// Keep running until gap = 1 or none elements were 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])
{
swap(a[i], a[i + gap]);
for (int i = l; i <= r - gap; ++i) {
if (a[i] > a[i + gap]) {
std::swap(a[i], a[i + gap]);
swapped = true;
}
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
int main() {
std::cin >> n;
for (int i = 1; i <= n; ++i) std::cin >> a[i];
CombSort(a, 1, n);
for (int i = 1; i <= n; ++i)
cout << a[i] << ' ';
for (int i = 1; i <= n; ++i) std::cout << a[i] << ' ';
return 0;
}