mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-14 02:30:40 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -5,8 +5,7 @@
|
||||
#define BEAD(i, j) beads[i * max + j]
|
||||
|
||||
// function to perform the above algorithm
|
||||
void beadSort(int *a, int len)
|
||||
{
|
||||
void beadSort(int *a, int len) {
|
||||
// Find the maximum element
|
||||
int max = a[0];
|
||||
for (int i = 1; i < len; i++)
|
||||
@@ -21,12 +20,10 @@ void beadSort(int *a, int len)
|
||||
for (int i = 0; i < len; i++)
|
||||
for (int j = 0; j < a[i]; j++) BEAD(i, j) = 1;
|
||||
|
||||
for (int j = 0; j < max; j++)
|
||||
{
|
||||
for (int j = 0; j < max; j++) {
|
||||
// count how many beads are on each post
|
||||
int sum = 0;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
for (int i = 0; i < len; i++) {
|
||||
sum += BEAD(i, j);
|
||||
BEAD(i, j) = 0;
|
||||
}
|
||||
@@ -36,11 +33,9 @@ void beadSort(int *a, int len)
|
||||
}
|
||||
|
||||
// Put sorted values in array using beads
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
for (int i = 0; i < len; i++) {
|
||||
int j;
|
||||
for (j = 0; j < max && BEAD(i, j); j++)
|
||||
{
|
||||
for (j = 0; j < max && BEAD(i, j); j++) {
|
||||
}
|
||||
|
||||
a[i] = j;
|
||||
@@ -49,8 +44,7 @@ void beadSort(int *a, int len)
|
||||
}
|
||||
|
||||
// driver function to test the algorithm
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int a[] = {5, 3, 1, 7, 4, 1, 1, 20};
|
||||
int len = sizeof(a) / sizeof(a[0]);
|
||||
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
/*The parameter dir indicates the sorting direction, ASCENDING
|
||||
or DESCENDING; if (a[i] > a[j]) agrees with the direction,
|
||||
then a[i] and a[j] are interchanged.*/
|
||||
void compAndSwap(int a[], int i, int j, int dir)
|
||||
{
|
||||
void compAndSwap(int a[], int i, int j, int dir) {
|
||||
if (dir == (a[i] > a[j]))
|
||||
std::swap(a[i], a[j]);
|
||||
}
|
||||
@@ -19,10 +18,8 @@ void compAndSwap(int a[], int i, int j, int dir)
|
||||
if dir = 1, and in descending order otherwise (means dir=0).
|
||||
The sequence to be sorted starts at index position low,
|
||||
the parameter cnt is the number of elements to be sorted.*/
|
||||
void bitonicMerge(int a[], int low, int cnt, int dir)
|
||||
{
|
||||
if (cnt > 1)
|
||||
{
|
||||
void bitonicMerge(int a[], int low, int cnt, int dir) {
|
||||
if (cnt > 1) {
|
||||
int k = cnt / 2;
|
||||
for (int i = low; i < low + k; i++) compAndSwap(a, i, i + k, dir);
|
||||
bitonicMerge(a, low, k, dir);
|
||||
@@ -33,10 +30,8 @@ void bitonicMerge(int a[], int low, int cnt, int dir)
|
||||
/* This function first produces a bitonic sequence by recursively
|
||||
sorting its two halves in opposite sorting orders, and then
|
||||
calls bitonicMerge to make them in the same order */
|
||||
void bitonicSort(int a[], int low, int cnt, int dir)
|
||||
{
|
||||
if (cnt > 1)
|
||||
{
|
||||
void bitonicSort(int a[], int low, int cnt, int dir) {
|
||||
if (cnt > 1) {
|
||||
int k = cnt / 2;
|
||||
|
||||
// sort in ascending order since dir here is 1
|
||||
@@ -56,8 +51,7 @@ void bitonicSort(int a[], int low, int cnt, int dir)
|
||||
void sort(int a[], int N, int up) { bitonicSort(a, 0, N, up); }
|
||||
|
||||
// Driver code
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int a[] = {3, 7, 4, 8, 6, 2, 1, 5};
|
||||
int N = sizeof(a) / sizeof(a[0]);
|
||||
|
||||
|
||||
@@ -40,8 +40,7 @@ optimized bubble sort algorithm. It's right down there.
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
bool swap_check = true;
|
||||
std::cout << "Enter the amount of numbers to sort: ";
|
||||
@@ -51,20 +50,16 @@ int main()
|
||||
int num;
|
||||
|
||||
// Input
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cin >> num;
|
||||
numbers.push_back(num);
|
||||
}
|
||||
|
||||
// Bubble Sorting
|
||||
for (int i = 0; (i < n) && (swap_check); i++)
|
||||
{
|
||||
for (int i = 0; (i < n) && (swap_check); i++) {
|
||||
swap_check = false;
|
||||
for (int j = 0; j < n - 1 - i; j++)
|
||||
{
|
||||
if (numbers[j] > numbers[j + 1])
|
||||
{
|
||||
for (int j = 0; j < n - 1 - i; j++) {
|
||||
if (numbers[j] > numbers[j + 1]) {
|
||||
swap_check = true;
|
||||
std::swap(numbers[j],
|
||||
numbers[j + 1]); // by changing swap location.
|
||||
@@ -77,14 +72,10 @@ int main()
|
||||
|
||||
// Output
|
||||
std::cout << "\nSorted Array : ";
|
||||
for (int i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
if (i != numbers.size() - 1)
|
||||
{
|
||||
for (int i = 0; i < numbers.size(); i++) {
|
||||
if (i != numbers.size() - 1) {
|
||||
std::cout << numbers[i] << ", ";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << numbers[i] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,12 @@
|
||||
#include <vector>
|
||||
|
||||
// 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
|
||||
std::vector<float> *b = new std::vector<float>[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]);
|
||||
}
|
||||
@@ -27,8 +25,7 @@ void bucketSort(float arr[], int n)
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
@@ -9,35 +9,27 @@
|
||||
|
||||
// Iterative Version
|
||||
|
||||
void CocktailSelectionSort(std::vector<int> *vec, int low, int high)
|
||||
{
|
||||
while (low <= high)
|
||||
{
|
||||
void CocktailSelectionSort(std::vector<int> *vec, int low, int high) {
|
||||
while (low <= high) {
|
||||
int minimum = (*vec)[low];
|
||||
int minimumindex = low;
|
||||
int maximum = (*vec)[high];
|
||||
int maximumindex = high;
|
||||
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
if ((*vec)[i] >= maximum)
|
||||
{
|
||||
for (int i = low; i <= high; i++) {
|
||||
if ((*vec)[i] >= maximum) {
|
||||
maximum = (*vec)[i];
|
||||
maximumindex = i;
|
||||
}
|
||||
if ((*vec)[i] <= minimum)
|
||||
{
|
||||
if ((*vec)[i] <= minimum) {
|
||||
minimum = (*vec)[i];
|
||||
minimumindex = i;
|
||||
}
|
||||
}
|
||||
if (low != maximumindex || high != minimumindex)
|
||||
{
|
||||
if (low != maximumindex || high != minimumindex) {
|
||||
std::swap((*vec)[low], (*vec)[minimumindex]);
|
||||
std::swap((*vec)[high], (*vec)[maximumindex]);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::swap((*vec)[low], (*vec)[high]);
|
||||
}
|
||||
|
||||
@@ -48,8 +40,7 @@ void CocktailSelectionSort(std::vector<int> *vec, int low, int high)
|
||||
|
||||
// Recursive Version
|
||||
|
||||
void CocktailSelectionSort_v2(std::vector<int> *vec, int low, int high)
|
||||
{
|
||||
void CocktailSelectionSort_v2(std::vector<int> *vec, int low, int high) {
|
||||
if (low >= high)
|
||||
return;
|
||||
|
||||
@@ -58,26 +49,20 @@ void CocktailSelectionSort_v2(std::vector<int> *vec, int low, int high)
|
||||
int maximum = (*vec)[high];
|
||||
int maximumindex = high;
|
||||
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
if ((*vec)[i] >= maximum)
|
||||
{
|
||||
for (int i = low; i <= high; i++) {
|
||||
if ((*vec)[i] >= maximum) {
|
||||
maximum = (*vec)[i];
|
||||
maximumindex = i;
|
||||
}
|
||||
if ((*vec)[i] <= minimum)
|
||||
{
|
||||
if ((*vec)[i] <= minimum) {
|
||||
minimum = (*vec)[i];
|
||||
minimumindex = i;
|
||||
}
|
||||
}
|
||||
if (low != maximumindex || high != minimumindex)
|
||||
{
|
||||
if (low != maximumindex || high != minimumindex) {
|
||||
std::swap((*vec)[low], (*vec)[minimumindex]);
|
||||
std::swap((*vec)[high], (*vec)[maximumindex]);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::swap((*vec)[low], (*vec)[high]);
|
||||
}
|
||||
|
||||
@@ -86,15 +71,13 @@ void CocktailSelectionSort_v2(std::vector<int> *vec, int low, int high)
|
||||
|
||||
// main function, select any one of iterative or recursive version
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cout << "Enter number of elements\n";
|
||||
std::cin >> n;
|
||||
std::vector<int> v(n);
|
||||
std::cout << "Enter all the elements\n";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::cin >> v[i];
|
||||
}
|
||||
|
||||
@@ -102,22 +85,16 @@ int main()
|
||||
std::cout << "Enter method: \n\t0: iterative\n\t1: recursive:\t";
|
||||
std::cin >> method;
|
||||
|
||||
if (method == 0)
|
||||
{
|
||||
if (method == 0) {
|
||||
CocktailSelectionSort(&v, 0, n - 1);
|
||||
}
|
||||
else if (method == 1)
|
||||
{
|
||||
} else if (method == 1) {
|
||||
CocktailSelectionSort_v2(&v, 0, n - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cerr << "Unknown method" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
std::cout << "Sorted elements are\n";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::cout << v[i] << " ";
|
||||
}
|
||||
|
||||
|
||||
@@ -8,15 +8,13 @@
|
||||
int a[100005];
|
||||
int n;
|
||||
|
||||
int FindNextGap(int x)
|
||||
{
|
||||
int FindNextGap(int x) {
|
||||
x = (x * 10) / 13;
|
||||
|
||||
return std::max(1, x);
|
||||
}
|
||||
|
||||
void CombSort(int a[], int l, int r)
|
||||
{
|
||||
void CombSort(int a[], int l, int r) {
|
||||
// Init gap
|
||||
int gap = n;
|
||||
|
||||
@@ -24,18 +22,15 @@ void CombSort(int a[], int l, int r)
|
||||
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]) {
|
||||
std::swap(a[i], a[i + gap]);
|
||||
swapped = true;
|
||||
}
|
||||
@@ -43,8 +38,7 @@ void CombSort(int a[], int l, int r)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::cin >> n;
|
||||
for (int i = 1; i <= n; ++i) std::cin >> a[i];
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int Max(int Arr[], int N)
|
||||
{
|
||||
int Max(int Arr[], int N) {
|
||||
int max = Arr[0];
|
||||
for (int i = 1; i < N; i++)
|
||||
if (Arr[i] > max)
|
||||
@@ -10,8 +9,7 @@ int Max(int Arr[], int N)
|
||||
return max;
|
||||
}
|
||||
|
||||
int Min(int Arr[], int N)
|
||||
{
|
||||
int Min(int Arr[], int N) {
|
||||
int min = Arr[0];
|
||||
for (int i = 1; i < N; i++)
|
||||
if (Arr[i] < min)
|
||||
@@ -19,13 +17,11 @@ int Min(int Arr[], int N)
|
||||
return min;
|
||||
}
|
||||
|
||||
void Print(int Arr[], int N)
|
||||
{
|
||||
void Print(int Arr[], int N) {
|
||||
for (int i = 0; i < N; i++) cout << Arr[i] << ", ";
|
||||
}
|
||||
|
||||
int *Counting_Sort(int Arr[], int N)
|
||||
{
|
||||
int *Counting_Sort(int Arr[], int N) {
|
||||
int max = Max(Arr, N);
|
||||
int min = Min(Arr, N);
|
||||
int *Sorted_Arr = new int[N];
|
||||
@@ -36,8 +32,7 @@ int *Counting_Sort(int Arr[], int N)
|
||||
|
||||
for (int i = 1; i < (max - min + 1); i++) Count[i] += Count[i - 1];
|
||||
|
||||
for (int i = N - 1; i >= 0; i--)
|
||||
{
|
||||
for (int i = N - 1; i >= 0; i--) {
|
||||
Sorted_Arr[Count[Arr[i] - min] - 1] = Arr[i];
|
||||
Count[Arr[i] - min]--;
|
||||
}
|
||||
@@ -45,8 +40,7 @@ int *Counting_Sort(int Arr[], int N)
|
||||
return Sorted_Arr;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int Arr[] = {47, 65, 20, 66, 25, 53, 64, 69, 72, 22,
|
||||
74, 25, 53, 15, 42, 36, 4, 69, 86, 19},
|
||||
N = 20;
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
void countSort(string arr)
|
||||
{
|
||||
void countSort(string arr) {
|
||||
string output;
|
||||
|
||||
int count[256], i;
|
||||
@@ -14,8 +13,7 @@ void countSort(string arr)
|
||||
|
||||
for (i = 1; i <= 256; ++i) count[i] += count[i - 1];
|
||||
|
||||
for (i = 0; arr[i]; ++i)
|
||||
{
|
||||
for (i = 0; arr[i]; ++i) {
|
||||
output[count[arr[i]] - 1] = arr[i];
|
||||
--count[arr[i]];
|
||||
}
|
||||
@@ -25,8 +23,7 @@ void countSort(string arr)
|
||||
cout << "Sorted character array is " << arr;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
string arr;
|
||||
cin >> arr;
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
void heapify(int *a, int i, int n)
|
||||
{
|
||||
void heapify(int *a, int i, int n) {
|
||||
int largest = i;
|
||||
const int l = 2 * i + 1;
|
||||
const int r = 2 * i + 2;
|
||||
@@ -13,38 +12,31 @@ void heapify(int *a, int i, int n)
|
||||
if (r < n && a[r] > a[largest])
|
||||
largest = r;
|
||||
|
||||
if (largest != i)
|
||||
{
|
||||
if (largest != i) {
|
||||
std::swap(a[i], a[largest]);
|
||||
heapify(a, n, largest);
|
||||
}
|
||||
}
|
||||
|
||||
void heapsort(int *a, int n)
|
||||
{
|
||||
for (int i = n - 1; i >= 0; --i)
|
||||
{
|
||||
void heapsort(int *a, int n) {
|
||||
for (int i = n - 1; i >= 0; --i) {
|
||||
std::swap(a[0], a[i]);
|
||||
heapify(a, 0, i);
|
||||
}
|
||||
}
|
||||
|
||||
void build_maxheap(int *a, int n)
|
||||
{
|
||||
for (int i = n / 2 - 1; i >= 0; --i)
|
||||
{
|
||||
void build_maxheap(int *a, int n) {
|
||||
for (int i = n / 2 - 1; i >= 0; --i) {
|
||||
heapify(a, i, n);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cout << "Enter number of elements of array\n";
|
||||
std::cin >> n;
|
||||
int a[20];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::cout << "Enter Element " << i << std::endl;
|
||||
std::cin >> a[i];
|
||||
}
|
||||
@@ -52,8 +44,7 @@ int main()
|
||||
build_maxheap(a, n);
|
||||
heapsort(a, n);
|
||||
std::cout << "Sorted Output\n";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::cout << a[i] << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cout << "\nEnter the length of your array : ";
|
||||
std::cin >> n;
|
||||
@@ -11,18 +10,15 @@ int main()
|
||||
std::cout << "\nEnter any " << n << " Numbers for Unsorted Array : ";
|
||||
|
||||
// Input
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cin >> Array[i];
|
||||
}
|
||||
|
||||
// Sorting
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
for (int i = 1; i < n; i++) {
|
||||
int temp = Array[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && temp < Array[j])
|
||||
{
|
||||
while (j >= 0 && temp < Array[j]) {
|
||||
Array[j + 1] = Array[j];
|
||||
j--;
|
||||
}
|
||||
@@ -31,8 +27,7 @@ int main()
|
||||
|
||||
// Output
|
||||
std::cout << "\nSorted Array : ";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << Array[i] << "\t";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
void librarySort(int *index, int n)
|
||||
{
|
||||
void librarySort(int *index, int n) {
|
||||
int lib_size, index_pos,
|
||||
*gaps, // gaps
|
||||
*library[2]; // libraries
|
||||
@@ -19,8 +18,7 @@ void librarySort(int *index, int n)
|
||||
target_lib = 0;
|
||||
library[target_lib][0] = index[0];
|
||||
|
||||
while (index_pos < n)
|
||||
{
|
||||
while (index_pos < n) {
|
||||
// binary search
|
||||
int insert = std::distance(
|
||||
library[target_lib],
|
||||
@@ -29,23 +27,19 @@ void librarySort(int *index, int n)
|
||||
|
||||
// if there is no gap to insert a new index ...
|
||||
|
||||
if (numbered[insert] == true)
|
||||
{
|
||||
if (numbered[insert] == true) {
|
||||
int prov_size = 0, next_target_lib = !target_lib;
|
||||
|
||||
// update library and clear gaps
|
||||
|
||||
for (int i = 0; i <= n; i++)
|
||||
{
|
||||
if (numbered[i] == true)
|
||||
{
|
||||
for (int i = 0; i <= n; i++) {
|
||||
if (numbered[i] == true) {
|
||||
library[next_target_lib][prov_size] = gaps[i];
|
||||
prov_size++;
|
||||
numbered[i] = false;
|
||||
}
|
||||
|
||||
if (i <= lib_size)
|
||||
{
|
||||
if (i <= lib_size) {
|
||||
library[next_target_lib][prov_size] =
|
||||
library[target_lib][i];
|
||||
prov_size++;
|
||||
@@ -54,9 +48,7 @@ void librarySort(int *index, int n)
|
||||
|
||||
target_lib = next_target_lib;
|
||||
lib_size = prov_size - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
numbered[insert] = true;
|
||||
gaps[insert] = index[index_pos];
|
||||
index_pos++;
|
||||
@@ -64,17 +56,14 @@ void librarySort(int *index, int n)
|
||||
}
|
||||
|
||||
int index_pos_for_output = 0;
|
||||
for (int i = 0; index_pos_for_output < n; i++)
|
||||
{
|
||||
if (numbered[i] == true)
|
||||
{
|
||||
for (int i = 0; index_pos_for_output < n; i++) {
|
||||
if (numbered[i] == true) {
|
||||
// std::cout << gaps[i] << std::endl;
|
||||
index[index_pos_for_output] = gaps[i];
|
||||
index_pos_for_output++;
|
||||
}
|
||||
|
||||
if (i < lib_size)
|
||||
{
|
||||
if (i < lib_size) {
|
||||
// std::cout << library[target_lib][i] << std::endl;
|
||||
index[index_pos_for_output] = library[target_lib][i];
|
||||
index_pos_for_output++;
|
||||
@@ -82,8 +71,7 @@ void librarySort(int *index, int n)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// ---example--
|
||||
int index_ex[] = {-6, 5, 9, 1, 9, 1, 0, 1, -8, 4, -12};
|
||||
int n_ex = sizeof(index_ex) / sizeof(index_ex[0]);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
|
||||
void merge(int arr[], int l, int m, int r)
|
||||
{
|
||||
void merge(int arr[], int l, int m, int r) {
|
||||
int i, j, k;
|
||||
int n1 = m - l + 1;
|
||||
int n2 = r - m;
|
||||
@@ -14,30 +13,24 @@ void merge(int arr[], int l, int m, int r)
|
||||
i = 0;
|
||||
j = 0;
|
||||
k = l;
|
||||
while (i < n1 && j < n2)
|
||||
{
|
||||
if (L[i] <= R[j])
|
||||
{
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
while (i < n1)
|
||||
{
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
while (j < n2)
|
||||
{
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
@@ -47,10 +40,8 @@ void merge(int arr[], int l, int m, int r)
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
void mergeSort(int arr[], int l, int r)
|
||||
{
|
||||
if (l < r)
|
||||
{
|
||||
void mergeSort(int arr[], int l, int r) {
|
||||
if (l < r) {
|
||||
int m = l + (r - l) / 2;
|
||||
|
||||
mergeSort(arr, l, m);
|
||||
@@ -60,14 +51,12 @@ void mergeSort(int arr[], int l, int r)
|
||||
}
|
||||
}
|
||||
|
||||
void show(int A[], int size)
|
||||
{
|
||||
void show(int A[], int size) {
|
||||
int i;
|
||||
for (i = 0; i < size; i++) std::cout << A[i] << "\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int size;
|
||||
std::cout << "\nEnter the number of elements : ";
|
||||
|
||||
@@ -77,8 +66,7 @@ int main()
|
||||
|
||||
std::cout << "\nEnter the unsorted elements : ";
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
std::cout << "\n";
|
||||
std::cin >> arr[i];
|
||||
}
|
||||
|
||||
@@ -22,20 +22,17 @@ void merge(Iterator, Iterator, const Iterator, char[]);
|
||||
*/
|
||||
template <class Iterator>
|
||||
void non_recursive_merge_sort(const Iterator first, const Iterator last,
|
||||
const size_t n)
|
||||
{
|
||||
const size_t n) {
|
||||
// create a buffer large enough to store all elements
|
||||
// dynamically allocated to comply with cpplint
|
||||
char* buffer = new char[n * sizeof(*first)];
|
||||
// buffer size can be optimized to largest power of 2 less than n elements
|
||||
// divide the container into equally-sized segments whose length start at 1
|
||||
// and keeps increasing by factors of 2
|
||||
for (size_t length(1); length < n; length <<= 1)
|
||||
{
|
||||
for (size_t length(1); length < n; length <<= 1) {
|
||||
// merge adjacent segments whose number is n / (length * 2)
|
||||
Iterator left(first);
|
||||
for (size_t counter(n / (length << 1)); counter; --counter)
|
||||
{
|
||||
for (size_t counter(n / (length << 1)); counter; --counter) {
|
||||
Iterator right(left + length), end(right + length);
|
||||
merge(left, right, end, buffer);
|
||||
left = end;
|
||||
@@ -56,8 +53,7 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last,
|
||||
* @param b points at the buffer
|
||||
*/
|
||||
template <class Iterator>
|
||||
void merge(Iterator l, Iterator r, const Iterator e, char b[])
|
||||
{
|
||||
void merge(Iterator l, Iterator r, const Iterator e, char b[]) {
|
||||
// create 2 pointers to point at the buffer
|
||||
auto p(reinterpret_cast<std::remove_reference_t<decltype(*l)>*>(b)), c(p);
|
||||
// move the left part of the segment
|
||||
@@ -77,8 +73,7 @@ void merge(Iterator l, Iterator r, const Iterator e, char b[])
|
||||
* @param n the number of elements
|
||||
*/
|
||||
template <class Iterator>
|
||||
void non_recursive_merge_sort(const Iterator first, const size_t n)
|
||||
{
|
||||
void non_recursive_merge_sort(const Iterator first, const size_t n) {
|
||||
non_recursive_merge_sort(first, first + n, n);
|
||||
}
|
||||
/// bottom-up merge sort which sorts elements in a non-decreasing order
|
||||
@@ -87,19 +82,16 @@ void non_recursive_merge_sort(const Iterator first, const size_t n)
|
||||
* @param last points to 1-step past the last element
|
||||
*/
|
||||
template <class Iterator>
|
||||
void non_recursive_merge_sort(const Iterator first, const Iterator last)
|
||||
{
|
||||
void non_recursive_merge_sort(const Iterator first, const Iterator last) {
|
||||
non_recursive_merge_sort(first, last, last - first);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int main(int argc, char** argv) {
|
||||
int size;
|
||||
std::cout << "Enter the number of elements : ";
|
||||
std::cin >> size;
|
||||
int* arr = new int[size];
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
std::cout << "arr[" << i << "] = ";
|
||||
std::cin >> arr[i];
|
||||
}
|
||||
|
||||
@@ -13,14 +13,11 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
bool NumericSort(std::string a, std::string b)
|
||||
{
|
||||
while (a[0] == '0')
|
||||
{
|
||||
bool NumericSort(std::string a, std::string b) {
|
||||
while (a[0] == '0') {
|
||||
a.erase(a.begin());
|
||||
}
|
||||
while (b[0] == '0')
|
||||
{
|
||||
while (b[0] == '0') {
|
||||
b.erase(b.begin());
|
||||
}
|
||||
int n = a.length();
|
||||
@@ -30,31 +27,27 @@ bool NumericSort(std::string a, std::string b)
|
||||
return n < m;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cout << "Enter number of elements to be sorted Numerically\n";
|
||||
std::cin >> n;
|
||||
|
||||
std::vector<std::string> v(n);
|
||||
std::cout << "Enter the string of Numbers\n";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cin >> v[i];
|
||||
}
|
||||
|
||||
sort(v.begin(), v.end());
|
||||
std::cout << "Elements sorted normally \n";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << v[i] << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
|
||||
std::sort(v.begin(), v.end(), NumericSort);
|
||||
std::cout << "Elements sorted Numerically \n";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << v[i] << " ";
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,13 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
void oddEven(vector<int> &arr, int size)
|
||||
{
|
||||
void oddEven(vector<int> &arr, int size) {
|
||||
bool sorted = false;
|
||||
while (!sorted)
|
||||
{
|
||||
while (!sorted) {
|
||||
sorted = true;
|
||||
for (int i = 1; i < size - 1; i += 2) // Odd
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
swap(arr[i], arr[i + 1]);
|
||||
sorted = false;
|
||||
}
|
||||
@@ -21,8 +18,7 @@ void oddEven(vector<int> &arr, int size)
|
||||
|
||||
for (int i = 0; i < size - 1; i += 2) // Even
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
swap(arr[i], arr[i + 1]);
|
||||
sorted = false;
|
||||
}
|
||||
@@ -30,14 +26,12 @@ void oddEven(vector<int> &arr, int size)
|
||||
}
|
||||
}
|
||||
|
||||
void show(vector<int> A, int size)
|
||||
{
|
||||
void show(vector<int> A, int size) {
|
||||
int i;
|
||||
for (i = 0; i < size; i++) cout << A[i] << "\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int size, temp;
|
||||
cout << "\nEnter the number of elements : ";
|
||||
cin >> size;
|
||||
@@ -46,8 +40,7 @@ int main()
|
||||
|
||||
cout << "\nEnter the unsorted elements : \n";
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
cin >> temp;
|
||||
arr.push_back(temp);
|
||||
}
|
||||
|
||||
@@ -33,17 +33,14 @@
|
||||
*
|
||||
*/
|
||||
|
||||
int partition(int arr[], int low, int high)
|
||||
{
|
||||
int partition(int arr[], int low, int high) {
|
||||
int pivot = arr[high]; // taking the last element as pivot
|
||||
int i = (low - 1); // Index of smaller element
|
||||
|
||||
for (int j = low; j < high; j++)
|
||||
{
|
||||
for (int j = low; j < high; j++) {
|
||||
// If current element is smaller than or
|
||||
// equal to pivot
|
||||
if (arr[j] <= pivot)
|
||||
{
|
||||
if (arr[j] <= pivot) {
|
||||
i++; // increment index of smaller element
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
@@ -62,10 +59,8 @@ int partition(int arr[], int low, int high)
|
||||
* low --> Starting index,
|
||||
* high --> Ending index
|
||||
*/
|
||||
void quickSort(int arr[], int low, int high)
|
||||
{
|
||||
if (low < high)
|
||||
{
|
||||
void quickSort(int arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int p = partition(arr, low, high);
|
||||
quickSort(arr, low, p - 1);
|
||||
quickSort(arr, p + 1, high);
|
||||
@@ -73,15 +68,13 @@ void quickSort(int arr[], int low, int high)
|
||||
}
|
||||
|
||||
// prints the array after sorting
|
||||
void show(int arr[], int size)
|
||||
{
|
||||
void show(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
/** Driver program to test above functions */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int size;
|
||||
std::cout << "\nEnter the number of elements : ";
|
||||
|
||||
@@ -91,8 +84,7 @@ int main()
|
||||
|
||||
std::cout << "\nEnter the unsorted elements : ";
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
std::cout << "\n";
|
||||
std::cin >> arr[i];
|
||||
}
|
||||
|
||||
@@ -3,66 +3,53 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
void radixsort(int a[], int n)
|
||||
{
|
||||
void radixsort(int a[], int n) {
|
||||
int count[10];
|
||||
int* output = new int[n];
|
||||
memset(output, 0, n * sizeof(*output));
|
||||
memset(count, 0, sizeof(count));
|
||||
int max = 0;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i] > max)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (a[i] > max) {
|
||||
max = a[i];
|
||||
}
|
||||
}
|
||||
int maxdigits = 0;
|
||||
while (max)
|
||||
{
|
||||
while (max) {
|
||||
maxdigits++;
|
||||
max /= 10;
|
||||
}
|
||||
for (int j = 0; j < maxdigits; j++)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < maxdigits; j++) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int t = std::pow(10, j);
|
||||
count[(a[i] % (10 * t)) / t]++;
|
||||
}
|
||||
int k = 0;
|
||||
for (int p = 0; p < 10; p++)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int p = 0; p < 10; p++) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int t = std::pow(10, j);
|
||||
if ((a[i] % (10 * t)) / t == p)
|
||||
{
|
||||
if ((a[i] % (10 * t)) / t == p) {
|
||||
output[k] = a[i];
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
memset(count, 0, sizeof(count));
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
a[i] = output[i];
|
||||
}
|
||||
}
|
||||
delete[] output;
|
||||
}
|
||||
|
||||
void print(int a[], int n)
|
||||
{
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
void print(int a[], int n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::cout << a[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char const* argv[])
|
||||
{
|
||||
int main(int argc, char const* argv[]) {
|
||||
int a[] = {170, 45, 75, 90, 802, 24, 2, 66};
|
||||
int n = sizeof(a) / sizeof(a[0]);
|
||||
radixsort(a, n);
|
||||
|
||||
@@ -3,25 +3,20 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int Array[6];
|
||||
cout << "\nEnter any 6 Numbers for Unsorted Array : ";
|
||||
|
||||
// Input
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
for (int i = 0; i < 6; i++) {
|
||||
cin >> Array[i];
|
||||
}
|
||||
|
||||
// Selection Sorting
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
for (int i = 0; i < 6; i++) {
|
||||
int min = i;
|
||||
for (int j = i + 1; j < 6; j++)
|
||||
{
|
||||
if (Array[j] < Array[min])
|
||||
{
|
||||
for (int j = i + 1; j < 6; j++) {
|
||||
if (Array[j] < Array[min]) {
|
||||
min = j; // Finding the smallest number in Array
|
||||
}
|
||||
}
|
||||
@@ -32,8 +27,7 @@ int main()
|
||||
|
||||
// Output
|
||||
cout << "\nSorted Array : ";
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
for (int i = 0; i < 6; i++) {
|
||||
cout << Array[i] << "\t";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,23 @@
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int size = 10;
|
||||
int* array = new int[size];
|
||||
// Input
|
||||
std::cout << "\nHow many numbers do want to enter in unsorted array : ";
|
||||
std::cin >> size;
|
||||
std::cout << "\nEnter the numbers for unsorted array : ";
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
std::cin >> array[i];
|
||||
}
|
||||
|
||||
// Sorting
|
||||
for (int i = size / 2; i > 0; i = i / 2)
|
||||
{
|
||||
for (int j = i; j < size; j++)
|
||||
{
|
||||
for (int k = j - i; k >= 0; k = k - i)
|
||||
{
|
||||
if (array[k] < array[k + i])
|
||||
{
|
||||
for (int i = size / 2; i > 0; i = i / 2) {
|
||||
for (int j = i; j < size; j++) {
|
||||
for (int k = j - i; k >= 0; k = k - i) {
|
||||
if (array[k] < array[k + i]) {
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
int temp = array[k + i];
|
||||
array[k + i] = array[k];
|
||||
array[k] = temp;
|
||||
@@ -36,8 +28,7 @@ int main()
|
||||
|
||||
// Output
|
||||
std::cout << "\nSorted array : ";
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
std::cout << array[i] << "\t";
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
#include <utility>
|
||||
|
||||
template <class T>
|
||||
void show_data(T *arr, size_t LEN)
|
||||
{
|
||||
void show_data(T *arr, size_t LEN) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < LEN; i++) std::cout << arr[i] << ", ";
|
||||
@@ -16,8 +15,7 @@ void show_data(T *arr, size_t LEN)
|
||||
}
|
||||
|
||||
template <class T, size_t N>
|
||||
void show_data(T (&arr)[N])
|
||||
{
|
||||
void show_data(T (&arr)[N]) {
|
||||
show_data(arr, N);
|
||||
}
|
||||
|
||||
@@ -26,17 +24,14 @@ void show_data(T (&arr)[N])
|
||||
* Mar
|
||||
**/
|
||||
template <class T>
|
||||
void shell_sort(T *arr, size_t LEN)
|
||||
{
|
||||
void shell_sort(T *arr, size_t LEN) {
|
||||
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
||||
const unsigned int gap_len = 8;
|
||||
size_t i, j, g;
|
||||
|
||||
for (g = 0; g < gap_len; g++)
|
||||
{
|
||||
for (g = 0; g < gap_len; g++) {
|
||||
unsigned int gap = gaps[g];
|
||||
for (i = gap; i < LEN; i++)
|
||||
{
|
||||
for (i = gap; i < LEN; i++) {
|
||||
T tmp = arr[i];
|
||||
|
||||
for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap)
|
||||
@@ -48,16 +43,14 @@ void shell_sort(T *arr, size_t LEN)
|
||||
}
|
||||
|
||||
template <class T, size_t N>
|
||||
void shell_sort(T (&arr)[N])
|
||||
{
|
||||
void shell_sort(T (&arr)[N]) {
|
||||
shell_sort(arr, N);
|
||||
}
|
||||
|
||||
/**
|
||||
* function to compare sorting using cstdlib's qsort
|
||||
**/
|
||||
int compare(const void *a, const void *b)
|
||||
{
|
||||
int compare(const void *a, const void *b) {
|
||||
int arg1 = *static_cast<const int *>(a);
|
||||
int arg2 = *static_cast<const int *>(b);
|
||||
|
||||
@@ -71,8 +64,7 @@ int compare(const void *a, const void *b)
|
||||
// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
int i, NUM_DATA;
|
||||
|
||||
if (argc == 2)
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void SlowSort(int a[], int i, int j)
|
||||
{
|
||||
void SlowSort(int a[], int i, int j) {
|
||||
if (i >= j)
|
||||
return;
|
||||
int m = i + (j - i) / 2; // midpoint, implemented this way to avoid
|
||||
@@ -19,8 +18,7 @@ void SlowSort(int a[], int i, int j)
|
||||
int temp;
|
||||
SlowSort(a, i, m);
|
||||
SlowSort(a, m + 1, j);
|
||||
if (a[j] < a[m])
|
||||
{
|
||||
if (a[j] < a[m]) {
|
||||
temp = a[j]; // swapping a[j] & a[m]
|
||||
a[j] = a[m];
|
||||
a[m] = temp;
|
||||
@@ -30,8 +28,7 @@ void SlowSort(int a[], int i, int j)
|
||||
|
||||
// Sample Main function
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int size;
|
||||
std::cout << "\nEnter the number of elements : ";
|
||||
|
||||
@@ -41,8 +38,7 @@ int main()
|
||||
|
||||
std::cout << "\nEnter the unsorted elements : ";
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
std::cout << "\n";
|
||||
std::cin >> arr[i];
|
||||
}
|
||||
@@ -51,8 +47,7 @@ int main()
|
||||
|
||||
std::cout << "Sorted array\n";
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
std::cout << arr[i] << " ";
|
||||
}
|
||||
|
||||
|
||||
@@ -6,14 +6,12 @@
|
||||
|
||||
// Function returns the minimum number of swaps
|
||||
// required to sort the array
|
||||
int minSwaps(int arr[], int n)
|
||||
{
|
||||
int minSwaps(int arr[], int n) {
|
||||
// Create an array of pairs where first
|
||||
// element is array element and second element
|
||||
// is position of first element
|
||||
std::pair<int, int> *arrPos = new std::pair<int, int>[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
arrPos[i].first = arr[i];
|
||||
arrPos[i].second = i;
|
||||
}
|
||||
@@ -31,8 +29,7 @@ int minSwaps(int arr[], int n)
|
||||
int ans = 0;
|
||||
|
||||
// Traverse array elements
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
// already swapped and corrected or
|
||||
// already present at correct pos
|
||||
if (vis[i] || arrPos[i].second == i)
|
||||
@@ -42,8 +39,7 @@ int minSwaps(int arr[], int n)
|
||||
// this cycle and add in ans
|
||||
int cycle_size = 0;
|
||||
int j = i;
|
||||
while (!vis[j])
|
||||
{
|
||||
while (!vis[j]) {
|
||||
vis[j] = 1;
|
||||
|
||||
// move to next node
|
||||
@@ -52,8 +48,7 @@ int minSwaps(int arr[], int n)
|
||||
}
|
||||
|
||||
// Update answer by adding current cycle.
|
||||
if (cycle_size > 0)
|
||||
{
|
||||
if (cycle_size > 0) {
|
||||
ans += (cycle_size - 1);
|
||||
}
|
||||
}
|
||||
@@ -65,8 +60,7 @@ int minSwaps(int arr[], int n)
|
||||
}
|
||||
|
||||
// program to test
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int arr[] = {6, 7, 8, 1, 2, 3, 9, 12};
|
||||
int n = (sizeof(arr) / sizeof(int));
|
||||
std::cout << minSwaps(arr, n);
|
||||
|
||||
@@ -6,14 +6,11 @@ const int RUN = 32;
|
||||
|
||||
// this function sorts array from left index to to right index which is of size
|
||||
// atmost RUN
|
||||
void insertionSort(int arr[], int left, int right)
|
||||
{
|
||||
for (int i = left + 1; i <= right; i++)
|
||||
{
|
||||
void insertionSort(int arr[], int left, int right) {
|
||||
for (int i = left + 1; i <= right; i++) {
|
||||
int temp = arr[i];
|
||||
int j = i - 1;
|
||||
while (arr[j] > temp && j >= left)
|
||||
{
|
||||
while (arr[j] > temp && j >= left) {
|
||||
arr[j + 1] = arr[j];
|
||||
j--;
|
||||
}
|
||||
@@ -22,8 +19,7 @@ void insertionSort(int arr[], int left, int right)
|
||||
}
|
||||
|
||||
// merge function merges the sorted runs
|
||||
void merge(int arr[], int l, int m, int r)
|
||||
{
|
||||
void merge(int arr[], int l, int m, int r) {
|
||||
// original array is broken in two parts, left and right array
|
||||
int len1 = m - l + 1, len2 = r - m;
|
||||
int *left = new int[len1], *right = new int[len2];
|
||||
@@ -35,15 +31,11 @@ void merge(int arr[], int l, int m, int r)
|
||||
int k = l;
|
||||
|
||||
// after comparing, we merge those two array in larger sub array
|
||||
while (i < len1 && j < len2)
|
||||
{
|
||||
if (left[i] <= right[j])
|
||||
{
|
||||
while (i < len1 && j < len2) {
|
||||
if (left[i] <= right[j]) {
|
||||
arr[k] = left[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
arr[k] = right[j];
|
||||
j++;
|
||||
}
|
||||
@@ -51,16 +43,14 @@ void merge(int arr[], int l, int m, int r)
|
||||
}
|
||||
|
||||
// copy remaining elements of left, if any
|
||||
while (i < len1)
|
||||
{
|
||||
while (i < len1) {
|
||||
arr[k] = left[i];
|
||||
k++;
|
||||
i++;
|
||||
}
|
||||
|
||||
// copy remaining element of right, if any
|
||||
while (j < len2)
|
||||
{
|
||||
while (j < len2) {
|
||||
arr[k] = right[j];
|
||||
k++;
|
||||
j++;
|
||||
@@ -70,21 +60,18 @@ void merge(int arr[], int l, int m, int r)
|
||||
}
|
||||
|
||||
// iterative Timsort function to sort the array[0...n-1] (similar to merge sort)
|
||||
void timSort(int arr[], int n)
|
||||
{
|
||||
void timSort(int arr[], int n) {
|
||||
// Sort individual subarrays of size RUN
|
||||
for (int i = 0; i < n; i += RUN)
|
||||
insertionSort(arr, i, std::min((i + 31), (n - 1)));
|
||||
|
||||
// start merging from size RUN (or 32). It will merge to form size 64, then
|
||||
// 128, 256 and so on ....
|
||||
for (int size = RUN; size < n; size = 2 * size)
|
||||
{
|
||||
for (int size = RUN; size < n; size = 2 * size) {
|
||||
// pick starting point of left sub array. We are going to merge
|
||||
// arr[left..left+size-1] and arr[left+size, left+2*size-1] After every
|
||||
// merge, we increase left by 2*size
|
||||
for (int left = 0; left < n; left += 2 * size)
|
||||
{
|
||||
for (int left = 0; left < n; left += 2 * size) {
|
||||
// find ending point of left sub array
|
||||
// mid+1 is starting point of right sub array
|
||||
int mid = left + size - 1;
|
||||
@@ -97,15 +84,13 @@ void timSort(int arr[], int n)
|
||||
}
|
||||
|
||||
// utility function to print the Array
|
||||
void printArray(int arr[], int n)
|
||||
{
|
||||
void printArray(int arr[], int n) {
|
||||
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// Driver program to test above function
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int arr[] = {5, 21, 7, 23, 19};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
printf("Given Array is\n");
|
||||
|
||||
Reference in New Issue
Block a user