Created
December 17, 2018 07:15
-
-
Save PulkitAgg/5ab12486dcb84c221e97e3bf118a75e1 to your computer and use it in GitHub Desktop.
Chronological Order Sorting Using Bubble Sort.
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
//Problem: You have to sort the string array in chronological order without using any external sorting method. | |
//Solution: | |
A = ["ab","aa","ca",'ab']; // user input | |
for(let count=0; count< A.length; count++) { | |
for(let innerCount=0; innerCount<A.length - count -1; innerCount++) { | |
if(A[innerCount] > A[innerCount+1]) { | |
// swap the elements. | |
let temp = A[innerCount+1]; | |
A[innerCount+1] = A[innerCount]; | |
A[innerCount] = temp; | |
} | |
} | |
} | |
console.log(A); // print the sorted array. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment