Friday, 1 December 2017

HOW TO SORT ANY ARRAY WITHOUT WRITING ANY TYPE OF CODE
Helllo friends!!! welcome to my blog after a long time, today I am gonna show you how you can implement sorting and searching any array without writing any code or any prior kowlwdge of complex sorting algorithms  in c and c++  ,probably you did’nt know about it last before.
So guys ,all you have to do is to have small knowledge of what is header files in c and c++.
We all know as header file consist of prototype of some predefined function such as clrscr().getch(),strcmp() etc.
Here in the same way the c and c++ language provide you with the prototype of the sorting algorithm such as quick sort and for searching like binary search algorithm.
We look through the procedure in both c and c++ ,how we can use these header files  along with their function and protype  so let’s start with the basic code in c.
C program
#include<stdio.h>
#include<stdlib.h>//prototype of qsort function
//compare function pointer
int compare(const void *a,const void *b)
{
return(*(int *)a<*(int *)b);
}
int main()
{
int data[]={12,1,13,67,20};
//start ptr,#elements,size,func,ptr
qsort(data,5,sizeof(int),compare);
for(int i=0;i<5;i++)
{
printf(“%d”,data[i]);
printf(“\n”);
}
return 0;
}
Same code in c++ (Descending order)
#include<iostream>
#include<algorithm>
using namespace std;
//compare function pointer
bool compare(int i,int j) {
return(i>j);
}
int main()
{
int data[]={12,1,13,67,20};
//start ptr,end ptr,func,ptr
sort(data,data+5,compare);
for(int i=0;i<5;i++)
cout<<data[i]<<endl;
return 0;
}
Using default sort of algorithm
If you want to array elements in ascending order then c++ provide you with the default sort algorithm.
C++ program
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int data[]={12,1,13,67,20};
//start ptr,end ptr,func,ptr
sort(data,data+5);
for(int i=0;i<5;i++)
cout<<data[i]<<” ”;
return o;
}
from the above programs we can see how predefined function in c++ helps to sort the array without any complex set of code .now we see how the predefined function helps to search an element without any large code……..
c program
//file name :Binary _search.c:
#include<stdio.h>
#include<stdlib.h>
//compare function pointer
int compare(const void *a,const void *b)
{
If(*(int *)a<*(int *)b)
return -1;
If(*(int *)a==*(int *)b)
return 0;
If(*(int *)a>*(int *)b)
return 1;
}
int main() {
int data[]={1,2,3,4,5,};
int key=3;
if(bsearch(&key,data,5,sizeof(int),compare))
cout<<”Found!!!\n”;
else
cout<<”not found\n”;
return 0;
}
C++ program
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{        
int data[]={1,2,3,4,5};
int key=5;
if(binary_search(data,data+5,key))
cout<<”Found!!!\n”;
else
cout<<”not found\n”;
return 0;
}
The Algorithm Library
the  algorithm libraray of c++ helps us to easily implementcommonly used complex function .we discussed the functions for sort and search,but there are many functions available in library for operations such as
·       replace elements in an array
·       rotates the order of elements
if you want to know about all the useful operations ask in the comment box i’ll  show you how the simple use of such library fuctions helps to minimize the code and to the operations easily and effectively
thank you















HOW TO SORT ANY ARRAY WITHOUT WRITING ANY TYPE OF CODE Helllo friends!!! welcome to my blog after a long time, today I am gonna sho...