mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
sorting fixes for MSVC and CPPLINT
This commit is contained in:
parent
bfeffe532a
commit
76a5f572d5
@ -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
|
||||
|
@ -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)
|
||||
{
|
||||
// 1) Create n empty buckets
|
||||
vector<float> b[n];
|
||||
void bucketSort(float arr[], int n) {
|
||||
// 1) Create n empty buckets
|
||||
std::vector<float> b[n];
|
||||
|
||||
// 2) Put array elements in different buckets
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int bi = n * arr[i]; // Index in bucket
|
||||
b[bi].push_back(arr[i]);
|
||||
}
|
||||
// 2) Put array elements in different buckets
|
||||
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());
|
||||
// 3) Sort individual buckets
|
||||
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];
|
||||
// 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];
|
||||
}
|
||||
|
||||
/* Driver program to test above funtion */
|
||||
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);
|
||||
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] << " ";
|
||||
return 0;
|
||||
std::cout << "Sorted array is \n";
|
||||
for (int i = 0; i < n; i++) std::cout << arr[i] << " ";
|
||||
return 0;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user