u

Thursday 7 June 2012

C++ Program to implement bubble sort for a multi-dimensional array

C++ Program to implement bubble sort for a  multi-dimensional array[10][20].The array is stored through random generation of numbers between the defined range 1 to 500


#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include <conio.h>
#define MAX 500 //Highest possible value that
#define MIN 1 //Lowest possible value that
#define ORDER < //How you want the list sorted.
void main()


    {
    clrscr();
    int i, j, l, array[10][20];
    int row, col, x, y=0, z;
    int temp, moves=0;
    srand((unsigned) time(NULL));
    x=101;

    for(row = 0; row < 10; row++)


    {
        for(col = 0; col < 20; col++)


        {
         x = rand() % MAX + MIN;
         array[row][col] = x;
        }
    }

        for(row = 0; row < 10; row++) //displays the
        //unsorted array


            {
                for(col = 0; col < 20; col++)


                    {
                     cout<<setw(3)<<array[row][col]<<" ";
                    }
                    cout<<endl;
            }
            cout<<"This is the original array"<<endl;
            cout<<"Enter any key"<<endl;
            cin>>l;
        system("cls"); //Clears the screen of the
            //unsorted array
            for(i = 0; i < 200; i++)


                {   
                    z=1;
                   
                for(j = 0; j < 199; j++)   


                        {
                         if(array[y][z] ORDER array[y][z - 1])


                             {
                             temp = array[y][z];
                                array[y][z] = array[y][z - 1];
                                array[y][z - 1]=temp;
                                moves++;
                             }
                             z++;
                            }
                    }
                    for(row = 0; row < 10; row++) //displays the
                    //sorted array


                        {
                            for(col = 0; col < 20; col++)


                                {
                                 cout<<setw(3)<<array[row][col]<<" ";
                                }
                                cout<<endl;
                        }
            cout<<moves<<" moves where made to sort this list"<<endl;
            cout<<"Press any key to continue"<<endl;
            getch();
            }

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More