Last active
December 10, 2018 10:56
-
-
Save SajidK25/578d466311b4c3eacd80a6b56627eef5 to your computer and use it in GitHub Desktop.
Insertion sort [ Ascending and descending ] 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 insertionSort_asc(var L){ | |
int n=L.length; | |
for(var i=1;i<n;i++){ | |
var item=L[i]; | |
var j=i-1; | |
while(j>=0 && L[j]>item){ | |
L[j+1]=L[j]; | |
j=j-1; | |
L[j+1]=item; | |
} | |
} | |
} | |
void insertionSort_dsc(var L){ | |
int n=L.length; | |
for(var i=1;i<n;i++){ | |
var item=L[i]; | |
var j=i-1; | |
while(j>=0 && L[j]<item){ | |
L[j+1]=L[j]; | |
j=j-1; | |
L[j+1]=item; | |
} | |
} | |
} | |
void main() { | |
print("============ Descending ============="); | |
var L1=[8,1,77,12,67,5,19,4,7,10,100]; | |
print("Before sort L1: ${L1}"); | |
insertionSort_dsc(L1); | |
print("After sort L1: ${L1}"); | |
print("============ Ascending ============="); | |
var L2=[8,1,77,12,67,5,19,4,7,10,100]; | |
print("Before sort L2: ${L2}"); | |
insertionSort_asc(L2); | |
print("After sort L2: ${L2}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment