Wednesday, 13 August 2014

PROGRAM FOR BUBBLE SORT


  • Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

Step-by-step example[edit]

  • Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
    First Pass:
    5 1 4 2 8 ) \to ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
    ( 1 5 4 2 8 ) \to ( 1 4 5 2 8 ), Swap since 5 > 4
    ( 1 4 5 2 8 ) \to ( 1 4 2 5 8 ), Swap since 5 > 2
    ( 1 4 2 5 8 ) \to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
    Second Pass:
    1 4 2 5 8 ) \to ( 1 4 2 5 8 )
    ( 1 4 2 5 8 ) \to ( 1 2 4 5 8 ), Swap since 4 > 2
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
    Third Pass:
    1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
  • DYNAMIC MEMORY ALLOCATION :=
when the the memory needs of a program can only be determined during runtime,this is called dynamic memory allocation.

 For example, when the memory needed depends on user input. On these cases, programs need to dynamically allocate memory, for which the C++ language integrates the operators new and delete.

THE PROGRAM WRITTEN BELOW IS SELF TESTED AND VERY TRUE.THE CODE IS GIVEN BELOW.

#include<iostream>
using namespace std;
void main()
{
int temp;
    
int n;
int *a;       //pointer
cout<<"enter the size of array\n";
cin>>n;
a=new int[n]; //dynamic memory allocation
for(int z=0;z<n;z++)  //get elements of array
{
cin>>a[z];
}
for(int i=1;i<n-2;i++)  //compare and swap
{
    for(int z=1;z<n;z++)
{
if(a[z]<a[z-1])
{
temp=a[z-1];
a[z-1]=a[z];
a[z]=temp;
      }
}}
for(int z=0;z<n;z++) //display the array after comparing has been done
{
cout<<"\t"<<a[z];}
system("pause");
}

Friday, 1 August 2014

CREATING A GUI AND DISPLAYING TWO IMAGES

  1. MATLAB IS AN VERY EFFICIENT TOOL FOR VARIOUS ENGINEERING PURPOSES.
  2. GUI(GRAPHICAL USER INTERFACE)  is a type of interface that allows users to interact with electronic devices through graphical icons and visual indicators such as secondary notation, as opposed to text-based interfaces, typed command labels or text navigation. GUIs were introduced in reaction to the perceived steep learning curve of command-line interfaces  which require commands to be typed on the keyboard.
  3. WE ARE GONNA CREATE A GUI TO DISPLAY TWO IMAGES IN THE SAME AXES VIA SAME BUTTON.

  • FIRST CLICK ON THE NEW BUTTON FROM THE MENU BAR. GO FOR A GRAPHICAL USER INTERFACE.
  • SAVE YOUR FIGURE . A POP-UP WINDOW WILL APPEAR WHERE YOU CAN DESIGN AND BUILT YOUR GUI. A ".m" FILE ASSOCIATED WITH YOUR FIGURE WILL BE AUTOMATICALLY GENERATED.
  • THE WINDOW WILL APPEAR LIKE THIS.
     
  • YOU CAN CHANGE THE PATTERN IN WHICH YOUR COMPONENTS ARE TO BE APPEARED.



  • SELECT A PUSH BUTTON AND TWO AXES WHERE YOU WILL BE DISPLAYING YOUR IMGAGES.
  • YOU CAN THE STRING OF THE BUTTON BY CHANING ITS STRING IN THE PROPERTY INSPECTOR. AS SHOWN


  • CALLBACK IS A FUNCTION ASSOCIATED WITH EVERY GUI COMPONENTS WHERE THE FUNCTION WHICH IS TO BE PERFORMED BY THAT COMPONENT IS WRITTEN.
  •  OPENING FUNCTION PRESENT IN ANY GUI ".m" FILE IS THE FUNCTION WHICH IS FIRST OPENED WHEN THE GUI IS RUNNED,.
  • DO THE SAME IN YOUR PROGRAM BUT WITH THE MODIFICATIONS THAT "n" SHOULD BE THE A STRING WHICH IS THE DEFAULT DIRECTORY FOR YOUR CURRENT MATLAB FOLDER.

  •  

THE CODE IS GIVEN AS :

RUN YOUR GUI AND SELECT ANY IMAGE.