Open-Source
09-19-2010, 08:28 PM
دوستان میتونند نمونه کدها و نمونه برنامه های C++/C را در این قسمت قرار دهند.
خودم اول شروع میکنم.
این برنامه مرتب سازی ادغامی (Merge Sort) به زبون C++/C هستش.(به درد درس ساختمان داده میخوره).
//************************************
// Merge Sort
//
// http://open-source.blogfa.com
//************************************
# include <iostream.h>
# include <conio.h>
const int n=10;
//Function Declarations
void m_sort(int arr[], int temp[], int left, int right); //Sort
void merge(int arr[], int temp[], int left, int mid, int right); //Merge
//----------------- Main -----------------
void main(){
clrscr();
int arr1[n];
int arr2[n];
int i;
for (i=0;i<n;i++){
cout<<"Enter Num: ";
cin>>arr1[i];
}
//--------------- Merge Sort
m_sort(arr1, arr2, 0, n-1);
cout<<"\n\nMerge Sort:\n\n";
for (i=0;i<n;i++)
cout<<arr1[i]<<" , ";
getch();
}
//-----------------------------------------
//================================================== ============== Sorted
void m_sort(int arr[], int temp[], int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
m_sort(arr, temp, left, mid);
m_sort(arr, temp, (mid+1), right);
merge(arr, temp, left, (mid+1), right);
}
}
//================================================== ================ Merge
void merge(int arr[], int temp[], int left, int mid, int right)
{
int i, left_end, num_n, tmp_pos;
left_end = (mid - 1);
tmp_pos = left;
num_n = (right - left + 1);
//------------------------------------------
while ((left <= left_end) && (mid <= right))
{
if (arr[left] <= arr[mid])
{
temp[tmp_pos] = arr[left];
tmp_pos += 1;
left += 1;
}
else
{
temp[tmp_pos] = arr[mid];
tmp_pos += 1;
mid += 1;
}
}
//------------------------------------------
//------------------------------------------
while (left <= left_end)
{
temp[tmp_pos] = arr[left];
left += 1;
tmp_pos += 1;
}
while (mid <= right)
{
temp[tmp_pos] = arr[mid];
mid += 1;
tmp_pos += 1;
}
//-----------------------------------------
for (i=0; i <= num_n; i++)
{
arr[right] = temp[right];
right -= 1;
}
}
//================================================== ==========================
خودم اول شروع میکنم.
این برنامه مرتب سازی ادغامی (Merge Sort) به زبون C++/C هستش.(به درد درس ساختمان داده میخوره).
//************************************
// Merge Sort
//
// http://open-source.blogfa.com
//************************************
# include <iostream.h>
# include <conio.h>
const int n=10;
//Function Declarations
void m_sort(int arr[], int temp[], int left, int right); //Sort
void merge(int arr[], int temp[], int left, int mid, int right); //Merge
//----------------- Main -----------------
void main(){
clrscr();
int arr1[n];
int arr2[n];
int i;
for (i=0;i<n;i++){
cout<<"Enter Num: ";
cin>>arr1[i];
}
//--------------- Merge Sort
m_sort(arr1, arr2, 0, n-1);
cout<<"\n\nMerge Sort:\n\n";
for (i=0;i<n;i++)
cout<<arr1[i]<<" , ";
getch();
}
//-----------------------------------------
//================================================== ============== Sorted
void m_sort(int arr[], int temp[], int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
m_sort(arr, temp, left, mid);
m_sort(arr, temp, (mid+1), right);
merge(arr, temp, left, (mid+1), right);
}
}
//================================================== ================ Merge
void merge(int arr[], int temp[], int left, int mid, int right)
{
int i, left_end, num_n, tmp_pos;
left_end = (mid - 1);
tmp_pos = left;
num_n = (right - left + 1);
//------------------------------------------
while ((left <= left_end) && (mid <= right))
{
if (arr[left] <= arr[mid])
{
temp[tmp_pos] = arr[left];
tmp_pos += 1;
left += 1;
}
else
{
temp[tmp_pos] = arr[mid];
tmp_pos += 1;
mid += 1;
}
}
//------------------------------------------
//------------------------------------------
while (left <= left_end)
{
temp[tmp_pos] = arr[left];
left += 1;
tmp_pos += 1;
}
while (mid <= right)
{
temp[tmp_pos] = arr[mid];
mid += 1;
tmp_pos += 1;
}
//-----------------------------------------
for (i=0; i <= num_n; i++)
{
arr[right] = temp[right];
right -= 1;
}
}
//================================================== ==========================