Friday, 7 February 2020

Bubble Sort in C++

Theory

Bubble sort is the most simple type of sort that you can think when performing the sort operation. The first element gets compared with the adjacent element and then swapped(increasing or decreasing depending on user's choice) with the adjacent element and this is done until it finds the right place for the element. This goes on till every element gets sorted.

Time Complexity: n^2(Worst and average) 

Program:


int i, j, n, temp;
cin>>n;
int a[n];
 for(i=0; i<n; i++)
{
       cin>>a[i];
}

  for(i=0; i<n-1; i++)
        {
            for(j=0; j<n-i-1; j++)
            {
                if(a[j] > a[j+1])
                {
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp; //Can also be done by reference
                }
            }   //End of j-for loop
        }   //End of i-for loop

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

Input: 
5
10 2 8 7 3

Output:
2 3 7 8 10

Tuesday, 4 September 2018

Binary Search

It works on the principle of divide and conquer. You are given an array(In Sorted order). Now consider that the number that you are trying to search is at the last position(which is the worst case in Linear Search.) but with the binary search, you can do that in lesser time. Below is an Example of Binary Search(We will use C++):

Example:

void binarySearch(int arr[], int size, int element) {
  int first=0;
  int last=size-1;
  int middle=(first+last)/2;
  while(last>=first)
  {
    if(arr[middle]<element){ first=middle+1; }
    else if(arr[middle]==element){ cout<<"Element found at position: "<<middle+1; break; }
    else{ last=middle-1; }
    middle=(first+last)/2;
  }
}

int main()
{
clrscr();
int arr[50], size, x;
cout<<"Enter the size of array: ";
cin>>size;
cout<<"Enter the Elements of Array: \n";
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"Enter the Element to Search: ";
cin>>x;
binarySearch(arr,size,x); //Calling the BinarySearch Function
return 0;
}

Output:

Enter the size of array: 3
Enter the Elements of Array: 
2 4 6
Enter the Element to Search: 6
Element found at position: 3 

Thursday, 31 May 2018

Information Hiding in C++ 

Information hiding is the most useful technique that can be used in the program to make it more powerful. This is basically done by using Encapsulation


As we all know the Encapsulation is wrapping up of data into a single unit. The most basic examples of Encapsulation is Classes. We can have those data members(variables) and member functions(functions or methods in JAVA) in a single unit. The programmer can make the program less complex and make it easy to read. 

Saturday, 28 October 2017

Binary Search 


Let's understand without going into the details.
Let's say we have a sorted array, say in ascending order like:
1, 4, 10, 13, 40, 65, 100.

Now let's say I searched for 10. The program will execute and it will look at the middle term and will compare it with our number. If the match is found then it will show the position of array. And if it is smaller than the middle term then it will look for the numbers before the middle term, and this will go on until the number is found. If not found, then we will display: "Not found".

Program:


void main()
{
clrscr();
int n, i, arr[20], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
getch();
}

Monday, 11 September 2017

Linear Search. 



One of the easiest method for searching the element in an array.

Structure:

int Array[10], x; //x is variable for search 
for (int i =0 ; i<10; i++) //Initalize, condition, increment 
{
cin>>Array[i];

cout<<"Enter the element to Search: ";
cin>>x;
for (int i =0 ; i<10; i++) //Initalize, condition, increment 
{
if(Array[i] == x){ cout<<"Number found at position" <<i+1;  break;}
}

Tuesday, 25 July 2017

Handling the Patterns in C++


___________________________________________________________
These are often asked and the funny thing is we still don't pay much attention to this.
___________________________________________________________
Consider the Example:
*
**
***
****

Now , for a beginner it's a little bit challenging.

There are two ways to do this:
1. Just print everything with "COUT"
2. Using Loops(Your Logic)
________________________________________________________

The first can be done by anybody so lets start with second One.

for(int i=  0; i<=5 ;i++)
{
for(int j=0; j<i; j++)
{
cout<<"*";
}
cout<<"\n";
}
________________________________________________________

Explanation: 

This works from outer to inner. When i was 0, condition was true, loop goes inside, now j=0 and second condition is false so the loop comes outside and prints "\n"(A new Line).
Now the value of i=1, condition is true, loop goes inside, j=0, and the condition is true, so inner loop runs and prints "*".
Now the inner loop runs one more time and the j becomes 1 and now the condition is false, so inner loop terminates. back to the outer loop.
Like this the loop will go on until the i becomes greater than 5 and you will obtain an output. 

Tuesday, 20 June 2017

Easiest way to use onClick method in Android. 

It can be done by using the XML. Just define a Button with 'id' and and 'onClick'.

Like: 
<Button 
android: height = "wrap_content"
android: height = "wrap_content"
android: text = "Click it"
android: id = "@id/click_button_1"
android: onClick = "Do_the_Click"
/>

So when you will define the onClick, there will be a suggestion on the left side of the statement. Click on that, it would suggest to make a method in java file with name "Do_the_Click", Click on it and it will create it.

And then you can write your code in that method.