Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

mergeSort

#include <iostream>

using namespace std;

int n,v[100];

void citire (){
  int i;
  cin>>n;                   
  for(i=1;i<=n;i++)
    cin>>v[i];
 }

void afisare (){
  int i;
  for(i=1;i<=n;i++)
  cout<<v[i]<<" ";
}

void ordonare (int p, int u ){
  int aux;
  if(v[p]>v[u]){ 
    aux=v[p];
    v[p]=v[u];       
    v[u]=aux;
  }
}

void interclasare (int p, int u , int m ){
  int i , k=0, j,a[100];
  i=p;
  j=m+1;
  while (i<=m&&j<=u)
    if(v[i]<v[j]) {
       k++;
       a[k]=v[i];
       i++;
    }
    else {
      k++;
      a[k]=v[j];
      j++;
    }
    if(i<=m)
      for (j=i;j<=m;j++) {
        k++;
        a[k]=v[j];
    }
    else for(i=j;i<=u;i++) {
      k++;
      a[k]=v[i];
    }
  for(i=1;i<=k;i++) v[p+i-1]=a[i] ;
}

void divide (int p , int u ) {
  int m ;
  if(u<=p+1)     
  ordonare (p,u);
  else {
    m=(p+u)/2;
    divide(p,m);
    divide(m+1,u);
    interclasare(p,u,m);
    }
}

int main () { 
  citire ();
  divide (1,n);
  afisare();
  return 1;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.