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];
|
if (a[i] > max) max = a[i];
|
||||||
|
|
||||||
// allocating memory
|
// allocating memory
|
||||||
unsigned char beads[max * len];
|
unsigned char *beads = new unsigned char[max * len];
|
||||||
memset(beads, 0, sizeof(beads));
|
memset(beads, 0, max * len);
|
||||||
|
|
||||||
// mark the beads
|
// mark the beads
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
@ -39,6 +39,7 @@ void beadSort(int *a, int len) {
|
|||||||
|
|
||||||
a[i] = j;
|
a[i] = j;
|
||||||
}
|
}
|
||||||
|
delete[] beads;
|
||||||
}
|
}
|
||||||
|
|
||||||
// driver function to test the algorithm
|
// driver function to test the algorithm
|
||||||
|
@ -1,42 +1,35 @@
|
|||||||
// C++ program to sort an array using bucket sort
|
// C++ program to sort an array using bucket sort
|
||||||
#include <iostream>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
// Function to sort arr[] of size n using bucket sort
|
// 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
|
||||||
// 1) Create n empty buckets
|
std::vector<float> b[n];
|
||||||
vector<float> b[n];
|
|
||||||
|
|
||||||
// 2) Put array elements in different buckets
|
// 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
|
||||||
int bi = n * arr[i]; // Index in bucket
|
b[bi].push_back(arr[i]);
|
||||||
b[bi].push_back(arr[i]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 3) Sort individual buckets
|
// 3) Sort individual buckets
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++) std::sort(b[i].begin(), b[i].end());
|
||||||
sort(b[i].begin(), b[i].end());
|
|
||||||
|
|
||||||
// 4) Concatenate all buckets into arr[]
|
// 4) Concatenate all buckets into arr[]
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++)
|
||||||
for (int j = 0; j < b[i].size(); j++)
|
for (int j = 0; j < b[i].size(); j++) arr[index++] = b[i][j];
|
||||||
arr[index++] = b[i][j];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Driver program to test above funtion */
|
/* Driver program to test above funtion */
|
||||||
int main()
|
int main() {
|
||||||
{
|
float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
|
||||||
float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
|
int n = sizeof(arr) / sizeof(arr[0]);
|
||||||
int n = sizeof(arr) / sizeof(arr[0]);
|
bucketSort(arr, n);
|
||||||
bucketSort(arr, n);
|
|
||||||
|
|
||||||
cout << "Sorted array is \n";
|
std::cout << "Sorted array is \n";
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++) std::cout << arr[i] << " ";
|
||||||
cout << arr[i] << " ";
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
@ -1,59 +1,48 @@
|
|||||||
//Kind of better version of Bubble sort.
|
// 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
|
||||||
//Best case: O(n)
|
// than 1 Best case: O(n) Worst case: O(n ^ 2)
|
||||||
//Worst case: O(n ^ 2)
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int a[100005];
|
int a[100005];
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
int FindNextGap(int x)
|
int FindNextGap(int x) {
|
||||||
{
|
|
||||||
x = (x * 10) / 13;
|
x = (x * 10) / 13;
|
||||||
|
|
||||||
return max(1, x);
|
return std::max(1, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CombSort(int a[], int l, int r)
|
void CombSort(int a[], int l, int r) {
|
||||||
{
|
// Init gap
|
||||||
//Init gap
|
|
||||||
int gap = n;
|
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;
|
bool swapped = true;
|
||||||
|
|
||||||
//Keep running until gap = 1 or none elements were swapped
|
// Keep running until gap = 1 or none elements were swapped
|
||||||
while (gap != 1 || swapped)
|
while (gap != 1 || swapped) {
|
||||||
{
|
// Find next gap
|
||||||
//Find next gap
|
|
||||||
gap = FindNextGap(gap);
|
gap = FindNextGap(gap);
|
||||||
|
|
||||||
swapped = false;
|
swapped = false;
|
||||||
|
|
||||||
// Compare all elements with current gap
|
// Compare all elements with current gap
|
||||||
for (int i = l; i <= r - gap; ++i)
|
for (int i = l; i <= r - gap; ++i) {
|
||||||
{
|
if (a[i] > a[i + gap]) {
|
||||||
if (a[i] > a[i + gap])
|
std::swap(a[i], a[i + gap]);
|
||||||
{
|
|
||||||
swap(a[i], a[i + gap]);
|
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
std::cin >> n;
|
||||||
cin >> n;
|
for (int i = 1; i <= n; ++i) std::cin >> a[i];
|
||||||
for (int i = 1; i <= n; ++i)
|
|
||||||
cin >> a[i];
|
|
||||||
|
|
||||||
CombSort(a, 1, n);
|
CombSort(a, 1, n);
|
||||||
|
|
||||||
for (int i = 1; i <= n; ++i)
|
for (int i = 1; i <= n; ++i) std::cout << a[i] << ' ';
|
||||||
cout << a[i] << ' ';
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user