Created
December 10, 2018 20:24
-
-
Save SajidK25/93983318c5a2eddb2ef2fc9af19d1772 to your computer and use it in GitHub Desktop.
Bubble Sort Implementation using Dart Programming Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void bubbleSort(var L) { | |
var n = L.length; | |
for (var i = 0; i < n; i++) { | |
for (var j = 0; j < n - i - 1; j++) { | |
if (L[j] > L[j + 1]) { | |
var temp = L[j]; | |
L[j] = L[j + 1]; | |
L[j + 1] = temp; | |
} | |
} | |
} | |
} | |
//recursive | |
void recursiveBubbleSort_desc(var L, var n) { | |
if (n == 1) return; | |
for (var i = 0; i < n - 1; i++) | |
if (L[i] < L[i + 1]) { | |
var temp = L[i]; | |
L[i] = L[i + 1]; | |
L[i + 1] = temp; | |
} | |
recursiveBubbleSort_desc(L, n - 1); | |
} | |
void main() { | |
print("==============Acending order============="); | |
var lst = [8,1,77,12,67,5,19,4,7,10,100]; | |
print("Before sort lst: ${lst}"); | |
bubbleSort(lst); | |
print("After sort lst: ${lst}"); | |
print("==============Decending order========="); | |
var dlst = [8,1,77,12,67,5,19,4,7,10,100]; | |
var n = dlst.length; | |
print("Before sort lst: ${dlst}"); | |
recursiveBubbleSort_desc(dlst, n); | |
print("After sort lst: ${dlst}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is also an easier sort technique.
void main(){
List nameAndAge = [
Person("Jahangir", 41),
Person("Ibrahim", 5),
Person("Labib", 13),
Person("Tahera", 12),
Person("Jasia", 4),
Person("Safowan", 5),
Person("Sheuly", 34),
Person("Alamgir", 38),
Person("Maliha", 5),
Person("Bashira", 10),
];
nameAndAge.sort((a, b) => a.age.compareTo(b.age));
for(Person nameAge in nameAndAge){
print(nameAge);
}
}
class Person{
String name;
int age;
Person(this.name, this.age);
@OverRide
String toString() => "$name ($age)";
}