diff --git a/DIRECTORY.md b/DIRECTORY.md index 63e3f76..1c774a6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/sort/bitonic_sort.dart b/sort/bitonic_sort.dart new file mode 100644 index 0000000..3c74fdb --- /dev/null +++ b/sort/bitonic_sort.dart @@ -0,0 +1,47 @@ +void bitonicSort(List 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 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 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 data = [3, 7, 4, 8, 6, 2, 1, 5]; + bool ascending = true; // Sort in ascending order + bitonicSort(data, ascending); + print("Sorted Data (Ascending): $data"); +} diff --git a/sort/cycle_sort.dart b/sort/cycle_sort.dart new file mode 100644 index 0000000..a110c9a --- /dev/null +++ b/sort/cycle_sort.dart @@ -0,0 +1,60 @@ +void cycleSort(List 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 data = [5, 1, 3, 4, 2]; + cycleSort(data); + print("Sorted Data: $data"); +}