Skip to content

Instantly share code, notes, and snippets.

@sachin-handiekar
Created March 11, 2012 18:01
Show Gist options
  • Save sachin-handiekar/2017449 to your computer and use it in GitHub Desktop.
Save sachin-handiekar/2017449 to your computer and use it in GitHub Desktop.
Bubble Sort / String [ Ascending Order ]
/*
* Bubble Sort / String - Ascending order
*/
#include <iostream>
#include <string.h>
using namespace std;
const int MAX = 10;
class array
{
private:
char *arr[MAX];
int count;
public:
array();
void add(char *item);
void sort();
void display();
};
//initialises data member
array::array()
{
count = 0;
for(int i=0; i<MAX; i++)
arr[i]="";
}
//adds a new element to the array
void array::add(char *item)
{
if(count < MAX) {
arr[count] = item;
count++;
} else {
cout << "\nArray is full." << endl;
}
}
void array::sort()
{
char *temp;
for(int i=0; i<=count - 2; i++) {
for(int j=0; j<=count-2-i; j++) {
if(strcmp(arr[j],arr[j+1]) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void array::display()
{
for(int i=0; i<count; i++) {
cout<< arr[i] << "\t";
}
cout << endl;
}
int main()
{
array a;
a.add("zeus");
a.add("alpha");
a.add("bravo");
a.add("delta");
a.add("charlie");
a.add("mike");
cout<< endl << "Array before sorting..." << endl;
a.display();
a.sort();
cout<< endl << "Array after sorting..." << endl;
a.display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment