Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created August 22, 2016 20:50
Show Gist options
  • Select an option

  • Save DreamVB/c7af7528053fbe50bca849592ed22135 to your computer and use it in GitHub Desktop.

Select an option

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.
// 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