نبدأ أولا بـ برنامج Sorting Array using Quick Sort
Code:
#include<iostream.h>
const int max=10;
void swap(int arr[],int x,int y)
{
int temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
int partition(int arr[],int first,int last)
{
int lastele=first;
for(int i=first+1;i<=last;i++)
{
if(arr[first]>=arr[i])
{
++lastele;
swap(arr,lastele,i);
}
}
swap(arr,first,lastele);
return lastele;
}
void quick(int arr[],int first,int last)
{
if(first>=last)return ;
int split=partition(arr,first,last);
quick(arr,first,split-1);
quick(arr,split+1,last);
}
int main()
{
int arr[max];
cout<<"Enter The Array Before Sorting:"<<endl;
for(int i=0;i<max;i++)
cin>>arr[i];
quick(arr,0,max);
cout<<"The Array After Sorting Is:"<<endl;
for(int j=0;j<max;j++)
cout<<arr[j]<<endl;
return 0;
}
برنامج بحث في array عن طريق RECRSION_SEARCH
Code:
#include<iostream.h>
int rec_search(int a[],int n,int target)
{
if(n==0)
return -1;
if(target==a[n-1])
return n;
else
return rec_search(a,n-1,target);
return-1;
}
int main()
{
int arr[100],t,nn;
cout<<"Enter The Length Of The Array :"<<endl;
cin>>nn;
cout<<"Enter The Array :"<<endl;
for(int j=0;j<nn;j++)
cin>>arr[j];
cout<<"Enter The Target :"<<endl;
cin>>t;
int b=rec_search(arr,nn,t);
cout<<"You Found The Element At Number "<<b<<endl;
return 0;
}
برنامج مثل السابق ولكن باستخدام RECRSION_BINARY_SEARCH
Code:
#include<iostream.h>
int rec_binary_search(int a[],int n,int first,int last,int target)
{
if(n==0)
return -1;
if(first>last)
return -1;
int mid=(first+last)/2;
if(target==a[mid])
return mid+1;
else
if(target>a[mid])
return rec_binary_search(a,n,mid+1,last,target);
else
return rec_binary_search(a,n,first,mid-1,target);
}
int main()
{
int arr[10],t;
int nn=10;
cout<<"Enter The Array :"<<endl;
for(int i=0;i<nn;i++)
cin>>arr[i];
cout<<"Enter The Target :"<<endl;
cin>>t;
int b=rec_binary_search(arr,nn,0,9,t);
cout<<"You Found The Element AT Number "<<b<<endl;
return 0;
}
برنامج للتحويل من النظام ديسيمال الي بايناري Decimal to Binary
Code:
#include<iostream.h>
#include<process.h>
void BinPrint(int x,int n)
{
if(n==0)
return;
if(n>0)
BinPrint(x/2,n-1);
cout<<x%2;
}
int main()
{
int xx;
int nn;
cout<<"Enter The Number :"<<endl;
cin>>xx;
cout<<"Enter The Length :"<<endl;
cin>>nn;
cout<<"The Binary Code Is :"<<endl;
BinPrint(xx,nn);
cout<<endl;
return 0;
}
برنامج مثل السابق ولكن بأسلوب أسهل نوعا ما
Code:
#include<iostream.h>
void Binary(int x)
{
int x2;
while(x>0)
{
x2=x%2;
Binary(x/2);
cout<<x2;
break;
}
}
int main()
{
int x1;
cout<<"Enter A Decimal Number :"<<endl;
cin>>x1;
cout<<"The Binary Code Is :"<<endl;
Binary(x1);
cout<<endl;
return 0;
}
برنامج Stack
Code:
#include<iostream.h>
#include<assert.h>
typedef char El;
class Stack
{
public:
Stack();
~Stack();
void push(El &elem);
El pop();
El Top();
bool isEmpty();
private:
struct Node;
typedef Node *link;
struct Node
{
El data;
link next;
};
link top;
};
///////////////////////////////////////////////////////////////////
Stack::Stack()
{
top=0;
}
void Stack::push(El &elem)
{
link newnode=new Node;
assert(newnode);
newnode->data=elem;
newnode->next=top;
top=newnode;
}
El Stack::pop()
{
assert(top);
link temp=top;
top=top->next;
El Elem=temp->data;
delete temp;
return Elem;
}
El Stack::Top()
{
assert(top);
return top->data;
}
bool Stack::isEmpty()
{
return top==0;
}
Stack::~Stack()
{
link ptr;
while(top)
{
ptr=top;
top=top->next;
delete ptr;
}
}
int main()
{
Stack S;
El ch;
cout<<"Enter Char Terminate With . :"<<endl;
cin.get(ch);
do
{
S.push(ch);
cin.get(ch);
}while(ch!='.');
cout<<"The Reverse Is :"<<endl;
while(!S.isEmpty())
{
cout<<S.pop();
}
cout<<endl;
return 0;
}
برنامج بحث وحذف واضافة في Array
Code:
#include<iostream.h>
#include<process.h>
void Delete(int arr[],int& n,int p)
{
for(int i=p;i<n;i++)
arr[i]=arr[i+1];
n--;
}
void insert(int arr[],int& n,int num,int p)
{
for(int i=n;i>=p;i--)
arr[i+1] = arr[i];
arr[p]=num;
n++;
}
int Search(int arr[],int n,int num)
{
for(int i=0;i<n;i++)
if(arr[i]==num)
return i;
return -1;
}
int FirstSmaller(int arr[],int n,int from,int& value)
{
for(int i=from;i<n;i++)
if(arr[i]<value)
{
value = arr[i];
return i;
}
return -1;
}
int main()
{
cout<<"Deletion"<<endl;
int a[100];
cout<<"Enter the array"<<endl;
for(int i=0;i<10;i++)
cin>>a[i];
int n=10;
cout<<"Enter the postion : ";
int p;
cin>>p;
Delete(a,n,p);
for(i=0;i<n;i++)
cout<<a[i]<<endl;
cout<<"Insertion"<<endl;
cout<<"Enter the number:";
int num;
cin>>num;
cout<<"Enter the postion : ";
cin>>p;
insert(a,n,num,p);
for(i=0;i<n;i++)
cout<<a[i]<<endl;
cout<<endl;
cout<<"Search"<<endl;
cout<<"Enter the number : ";
cin>>num;
cout<<"The number "<<num<<"is Found at "<<Search(a,n,num)<<endl;
cout<<"FirstSmaller"<<endl;
cout<<"Start from:";
int from;
cin>>from;
cout<<"Enter the value : ";
cin>>num;
cout<<"The number "<<num<<" Found at position "<<FirstSmaller(a,n,from,num)<<endl;
cout<<"Enter 1,2,3,4,5:"<<endl;
return 0;
}
وأتمني أن أكون قد وفقت والمرة القادة ان شاء الله ستكون في File Structure
Bookmarks