Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions programs/cpp/Bitonic_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include<bits/stdc++.h>
using namespace std;

/*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)
{
if (dir==(a[i]>a[j]))
swap(a[i],a[j]);
}

/*It recursively sorts a bitonic sequence in ascending order,
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)
{
int k = cnt/2;
for (int i=low; i<low+k; i++)
compAndSwap(a, i, i+k, dir);
bitonicMerge(a, low, k, dir);
bitonicMerge(a, low+k, k, 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)
{
int k = cnt/2;

// sort in ascending order since dir here is 1
bitonicSort(a, low, k, 1);

// sort in descending order since dir here is 0
bitonicSort(a, low+k, k, 0);

// Will merge whole sequence in ascending order
// since dir=1.
bitonicMerge(a,low, cnt, dir);
}
}

/* Caller of bitonicSort for sorting the entire array of
length N in ASCENDING order */
void sort(int a[], int N, int up)
{
bitonicSort(a,0, N, up);
}

// Driver code
int main()
{
int a[]= {3, 7, 4, 8, 6, 2, 1, 5};
int N = sizeof(a)/sizeof(a[0]);

int up = 1; // means sort in ascending order
sort(a, N, up);

printf("Sorted array: \n");
for (int i=0; i<N; i++)
printf("%d ", a[i]);
return 0;
}
51 changes: 51 additions & 0 deletions programs/cpp/Cycle_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
using namespace std;

void cyclicSort(int arr[], int n){
int i = 0;
while(i < n)
{
// as array is of 1 based indexing so the
// correct position or index number of each
// element is element-1 i.e. 1 will be at 0th
// index similarly 2 correct index will 1 so
// on...
int correct = arr[i] - 1 ;
if(arr[i] != arr[correct]){

// if array element should be lesser than
// size and array element should not be at
// its correct position then only swap with
// its correct position or index value
swap(arr[i], arr[correct]) ;
}else{

// if element is at its correct position
// just increment i and check for remaining
// array elements
i++ ;
}
}

}

void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

int main() {

int arr[] = { 3, 2, 4, 5, 1};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Before sorting array: \n";
printArray(arr, n);
cyclicSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;

}