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

Dart Sorting Algorithms #195

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
41 changes: 41 additions & 0 deletions Dart/BubbleSort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
void main() {
List<Comparable> numbers = [5, 1, 4, 2, 8];
List<Comparable> letters = ['q', 'a', 'd', 'e', 'h'];
show("Old Array", numbers);
sort(numbers);
show("New Array", numbers);
}

// bubble sort
sort(List<Comparable> a) {
bool swapped = true;
while (swapped) {
// assume this is last pass over array
swapped = false;
for (int i = 0; i < a.length - 1; i++) {
if (isLess(a[i + 1], a[i])) {
// exchange a[i] and a[j]
exchange(a, i, i + 1);
// after an exchange, must look again
swapped = true;
}
}
}
}

bool isLess(Comparable v, Comparable w) {
return (v.compareTo(w) < 0);
}

void exchange(List a, int i, int j) {
var swap = a[i];
a[i] = a[j];
a[j] = swap;
}

show(String text, List<Comparable> a) {
print(text);
for (int i = 0; i < a.length; i++) {
print(a[i]);
}
}
34 changes: 34 additions & 0 deletions Dart/InsertionSort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
void main() {
List<Comparable> numbers = [5, 1, 4, 2, 8];
List<Comparable> letters = ['q', 'a', 'd', 'e', 'h'];
show("Old Array", numbers);
sort(numbers);
show("New Array", numbers);
}

// Insertion Sort
void sort(List<Comparable> a) {
int N = a.length;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0 && isLess(a[j], a[j - 1]); j--) {
exchange(a, j, j - 1);
}
}
}

bool isLess(Comparable v, Comparable w) {
return (v.compareTo(w) < 0);
}

void exchange(List a, int i, int j) {
var swap = a[i];
a[i] = a[j];
a[j] = swap;
}

show(String text, List<Comparable> a) {
print(text);
for (int i = 0; i < a.length; i++) {
print(a[i]);
}
}
58 changes: 58 additions & 0 deletions Dart/MergeSort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
void main() {
List<Comparable> numbers = [5, 1, 4, 2, 8];
List<Comparable> letters = ['q', 'a', 'd', 'e', 'h'];
show("Old Array", numbers);
sort(numbers);
show("New Array", numbers);
}

void sort(List<Comparable> a) {
List<Comparable> aux = new List<Comparable>(a.length);
sortHighLow(a, aux, 0, a.length - 1);
}

// merge sort a[low..high] using auxiliary list aux[low..high]
void sortHighLow(List<Comparable> a, List<Comparable> aux, int low, int high) {
if (high <= low) {
return;
}

int mid = low + ((high - low) / 2).toInt();
sortHighLow(a, aux, low, mid);
sortHighLow(a, aux, mid + 1, high);
merge(a, aux, low, mid, high);
}

// stably merge a[low..mid] with a[mid+1..high] using aux[low..high]
void merge(
List<Comparable> a, List<Comparable> aux, int low, int mid, int high) {
// copy to aux[]
for (int k = low; k <= high; k++) {
aux[k] = a[k];
}

// merge back to a[]
int i = low, j = mid + 1;
for (int k = low; k <= high; k++) {
if (i > mid) {
a[k] = aux[j++];
} else if (j > high) {
a[k] = aux[i++];
} else if (isLess(aux[j], aux[i])) {
a[k] = aux[j++];
} else {
a[k] = aux[i++];
}
}
}

bool isLess(Comparable v, Comparable w) {
return (v.compareTo(w) < 0);
}

show(String text, List<Comparable> a) {
print(text);
for (int i = 0; i < a.length; i++) {
print(a[i]);
}
}
56 changes: 56 additions & 0 deletions Dart/QuickSort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
void main() {
List<Comparable> numbers = [5, 1, 4, 2, 8];
List<Comparable> letters = ['q', 'a', 'd', 'e', 'h'];
show("Old Array", numbers);
sort(numbers);
show("New Array", numbers);
}

// quicksort the array a[] using 3-way partitioning
void sort(List<Comparable> a) {
sortWithComparable(a, 0, a.length - 1);
}

// quicksort the subarray a[high .. low] using 3-way partitioning
void sortWithComparable(List<Comparable> a, int high, int low) {
if (low <= high) {
return;
}

int lt = high, gt = low;
Comparable v = a[high];
int i = high;
while (i <= gt) {
int cmp = a[i].compareTo(v);
if (cmp < 0) {
// exchange a[i] and a[j]
exchange(a, lt++, i++);
} else if (cmp > 0) {
exchange(a, i, gt--);
} else {
i++;
}
}

// a[high..lt-1] < v = a[lt .. gt] < a[gt+1 .. low].
sortWithComparable(a, high, lt - 1);
sortWithComparable(a, gt + 1, low);
}

bool isLess(Comparable v, Comparable w) {
return (v.compareTo(w) < 0);
}

// exchange a[i] and a[j]
void exchange(List a, int i, int j) {
var swap = a[i];
a[i] = a[j];
a[j] = swap;
}

show(String text, List<Comparable> a) {
print(text);
for (int i = 0; i < a.length; i++) {
print(a[i]);
}
}
39 changes: 39 additions & 0 deletions Dart/SelectionSort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
void main() {
List<Comparable> numbers = [5, 1, 4, 2, 8];
List<Comparable> letters = ['q', 'a', 'd', 'e', 'h'];
show("Old Array", numbers);
sort(numbers);
show("New Array", numbers);
}

// Selection Sort
void sort(List<Comparable> a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int min = i;
for (int j = i + 1; j < N; j++) {
if (isLess(a[j], a[min])) {
min = j;
}
}
// exchange a[i] and a[j]
exchange(a, i, min);
}
}

bool isLess(Comparable v, Comparable w) {
return (v.compareTo(w) < 0);
}

void exchange(List a, int i, int j) {
var swap = a[i];
a[i] = a[j];
a[j] = swap;
}

show(String text, List<Comparable> a) {
print(text);
for (int i = 0; i < a.length; i++) {
print(a[i]);
}
}