Created
August 22, 2016 20:50
-
-
Save DreamVB/c7af7528053fbe50bca849592ed22135 to your computer and use it in GitHub Desktop.
Bubble sort algorithm to sort a array of words into acsending / desending order.
This file contains hidden or 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
| // Sort words assending or desending order. | |
| // By DreamVB | |
| #include <iostream> | |
| using namespace std; | |
| using std::cout; | |
| using std::endl; | |
| template <typename T>void BSortNames(T *item, int size, bool asending){ | |
| int i = 0; | |
| T t = 0; | |
| bool Order = true; | |
| while(i < size){ | |
| for(int j = i+1;j<size;j++){ | |
| if(asending){ | |
| Order = (item[i][0] > item[j][0]); | |
| }else{ | |
| Order = (item[i][0] < item[j][0]); | |
| } | |
| if(Order){ | |
| t = item[j]; | |
| item[j] = item[i]; | |
| item[i] = t; | |
| } | |
| } | |
| i++; | |
| } | |
| } | |
| template <typename T> void Display(T *items, int size){ | |
| int i = 0; | |
| while(i < size){ | |
| cout << i+1 << " " << items[i] << endl; | |
| i++; | |
| } | |
| cout << endl; | |
| } | |
| int main(int argc, char **anvg){ | |
| //Add some random computer lanuages | |
| char *languages[] = {"C++","VB","J++","JAVA","B","ASM","PHP" | |
| ,"DELPHI","PASCAL","AUTOIT","BASIC","RUBY","HTML", | |
| "JS","VBS","ASP","POWER BASIC"}; | |
| int len = sizeof(languages) / sizeof(languages[0]); | |
| //Sort word asending | |
| BSortNames(languages,len,true); | |
| Display(languages,len); | |
| cout << endl; | |
| //Sort word desending | |
| BSortNames(languages,len,false); | |
| Display(languages,len); | |
| //Keep console open | |
| system("pause"); | |
| return 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment