void oddEvenSort(List<int> arr, int n) {
bool isSorted = false;
while (!isSorted) {
isSorted = true;
int temp = 0;
// Perform Bubble sort on odd indexed elements
for (int i = 1; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
// Perform Bubble sort on even indexed elements
for (int i = 0; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
}
}
class Iter {
void oddEvenSort(List<int> arr, int n) {
final Stopwatch stopwatch = Stopwatch()..start();
bool isSorted = false;
while (!isSorted) {
isSorted = true;
int temp = 0;
// Perform Bubble sort on odd indexed elements
for (int i = 1; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
// Perform Bubble sort on even indexed elements
for (int i = 0; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
}
stopwatch.stop();
print('Iter Took: ${stopwatch.elapsedMicroseconds} microseconds');
}
}
class StaticIter {
static void oddEvenSort(List<int> arr, int n) {
final Stopwatch stopwatch = Stopwatch()..start();
bool isSorted = false;
while (!isSorted) {
isSorted = true;
int temp = 0;
// Perform Bubble sort on odd indexed elements
for (int i = 1; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
// Perform Bubble sort on even indexed elements
for (int i = 0; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
}
stopwatch.stop();
print('Static Iter Took: ${stopwatch.elapsedMicroseconds} microseconds');
}
}
class PrivateStaticIter {
static oddEvenSort(List<int> arr, int n) {
final Stopwatch stopwatch = Stopwatch()..start();
_oddEvenSort(arr, n);
stopwatch.stop();
print(
'Private Static Iter Took: ${stopwatch.elapsedMicroseconds} microseconds');
}
static void _oddEvenSort(List<int> arr, int n) {
bool isSorted = false;
while (!isSorted) {
isSorted = true;
int temp = 0;
// Perform Bubble sort on odd indexed elements
for (int i = 1; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
// Perform Bubble sort on even indexed elements
for (int i = 0; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
}
}
}
Calling each methods
class Algorithm {
first(List<int> arr, int n) => Iter().oddEvenSort(arr, n);
second(List<int> arr, int n) => StaticIter.oddEvenSort(arr, n);
third(List<int> arr, int n) => PrivateStaticIter.oddEvenSort(arr, n);
}
final List<int> array = List.generate(100000, (index) => Random().nextInt(10000));
void main() {
final algorithm = Algorithm();
algorithm.first(array, array.length);
algorithm.second(array, array.length);
algorithm.third(array, array.length);
}
void main() {
final List<int> array = List.generate(1000, (index) => Random().nextInt(100));
group('Iteration', () {
test('IterTEST', () {
Iter().oddEvenSort(array, array.length);
});
test('Static Iter', () {
StaticIter.oddEvenSort(array, array.length);
});
test('Private Static Iter', () {
PrivateStaticIter.oddEvenSort(array, array.length);
});
});
}
Iter Took: 88857909 microseconds
Static Iter Took: 2789 microseconds
Private Static Iter Took: 2868 microseconds
Iter Took: 87715375 microseconds
Static Iter Took: 2993 microseconds
Private Static Iter Took: 2886 microseconds
Iter Took: 87956866 microseconds
Static Iter Took: 3137 microseconds
Private Static Iter Took: 2798 microseconds