Wednesday, September 26, 2012

Өгөгдлийн бүтэц bubblesort

#include <cstdlib>
#include <iostream>

using namespace std;
void swap (int a[], int i, int j)
     {
     int temp=a[i];
     a[i]=a[j];
     a[j]=temp;
     }

void bubblesort (int a[], int n){
    int i,j;
    for(i=n-1; i>0; i--)
    for(j=0; j<i; j++)
    if (a[j]>a[j+1])
    swap(a,j,j+1);
}              
              
              
              
void shellsort(int a[], int n){
    int h,i,j, temp, right, left;
    h=n/2;
    while(h>0){
    left=0; right=h;
    while (right<n){
     i=right; temp=a[i];j=i-h;
    while(j>=0 && a[j]>temp){
    a[j+h]=a[j]; j-=h;
    }
    a[j+h]=temp; left++; right++;
    }
    h/=2;
}
}     

  
        
int main(int argc, char *argv[]){
    int a[10];
    int i,j,n=10;
    for (i=0; i<n; i++)
    {
        a[i]=rand()%100;
        cout<<a [i]<<" ";
        }cout <<endl;
       
       
       // shellsort(a,n);
        bubblesort(a, n);
       
       
       
        for(i=0; i<n; i++)
        {
                 cout<<a[i]<<" ";
                 }cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
                                                           Админ Ээгий

No comments:

Post a Comment