Amazon Affiliate

Thursday 31 May 2012

PROGRAM FOR BUBBLE SORT FOR SORTING THE STRINGS...

// BUBBLE SORT FOR SORTING THE STRINGS


 # include<iostream.h>
 # include <stdlib.h>
 # include <string.h>
 # include<alloc.h>
 # include<stdio.h>

 class bubble_string
    {
    private:   char *temp;
           int i, j;
    public:
           void bubble_sort(char *array[], int );
           void display(char *list[], int);
     };


void bubble_string :: bubble_sort(char *array[], int size)
 {

   for (i = 0; i < size; i++)
    for (j = 0; j < size; j++)
      if (strcmp(array[i], array[j]) < 0)
    {
      temp =(char *) malloc(sizeof(array[i]));
      temp = array[i];
          array[i] = array[j];
          array[j] = temp;
        }
  }
    void bubble_string :: display(char *list[], int n)
      {

    for(int i = 0 ; i < n; i++)
       {
         cout<<"\n "<<list[i];
       }
      }


void main(void)
 {
   bubble_string b_sort;
   char *list[100] = {"MONDAY", "TUESDAY", "WENSDAY", "FRIDAY", "SATARDAY"};
   cout<<"\n Input list is as follows:";
   b_sort.display(list, 5);
   b_sort.bubble_sort(list, 5);
   cout<<"\n Sorted list is as follows:\n";
   b_sort.display(list, 5);
 }

No comments:

Post a Comment