Wednesday, September 26, 2012

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

#include <cstdlib>
#include <iostream>

using namespace std;
void insertionsort(int a[], int n){
     int i,j;
     int elem;
     for (i=1; i<n; i++){
         elem=a[i]; j=i-1;
         while (j>=0 && elem<a[j]){
               a[j+1]=a[j];
               j=j-1;
               }
               a[j+1]=elem;
               }
               }
              
              
              
              
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);
        insertionsort(a, n);
       
       
       
        for(i=0; i<n; i++)
        {
                 cout<<a[i]<<" ";
                 }cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
                                                           Админ Ээгий

No comments:

Post a Comment