خمسة من اهم خوارزميات في لغة سي بلاس بلاس selection and quick and insertion and sequential and binary
//member function
void insertion_sort(int arr[], int length);
void print_array(int array[],int size);
void insertion_sort(int arr[], int length);
void print_array(int array[],int size);
int main() {
int array[5]= {9,12,6,7,1};
insertion_sort(array,5);
insertion_sort(array,5);
return 0;
}//end of main
}//end of main
void insertion_sort(int arr[], int length) {
int i, j ,tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}//end of while loop
print_array(arr,5);
}//end of for loop
}//end of insertion_sort.
int i, j ,tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}//end of while loop
print_array(arr,5);
}//end of for loop
}//end of insertion_sort.
void print_array(int array[], int size){
cout<< "sorting: ";
int j;
for (j=0; j<size;j++)
cout <<" "<< array[j];
cout << endl;
}
int j;
for (j=0; j<size;j++)
cout <<" "<< array[j];
cout << endl;
}
★★★★ ★★★★ ★★★★ ★★★★
Quick Sort
#include<iostream.h>
void swap(int&,int&);
int partition(int[],int ,int);
void quicksort(int[],int ,int);
main()
{ int data[7]={11,52,83,68,61,99,10};
cout<<"befor sorting "<<endl;
for(int i=0;i<7;i++)
cout<<" "<<data[i]; cout<<endl;
quicksort(data,0,7);
cout<<"after sorting "<<endl;
for(int j=0;j<7;j++)
cout<<" "<<data[j]; cout<<endl; }
#include<iostream.h>
void swap(int&,int&);
int partition(int[],int ,int);
void quicksort(int[],int ,int);
main()
{ int data[7]={11,52,83,68,61,99,10};
cout<<"befor sorting "<<endl;
for(int i=0;i<7;i++)
cout<<" "<<data[i]; cout<<endl;
quicksort(data,0,7);
cout<<"after sorting "<<endl;
for(int j=0;j<7;j++)
cout<<" "<<data[j]; cout<<endl; }
void swap(int& a,int& b)
{int temp=a;
a=b;
b=temp; }
{int temp=a;
a=b;
b=temp; }
int partition(int data[],int l,int r)
{int pivot=data[l];
int index=l;
for(int i=l+1;i<=r;i++)
{if(data[i]<pivot)
{index++;
swap(data[i],data[index]); }}
swap(data[l],data[index]);
return index; }
{int pivot=data[l];
int index=l;
for(int i=l+1;i<=r;i++)
{if(data[i]<pivot)
{index++;
swap(data[i],data[index]); }}
swap(data[l],data[index]);
return index; }
void quicksort(int data[],int l,int r)
{int x;
if(l<r)
{x=partition(data,l,r);
quicksort(data,l,x-1);
quicksort(data,x+1,r);}}
{int x;
if(l<r)
{x=partition(data,l,r);
quicksort(data,l,x-1);
quicksort(data,x+1,r);}}
★★★★ ★★★★ ★★★★ ★★★★
sequential search
#include<iostream.h>
const s=5;
main(){
int bb[s],x,i,c=0;
cout<<"enter the array : "<<endl;
for(i=0;i<s;i++)
cin>>bb[i];
cout<<"enter the element : "<<endl;
cin>>x;
for(int j=0;j<s;j++)
if (bb[j]==x)
c=j;
if(c>0)
cout<<"found n location : "<<c+1<<endl;
else
cout<<"not found in the array ";
}
#include<iostream.h>
const s=5;
main(){
int bb[s],x,i,c=0;
cout<<"enter the array : "<<endl;
for(i=0;i<s;i++)
cin>>bb[i];
cout<<"enter the element : "<<endl;
cin>>x;
for(int j=0;j<s;j++)
if (bb[j]==x)
c=j;
if(c>0)
cout<<"found n location : "<<c+1<<endl;
else
cout<<"not found in the array ";
}
★★★★ ★★★★ ★★★★ ★★★★
selection sort
#include<iostream.h>
const int n=7;
main()
{
int array[n];
int j,small,i,temp,z,x;
cout<<"enter the array "<<endl;
for(z=0;z<n;z++)
cin>>array[z];
#include<iostream.h>
const int n=7;
main()
{
int array[n];
int j,small,i,temp,z,x;
cout<<"enter the array "<<endl;
for(z=0;z<n;z++)
cin>>array[z];
for(j=0;j<n;j++)
{small=j;
for(i=j+1;i<n;i++)
if(array[i]<array[small])
{small=i;
temp=array[j];
array[j]=array[small];
array[small]=temp;} }
{small=j;
for(i=j+1;i<n;i++)
if(array[i]<array[small])
{small=i;
temp=array[j];
array[j]=array[small];
array[small]=temp;} }
for(x=0;x<n;x++)
cout<<array[x]<<" "; }
cout<<array[x]<<" "; }
★★★★ ★★★★ ★★★★ ★★★★
binary search
#include<iostream.h>
#include<conio.h>
main() {
int abb[4]; int r; int initial=0; int final=4; int mid; int location=-5;
cout<<"Enter 5 numbers to store in array: "<<endl;
for(int i=0; i<5; i++)
{ cin>>abb[i]; }
cout<<endl;
cout<<"Enter the number you want to found :";
cin>>r;
cout<<endl;
while(initial<=final)
{ mid= (initial+final)/2;
if(abb[mid]==r)
{ location=mid; break; }
if(r<abb[mid]) final=mid-1;
if(r>abb[mid]) initial=mid+1; }
if(location==-5) cout<<" Required number not found "<<endl;
else cout<<" Required number is found at index "<<location<<endl;
getch(); }
#include<iostream.h>
#include<conio.h>
main() {
int abb[4]; int r; int initial=0; int final=4; int mid; int location=-5;
cout<<"Enter 5 numbers to store in array: "<<endl;
for(int i=0; i<5; i++)
{ cin>>abb[i]; }
cout<<endl;
cout<<"Enter the number you want to found :";
cin>>r;
cout<<endl;
while(initial<=final)
{ mid= (initial+final)/2;
if(abb[mid]==r)
{ location=mid; break; }
if(r<abb[mid]) final=mid-1;
if(r>abb[mid]) initial=mid+1; }
if(location==-5) cout<<" Required number not found "<<endl;
else cout<<" Required number is found at index "<<location<<endl;
getch(); }
★★★★ ★★★★ ★★★★ ★★★★
By HamedDiyala
By HamedDiyala
خمسة من اهم خوارزميات في لغة سي بلاس بلاس selection and quick and insertion and sequential and binary
Reviewed by حامد طالب العراقي
on
5/17/2015 08:00:00 م
Rating:
ليست هناك تعليقات: