Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bitonic sorting and cycle sorting methods added #230

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@
* [Ternary Search](https://github.com/TheAlgorithms/Dart/blob/master/search/ternary_Search.dart)

## Sort
* [Bitonic Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/bitonic_sort.dart)
* [Bubble Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/bubble_Sort.dart)
* [Cocktail Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/cocktail_sort.dart)
* [Comb Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/comb_sort.dart)
* [Count Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/count_sort.dart)
* [Cycle Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/cycle_sort.dart)
* [Gnome Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/gnome_Sort.dart)
* [Heap Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/heap_Sort.dart)
* [Insert Sort](https://github.com/TheAlgorithms/Dart/blob/master/sort/insert_Sort.dart)
Expand Down
47 changes: 47 additions & 0 deletions sort/bitonic_sort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
void bitonicSort(List<int> arr, bool ascending) {
// Ensure that the input array size is a power of 2
if ((arr.length & (arr.length - 1)) != 0) {
throw ArgumentError("Input array size must be a power of 2.");
}

// Recursive function to perform bitonic merge
void bitonicMerge(List<int> arr, int low, int count, bool ascending) {
if (count > 1) {
int k = count ~/ 2;
for (int i = low; i < low + k; i++) {
if ((arr[i] > arr[i + k]) == ascending) {
// Swap elements to maintain bitonicity
int temp = arr[i];
arr[i] = arr[i + k];
arr[i + k] = temp;
}
}
bitonicMerge(arr, low, k, ascending);
bitonicMerge(arr, low + k, k, ascending);
}
}

// Main bitonic sort function
void bitonicSortRecursive(List<int> arr, int low, int count, bool ascending) {
if (count > 1) {
int k = count ~/ 2;

// Bitonic split phase
bitonicSortRecursive(arr, low, k, !ascending);
bitonicSortRecursive(arr, low + k, k, ascending);

// Bitonic merge phase
bitonicMerge(arr, low, count, ascending);
}
}

// Start the bitonic sort process
bitonicSortRecursive(arr, 0, arr.length, ascending);
}

void main() {
List<int> data = [3, 7, 4, 8, 6, 2, 1, 5];
bool ascending = true; // Sort in ascending order
bitonicSort(data, ascending);
print("Sorted Data (Ascending): $data");
}
60 changes: 60 additions & 0 deletions sort/cycle_sort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
void cycleSort(List<int> arr) {
int n = arr.length;

for (int cycleStart = 0; cycleStart < n - 1; cycleStart++) {
int item = arr[cycleStart];
int pos = cycleStart;

// Find the position where the item should be placed
for (int i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) {
pos++;
}
}

// If the item is already at the correct position, continue to the next cycle
if (pos == cycleStart) {
continue;
}

// Skip duplicate elements
while (item == arr[pos]) {
pos++;
}

// Swap the item with the element at its correct position
if (pos != cycleStart) {
int temp = item;
item = arr[pos];
arr[pos] = temp;
}

// Perform additional cycles if needed to place other elements in their correct positions
while (pos != cycleStart) {
pos = cycleStart;
for (int i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) {
pos++;
}
}

// Skip duplicate elements
while (item == arr[pos]) {
pos++;
}

// Swap the item with the element at its correct position
if (item != arr[pos]) {
int temp = item;
item = arr[pos];
arr[pos] = temp;
}
}
}
}

void main() {
List<int> data = [5, 1, 3, 4, 2];
cycleSort(data);
print("Sorted Data: $data");
}